HTTP 402 Payment Required with machine-readable pricing details. When the proof is present and valid, the middleware lets the request through transparently — your handler code never changes.
Middleware Configuration
ThecreateX402Middleware function accepts three required parameters and one optional one:
string
required
The amount to charge per request, denominated in USDC (e.g.,
"0.001" for one-tenth of a cent).string
required
The wallet address that receives USDC settlement. This is typically your treasury or operator wallet on Arc testnet.
number
required
The chain to settle on. For Arc testnet this is always
5042002.boolean
When
true, the middleware rejects requests that do not include a valid X-Agent-ID header. Useful if you want to restrict your endpoint to identified ERC-8004 agents only. Defaults to false.Express / Node.js Setup
Installing the middleware in Express takes a singleapp.use call. Mount it on the path you want to protect:
1
Install the FlareHQ SDK
2
Add the middleware to your route
Next.js API Route Setup
Next.js API routes don’t use Express middleware, so you verify the payment proof manually using theflarehq.x402.verifyPaymentProof method:
verifyPaymentProof performs on-chain verification against Arc testnet. The call is fast (typically under 200 ms) because Arc uses Circle’s Gateway API rather than scanning full block history.What the 402 Response Looks Like
When a client hits your paywall without a valid proof, they receive a structured JSON body alongside the402 status code:
X-402-Payment-Proof header attached.
Testing Your Paywall
You can confirm your paywall is working correctly with a plain cURL request. You should receive a402 response with the pricing body:
POST /api/x402/pay with a valid payment proof:
Restricting to Identified Agents Only
If you want your endpoint to accept requests only from ERC-8004 identified agents (and reject anonymous callers), setrequireAgentId: true in the middleware config:
X-Agent-ID header will receive a 401 Unauthorized response before the payment check even runs.
