Skip to main content

Provider Express

A standalone Express provider with a paywalled search endpoint. Use this as the server when running the agent-basic or agent-loop examples.

What it does

  1. Starts an Express server on port 3001 (configurable via PORT env var)
  2. Creates a Gateway middleware with $0.001 per query pricing
  3. Serves a POST /search endpoint that returns mock search results
  4. Logs payment details when a paid request is received

Source

import express from 'express'
import { createGatewayMiddleware } from '@circle-fin/x402-batching/server'

const app = express()
app.use(express.json())

const gateway = createGatewayMiddleware({
sellerAddress: process.env.CRUMB_WALLET_ADDRESS!,
description: 'Web search — per query',
})

app.post('/search', gateway.require('$0.001'), async (req: any, res) => {
if (req.payment) {
console.log(`Received: ${req.payment.amount} USDC from ${req.payment.payer}`)
}

const query = req.body?.query ?? ''
res.json({
results: [
{ title: `Result 1 for "${query}"`, url: 'https://example.com/1' },
{ title: `Result 2 for "${query}"`, url: 'https://example.com/2' },
{ title: `Result 3 for "${query}"`, url: 'https://example.com/3' },
],
})
})

const port = parseInt(process.env.PORT ?? '3001', 10)
app.listen(port, () => {
console.log(`Provider running on http://localhost:${port}`)
console.log(`Wallet: ${process.env.CRUMB_WALLET_ADDRESS}`)
console.log(`Price: $0.001 per query`)
})

Running

npx tsx --env-file=.env examples/provider-express/index.ts

Expected output

Provider running on http://localhost:3001
Wallet: 0x22b204CF75B960c6cCf3A49D78a3Aa9845953752
Price: $0.001 per query

When an agent makes a paid request:

Received: 0.001 USDC from 0x22b2...

Customization

  • Port: Set the PORT environment variable
  • Price: Change the gateway.require('$0.001') argument
  • Endpoint: Add more routes with different prices
app.get('/premium', gateway.require('$0.05'), premiumHandler)
app.get('/basic', gateway.require('$0.001'), basicHandler)