Overview
The x402 protocol is RelayCore’s implementation of HTTP 402 Payment Required, enabling gasless payments where agents pay for services without submitting blockchain transactions. Instead, agents sign EIP-3009 authorizations off-chain, and the Crypto.com Facilitator SDK settles payments on Cronos. Key Benefits:- Gasless: Agents never pay gas fees
- Instant: No waiting for block confirmations
- Secure: EIP-3009 standard with signature verification
- Scalable: Session-based budgets enable micro-payments
Complete Payment Flow
2. Server Returns 402 Payment Required
The Payment Requirements Breakdown:
requirePayment middleware intercepts the request and returns 402:payTo: Merchant wallet address (service provider)asset: USDC contract address on Cronos TestnetmaxAmountRequired: Amount in base units (10000 = 0.01 USDC with 6 decimals)resource: The protected endpoint URL
3. Agent Generates EIP-3009 Authorization
Agent uses the Facilitator SDK to generate a signed payment header:What’s in the Payment Header:
- EIP-3009
TransferWithAuthorizationparameters - EIP-712 signature proving authorization
- Nonce from USDC contract (prevents replay)
- Validity window (5 minutes)
4. Agent Submits Payment for Settlement
Agent sends the signed authorization to the settlement endpoint:
5. Facilitator Verifies and Settles On-Chain
Server uses Facilitator SDK to verify signature and execute transfer:Gasless Magic: The Facilitator executes
transferWithAuthorization on the USDC contract, paying gas fees while transferring USDC from agent to merchant.6. Server Records Payment and Grants Entitlement
Payment is recorded in database and cached in memory:Server responds with success:
7. Agent Retries Request with Payment ID
Agent retries the original request with the payment ID header:
Session-Based Payments
For agents making multiple requests, session escrow eliminates repeated x402 flows:
Session Benefits:
- One payment, unlimited requests (within budget)
- Real-time balance tracking
- Automatic refunds on expiration
- Perfect for autonomous agents
Implementation Guide
Protecting Routes with x402
Payment Settlement Endpoint
handlePaymentSettlement function:
- Validates payment parameters
- Builds Facilitator verify request
- Verifies EIP-3009 signature
- Settles payment on-chain
- Records in database
- Caches entitlement
- Returns transaction hash
Entitlement Caching
Security Guarantees
EIP-3009 Signature Verification
EIP-3009 Signature Verification
Every payment header contains an EIP-712 signature that proves:
- The payer authorized the exact amount
- The payer authorized the exact recipient
- The authorization is time-bound
- The nonce is unique (prevents replay)
Nonce Tracking
Nonce Tracking
USDC contract maintains a nonce counter for each address. Each
TransferWithAuthorization must use the next sequential nonce. This prevents:- Replay attacks
- Double-spending
- Authorization reuse
Time-Bound Authorizations
Time-Bound Authorizations
Amount Enforcement
Amount Enforcement
The
maxAmountRequired field enforces the exact payment amount. The Facilitator rejects any attempt to:- Overpay
- Underpay
- Change the amount after signing
Resource Binding
Resource Binding
Each payment is bound to a specific
resource URL. Entitlements are only valid for the exact resource that was paid for.Network Configuration
| Network | Chain ID | USDC Contract | Facilitator |
|---|---|---|---|
| Cronos Testnet | 338 | 0xc01efAaF7C5C61bEbFAeb358E1161b537b8bC0e0 | Crypto.com Facilitator SDK |
| Cronos Mainnet | 25 | 0xc21223249CA28397B4B6541dfFaEcC539BfF0c59 | Crypto.com Facilitator SDK |
Environment Variables
Indexer Integration
The Payment Indexer runs every 5 minutes to:- Query Cronos Explorer API for recent USDC transfers
- Match
tx_hashtopaymentstable records - Update
block_numberandstatusfields - Trigger reputation updates for services
Error Handling
| Error | Cause | Resolution |
|---|---|---|
Insufficient USDC balance | Agent wallet lacks USDC | Fund wallet with USDC |
Invalid signature | Signature verification failed | Regenerate payment header |
Nonce already used | Replay attack or stale nonce | Get fresh nonce from contract |
Authorization expired | validBefore timestamp passed | Generate new authorization |
Payment already settled | Duplicate payment ID | Use new payment ID |
Next Steps
Session Escrow
Learn about gasless session budgets
SDK Integration
Integrate x402 in your agent
MCP Tools
Use x402 via MCP server
First Payment Guide
Complete tutorial with code