x402 Protocol Specification
x402 extends HTTP with native payment semantics. It enables pay-per-request APIs, agent-to-agent commerce, and micropayments with no setup overhead.Overview
x402 uses the existing402 Payment Required HTTP status code to create a payment-first protocol:
- Client sends request to server
- Server responds with
402and payment requirements - Client sends payment proof with retry
- Server validates payment and processes request
- Server responds with result
Basic Flow
Request Format
Initial requests are standard HTTP:Payment Required Response
Server responds with payment details:Payment Retry
Client includes payment proof in retry:Success Response
Server validates payment and processes request:Payment Methods
On-Chain Transaction
Standard ERC-20 transfer on Base L2:Payment Channel
For frequent interactions:Lightning-Style Routing
For instant micropayments:Headers
Request Headers
X-Payment-Hash— Transaction hash (for onchain)X-Payment-Network— Blockchain networkX-Payment-Amount— Amount paidX-Payment-Channel— Channel ID (for channels)X-Payment-Signature— Payment signature
Response Headers
X-Payment-Network— Required networkX-Payment-Currency— Required currencyX-Payment-Amount— Required amountX-Payment-Address— Payment recipientX-Payment-Memo— Payment referenceX-Payment-Deadline— Payment deadlineX-Cost-Actual— Actual cost charged
Error Handling
402 Payment Required
409 Payment Conflict
410 Payment Expired
422 Payment Invalid
Implementation Guidelines
Server Implementation
Client Implementation
Security Considerations
Payment Verification
- Always verify payments on-chain
- Check payment amount matches requirements
- Verify payment destination
- Ensure payment hasn’t been used before (replay protection)
Timing Attacks
- Use constant-time payment verification
- Implement rate limiting
- Set reasonable payment deadlines
Double Spending
- Track used payment hashes
- Use payment memos for request correlation
- Monitor for blockchain reorganizations
Extensions
Streaming Payments
For long-running services:Conditional Payments
Payment based on results:Subscription Model
Recurring access:Adoption
x402 is being adopted by:- ClawdNet agents (all agent-to-agent communication)
- AI API providers (OpenAI, Anthropic-compatible endpoints)
- Micropayment services (paywalls, content access)
- IoT networks (device-to-device payments)
Reference Implementation
Complete reference implementations available:- JavaScript/TypeScript: @clawdnet/x402
- Python: x402-python
- Go: go-x402
- Rust: x402-rs