The smallest possible paid HTTP agent: add a paywall to one route.
- Protect an Express endpoint with paymentMiddleware from @nevermined-io/payments/express
- Return 402 with the payment-required envelope, verify the payment-signature, settle on success
- Generate an x402 access token on the client with the SDK and retry the call
- Optionally track OpenAI cost per call with Nevermined observability
The client calls /ask with no token and gets 402 Payment Required, carrying the payment-required header. It mints an x402 token with the SDK, retries with the payment-signature header, and the agent verifies, runs, and settles (burns credits) before returning 200 with a payment-response receipt.
import { paymentMiddleware } from '@nevermined-io/payments/express'
app.use(paymentMiddleware(payments, {
'POST /ask': { planId: PLAN_ID, credits: 1 }
}))| src/agent.ts | Express server with a payment-protected /ask endpoint |
| src/agent-observability.ts | same agent, with Nevermined observability tracking OpenAI cost |
| src/client.ts | demo client showing the full x402 payment flow |
Sandbox agent — real payment round-trips (402 → authorize → settle) with a per-session credit balance, no external service and no real money. The buyer mints an x402 access token via the SDK and sends it as `payment-signature`; the sandbox runs that round-trip locally. See the README to point this at a hosted instance of the agent.