Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 22 additions & 50 deletions api/search.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import type { VercelRequest, VercelResponse } from '@vercel/node'
import {
USDC_CONTRACT_MAINNET,
USDC_CONTRACT_TESTNET,
import {
STELLAR_NETWORK,
AMOUNT_USDC,
} from '../src/lib/constants'
import {
getNetwork,
buildPaymentRequiredPayload,
getPayTo,
} from '../src/lib/x402Config'
import { consumePaymentPayload } from '../src/lib/paymentIntegrity'
import { formatConfigurationError, readServerConfig } from '../src/lib/config'
import { normalizeOrganicResults } from '../src/lib/serperNormalizer'
import type { SearchResponse, ApiErrorResponse } from '../src/types/index.js'

// ─── Config ───────────────────────────────────────────────────────────────
let config
try {
config = readServerConfig()
} catch (error) {
console.error(formatConfigurationError(error))
throw error
}
const RECEIVING_ADDRESS = config.receivingAddress
const NETWORK = config.stellarNetwork
const SERPER_API_KEY = config.serperApiKey
const AMOUNT_STROOPS = config.amountStroops
const AMOUNT_USDC = config.amountUsdc
const USDC_CONTRACT = NETWORK === 'stellar:mainnet' ? USDC_CONTRACT_MAINNET : USDC_CONTRACT_TESTNET
const NETWORK = getNetwork() as 'stellar:testnet' | 'stellar:mainnet'
const RECEIVING_ADDRESS = getPayTo()
const SERPER_API_KEY = process.env.SERPER_API_KEY!

export default async function handler(req: VercelRequest, res: VercelResponse) {

Expand Down Expand Up @@ -59,28 +55,9 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
req.headers['X-PAYMENT']

if (!paymentHeader) {
// Return x402 v2 payment requirements
// The key fix: asset must be a Soroban C... contract address, NOT "USDC:ISSUER"
const paymentRequired = {
x402Version: 2,
error: 'Payment required',
resource: {
url: `${req.headers['x-forwarded-proto'] || 'http'}://${req.headers['host']}${req.url}`,
description: 'StellarSearch: pay-per-query web search — 0.001 USDC on Stellar',
mimeType: 'application/json',
},
accepts: [
{
scheme: 'exact',
network: NETWORK, // "stellar:testnet"
amount: AMOUNT_STROOPS, // "10000" (stroops, not dollars)
asset: USDC_CONTRACT, // "CBIELTK6..." (Soroban contract)
payTo: RECEIVING_ADDRESS, // your G... address
maxTimeoutSeconds: 300,
extra: { areFeesSponsored: true },
},
],
}
// Return x402 v2 payment requirements from shared config (Issue #108)
const requestUrl = `${req.headers['x-forwarded-proto'] || 'http'}://${req.headers['host']}${req.url}`
const paymentRequired = buildPaymentRequiredPayload(requestUrl)

res.setHeader(
'PAYMENT-REQUIRED',
Expand Down Expand Up @@ -147,19 +124,14 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
const latencyMs = Date.now() - t0

const results = normalizeOrganicResults(data)
const queryMeta = normalizeQueryMetadata(data, q.trim())

const responseBody: SearchResponse = {
query: queryMeta.executedQuery,
originalQuery: queryMeta.originalQuery,
executedQuery: queryMeta.executedQuery,
suggestedQuery: queryMeta.suggestedQuery,
isCorrected: queryMeta.isCorrected,
query: q.trim(),
results,
count: results.length,
network: NETWORK,
paidAmount: AMOUNT_USDC,
currency: 'USDC',
count: results.length,
network: NETWORK,
paidAmount: AMOUNT_USDC,
currency: 'USDC',
txHash,
latencyMs,
}
Expand All @@ -171,4 +143,4 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
const errorBody: ApiErrorResponse = { error: 'Search failed.' }
return res.status(500).json(errorBody)
}
}
}
Loading