How It Works
The x402 protocol, payment flows, and technical architecture
x402 protocol flow
Agent → GET /api/data → Server
Agent ← 402 Payment Required (price, network) ← Server
Agent → signs USDC transfer on Base
Agent → GET /api/data + X-402-Payment header → Server
Agent ← 200 OK (data) ← ServerThe payment is a USDC transfer on Base, signed by the agent's wallet and verified by the server via the 402.md API.
PROXY vs TICKET modes
402.md supports two verification modes for API skills:
PROXY mode
The agent's request is forwarded through 402.md's infrastructure:
Agent → 402.md proxy → Developer's API
Agent ← response ← 402.md proxy ← Developer's API402.md verifies the payment and forwards the request to the developer's endpoint. The developer doesn't need to run any middleware.
TICKET mode
The agent calls the developer's API directly with a signed JWT ticket:
Agent → pays 402.md → receives ticket (JWT)
Agent → Developer's API + ticket header → Developer
Agent ← response ← DeveloperThe developer verifies the ticket using the gateway middleware. This gives the developer full control over request handling and avoids routing through 402.md.
Checkout widget flow
For browser-based payments (checkout widget):
- User clicks "Pay" button on the merchant's website
- Widget creates a checkout session via
POST /api/v1/checkout/sessions - 402.md returns:
sessionId,exactAmount,walletAddress,expiresAt - Widget displays QR code, wallet address, and exact amount with countdown timer
- User sends USDC (via QR, manual transfer, or browser wallet)
- 402.md monitors the blockchain for a matching USDC transfer
- Payment confirmed → widget updates via WebSocket (with polling fallback)
- Merchant's
onSuccesscallback fires with thePaymentResult
Unique amount matching
Each session gets a unique exact amount (e.g., 49.000037 for a $49 payment). The random suffix ensures 402.md can match incoming transfers to the correct session without needing a unique wallet address per session.
API gateway flow
For API paywalls using the gateway middleware:
- Agent calls the merchant's API endpoint
- Gateway middleware returns HTTP 402 with payment requirements
- Agent signs a USDC transfer with its private key
- Agent retries the request with an
X-402-Paymentheader - Gateway verifies the payment via
POST /api/verifyon the 402.md API - If valid, the request proceeds to the merchant's handler with
req.x402populated
SKILL.md format
Every service on 402.md is described by a SKILL.md file. This file is auto-generated when you create a skill and add endpoints. It uses YAML frontmatter:
---
name: sentiment-api
base_url: https://api.402.md/api/v1/skills/sentiment-api
version: "1.0.0"
description: "Analyze sentiment of any text"
endpoints:
- path: /analyze
method: POST
price: "$0.05"
description: "Analyze text sentiment"
---
# Sentiment Analysis API
Analyze sentiment of any text
## How to pay
All endpoints below are protected by the **x402 payment protocol**...
## Endpoints
### POST /analyze — $0.05
Analyze text sentimentThe SKILL.md is regenerated whenever endpoints change. See Creating Skills for the full guide on registering skills, setting prices, and managing endpoints.
Authentication methods
| Method | Used by | Context |
|---|---|---|
| JWT ticket | Agents, browsers | Returned after payment, proves access to a skill |
| API key | Sellers | Server-side authentication for the gateway SDK |
X-402-Payment header | Agents | x402 protocol payment proof (Base64-encoded) |
| Publishable key | Browsers | Client-side checkout widget initialization |
On-chain monitoring
402.md watches USDC Transfer events on Base using viem:
- Filters transfers to the 402.md escrow wallet
- Matches transfer amounts to pending checkout sessions (using the unique amount suffix)
- Confirms payments within seconds of on-chain inclusion
- Notifies widgets via WebSocket and merchants via webhooks
Network
All payments happen on Base (Coinbase's L2 on Ethereum):
| Property | Value |
|---|---|
| USDC contract | 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 |
| Chain ID | 8453 |
| Block time | ~2 seconds |
| Gas fees | < $0.01 |
Testnet: Base Sepolia
| Property | Value |
|---|---|
| USDC contract | 0x036CbD53842c5426634e7929541eC2318f3dCF7e |
| Chain ID | 84532 |
Rate limiting
API requests are rate-limited to 60 requests per minute per wallet per skill.