Overview
The x402 protocol makes the HTTP402 Payment Required status code finally useful after 30+ years. Reserved since HTTP/1.1 in 1997, it now enables autonomous payment flows for AI agents and APIs.
Key insight: Payments become just another HTTP status code—like 401 Unauthorized but for money.
How It Works
The x402 protocol transforms payment requirements into standard HTTP exchanges:Retry with Payment Header
Client retries the request with the
X-PAYMENT header containing the signaturePayment Flow Diagram
Basic Implementation
Server-Side (Express)
The simplest x402 server uses thex402-express middleware:
server.ts
Client-Side
Clients can handle 402 responses automatically:Payment Requirements Format
The402 response must include a payment object:
Amount in token’s smallest unit (e.g., 6 decimals for USDC)Example:
"1000" = 0.001 USDCToken identifier (symbol or contract address)Example:
"USDC" or "0x036CbD53842c5426634e7929541eC2318f3dCF7e"Recipient wallet addressExample:
"0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb"EVM chain IDExample:
84532 (Base Sepolia), 8453 (Base Mainnet)Human-readable network nameExample:
"base-sepolia", "base"URL of the x402 facilitator serviceExample:
"https://x402.org/facilitator"The Facilitator
The facilitator is a critical component that handles blockchain interactions:Responsibilities
Signature Verification
Validates that signatures match the payment message and signer
Balance Checking
Ensures the payer has sufficient USDC balance
Transaction Submission
Submits USDC transfer transactions to the blockchain
Settlement Tracking
Returns transaction hashes for verification
Facilitator API
The facilitator exposes a simple verification endpoint:EIP-712 Typed Data
The x402 protocol uses EIP-712 for human-readable signatures. Users see exactly what they’re signing:Advantages
No Custom Protocols
No Custom Protocols
Uses standard HTTP status codes and headers. No WebSockets, no polling, no custom RPC.
Framework Agnostic
Framework Agnostic
Works with any HTTP server (Express, FastAPI, Cloudflare Workers, etc.)
Agent Friendly
Agent Friendly
AI agents can handle 402 responses just like 401 auth challenges
Cryptographically Secure
Cryptographically Secure
EIP-712 signatures prevent replay attacks and ensure non-repudiation
Blockchain Agnostic
Blockchain Agnostic
Currently supports EVM chains, but protocol design allows for Solana, Bitcoin, etc.
Real-World Examples
Ping
Minimal x402 server - perfect starting point
Weather API
Paid weather data with city parameter
Tweet Agent
Pay to post tweets via agent
Event RSVP
MCP-based event booking with payments
Next Steps
A2A Payments
Learn about agent-to-agent payment patterns
Smart Wallets
Understand Crossmint smart wallet integration
Payment Flow
Deep dive into end-to-end payment flows
Quickstart
Build your first x402 server