Overview
Build and deploy a paid API that other agents can use via x402. Use when you or the user want to monetize an API, offer a service, sell a service to other agents, or charge for endpoints.How it works
x402 is an HTTP-native payment protocol. When a client hits a protected endpoint without paying, the server returns HTTP 402 with payment requirements. The client signs a USDC payment and retries with a payment header. The facilitator verifies and settles the payment, and the server returns the response.Prerequisites
- Must be authenticated (
npx awal@latest statusto check)
1. Get the payment address
Get the wallet address that will receive payments:payTo value in your server configuration.
2. Set up the project
index.js:
3. Run and test
API reference
paymentMiddleware(payTo, routes, facilitator?)
| Parameter | Type | Description |
|---|---|---|
payTo | string | Ethereum address (0x…) to receive USDC payments |
routes | object | Route config mapping route patterns to payment config |
facilitator | object? | Optional custom facilitator (defaults to x402.org) |
Route config
Each key is"METHOD /path". The value is either a price string or a config object:
Route config fields
| Field | Type | Description |
|---|---|---|
price | string | USDC price (e.g. “1.00”) |
network | string | Blockchain network: “base” or “base-sepolia” |
config.description | string? | What this endpoint does (shown to clients) |
config.inputSchema | object? | Expected request body/query schema |
config.outputSchema | object? | Response body schema |
config.maxTimeoutSeconds | number? | Max time for payment settlement |
Patterns
Multiple endpoints with different prices
Health check (no payment)
Register free endpoints before the payment middleware:POST with body schema
Using the CDP facilitator
For production use with the Coinbase facilitator:CDP_API_KEY_ID and CDP_API_KEY_SECRET environment variables from the CDP Portal.
Pricing guidelines
| Use Case | Suggested Price |
|---|---|
| Simple data lookup | 0.01 |
| API proxy / enrichment | 0.10 |
| Compute-heavy query | 0.50 |
| AI inference | 1.00 |
Checklist
- Get wallet address with
npx awal@latest address - Install
expressandx402-express - Define routes with prices and descriptions
- Register payment middleware before protected routes
- Keep health/status endpoints before payment middleware
- Test with
curl(should get 402) andnpx awal@latest x402 pay(should get 200) - Announce your service so other agents can find and use it