Next.js Integration
This section documents how a Next.js app integrates with a hosted x402 facilitator.
Required environment variables
Set these in your Next.js deployment:
X402_FACILITATOR_URL=https://cohort-x402-go.vercel.appX402_FACILITATOR_API_KEY=cohort-10-rulesX402_NETWORK=baseUSDC_BASE_ADDRESS=0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913SERVICE_RECIPIENT_ADDRESS=<your receiving address>PRICE_IN_USDC=0.0001
Server: payment requirements
When a request is unpaid, return 402 with a v2 exact payload:
{
"paymentRequirements": {
"scheme": "exact",
"network": "eip155:8453",
"asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
"payTo": "0xYOUR_RECIPIENT",
"amount": "100",
"amountDecimal": "0.0001",
"maxTimeoutSeconds": 600,
"extra": { "name": "USD Coin", "version": "2" },
"x402Version": 2
}
}Notes:
amountis in raw token units (USDC has 6 decimals).amountDecimalis optional but helpful for UI display.extra.namemust beUSD Coinfor Base USDC.- Use
network: eip155:8453for Base.
Client: signing the EIP-712 authorization
- Build the domain using
extra.nameandextra.version:name: "USD Coin"version: "2"chainId: 8453verifyingContract: asset
- Sign
TransferWithAuthorizationviaeth_signTypedData_v4. - Send the x402 payment object in the
X-402-Paymentheader.
Payload shape:
{
"x402Version": 2,
"paymentPayload": {
"accepted": { "scheme": "exact", "network": "eip155:8453" },
"payload": {
"authorization": {
"from": "0xPAYER",
"to": "0xRECIPIENT",
"value": "100",
"validAfter": "1730000000",
"validBefore": "1730003600",
"nonce": "0x..."
},
"signature": "0x..."
}
},
"paymentRequirements": { }
}Server: verify and settle
Call the facilitator:
POST ${X402_FACILITATOR_URL}/verifywithX-API-KeyPOST ${X402_FACILITATOR_URL}/settlewithX-API-Key
If /verify returns isValid: true, call /settle.
Gotchas
- Invalid signature: EIP-712 domain name must be
USD Coin, notUSDC. - Version mismatch: Use
x402Version: 2whennetworkiseip155:8453. - Nested payload: Do not wrap
paymentPayloadinside anotherpaymentPayload. - Recipient field: Facilitator expects
payToandasset. - Address mismatch:
authorization.frommust match the signing address. - Wrong chain: Ensure wallet is on Base (chainId 8453) before signing.
Example files
app/api/secret/route.ts: builds payment requirements, verifies, settlesapp/secret/page.tsx: signs EIP-712 and sendsX-402-Paymentlib/facilitator.ts:verifyandsettlehelpers with API key header
Last updated on