Overview
The x402 server components let you monetize your API endpoints. Clients pay with USDC per request, and payments are settled gaslessly through Chipi’s paymaster. Two main components:X402Facilitator— Verifies payment signatures and settles via paymasterx402Middleware— Express middleware that handles the full 402 flow (also compatible with Express-like frameworks such as Hono)
Express / Node.js
Quick Setup
Middleware Behavior
- No
X-PAYMENTheader → Returns 402 withPAYMENT-REQUIREDheader - Malformed header → Returns 400
- Invalid payment → Returns 402 with error reason
- Valid payment → Settles via paymaster, attaches
req.x402, callsnext()
Access Payment Info
After successful payment,req.x402 contains:
Verify-Only Mode
To verify payments without auto-settling (for custom settlement logic):Custom Nonce Store
By default, the facilitator uses an in-memory nonce store (resets on server restart). For production, plug in a persistent store:Redis Example
PostgreSQL Example
The For cleanup, run periodically:
x402_nonces table must enforce uniqueness on the nonce column:DELETE FROM x402_nonces WHERE created_at < NOW() - INTERVAL '24 hours'Python — FastAPI
Python — Flask
Direct Facilitator Usage
For custom server frameworks or advanced use cases:CORS Configuration
The middleware automatically skipsOPTIONS (preflight) requests. Make sure your CORS setup includes the X-PAYMENT header:
