Skip to main content
The FlareHQ x402 middleware sits in front of your existing route handler and enforces a USDC micro-payment before any request reaches your application logic. When a client arrives without a valid payment proof, the middleware returns 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

The createX402Middleware 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 single app.use call. Mount it on the path you want to protect:
1

Install the FlareHQ SDK

2

Add the middleware to your route

You can mount createX402Middleware on any path prefix, which means you can gate an entire namespace — for example, app.use('/api/v1/premium', ...) — with a single line.

Next.js API Route Setup

Next.js API routes don’t use Express middleware, so you verify the payment proof manually using the flarehq.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 the 402 status code:
Clients that implement x402 — including the FlareHQ SDK and any x402 Marketplace consumer — parse this body automatically, sign a Circle Nanopayment authorization, and retry the request with the 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 a 402 response with the pricing body:
Expected output:
To test a successful paid request, use the FlareHQ SDK or submit directly to POST /api/x402/pay with a valid payment proof:
Plain HTTP clients — such as raw fetch calls or cURL without special headers — will always receive a 402 and must implement their own x402 payment logic to proceed. Only clients using the FlareHQ SDK, the x402 Marketplace executor, or a compatible x402 library can pay automatically.

Restricting to Identified Agents Only

If you want your endpoint to accept requests only from ERC-8004 identified agents (and reject anonymous callers), set requireAgentId: true in the middleware config:
Requests without a valid X-Agent-ID header will receive a 401 Unauthorized response before the payment check even runs.