Skip to main content

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

1

1. Agent Requests Protected Resource

Agent calls a protected API endpoint without payment:
2

2. Server Returns 402 Payment Required

The requirePayment middleware intercepts the request and returns 402:
Payment Requirements Breakdown:
  • payTo: Merchant wallet address (service provider)
  • asset: USDC contract address on Cronos Testnet
  • maxAmountRequired: Amount in base units (10000 = 0.01 USDC with 6 decimals)
  • resource: The protected endpoint URL
3

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 TransferWithAuthorization parameters
  • EIP-712 signature proving authorization
  • Nonce from USDC contract (prevents replay)
  • Validity window (5 minutes)
4

4. Agent Submits Payment for Settlement

Agent sends the signed authorization to the settlement endpoint:
5

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

6. Server Records Payment and Grants Entitlement

Payment is recorded in database and cached in memory:
Server responds with success:
7

7. Agent Retries Request with Payment ID

Agent retries the original request with the payment ID header:
8

8. Server Verifies Entitlement and Returns Content

Middleware checks entitlement cache and database:
Server returns the protected content:

Session-Based Payments

For agents making multiple requests, session escrow eliminates repeated x402 flows:
1

1. Create Session with Budget

Returns 402 for session deposit.
2

2. Pay Session Deposit via x402

Agent pays once to fund the session budget.
3

3. Use Session for All Requests

Budget Tracking:
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

The handlePaymentSettlement function:
  1. Validates payment parameters
  2. Builds Facilitator verify request
  3. Verifies EIP-3009 signature
  4. Settles payment on-chain
  5. Records in database
  6. Caches entitlement
  7. Returns transaction hash

Entitlement Caching

Security Guarantees

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)
The Facilitator SDK verifies all parameters before settlement.
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
Every authorization includes:
  • validAfter: Earliest timestamp for execution
  • validBefore: Latest timestamp for execution
Expired authorizations are rejected by the USDC contract.
The maxAmountRequired field enforces the exact payment amount. The Facilitator rejects any attempt to:
  • Overpay
  • Underpay
  • Change the amount after signing
Each payment is bound to a specific resource URL. Entitlements are only valid for the exact resource that was paid for.

Network Configuration

Environment Variables

Indexer Integration

The Payment Indexer runs every 5 minutes to:
  1. Query Cronos Explorer API for recent USDC transfers
  2. Match tx_hash to payments table records
  3. Update block_number and status fields
  4. Trigger reputation updates for services

Error Handling

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