What is x402?
x402 transforms how agents interact with paid services by embedding payments directly into HTTP requests. Instead of managing API keys or subscriptions, your agents can pay for exactly what they use, when they use it.Key Benefits
- Autonomous economic transactions - Agents can transact without human intervention
- Pay-as-you-go monetization - Only pay for the services you actually use
- Minimal setup - Often requires just one middleware line
- Instant settlement - Payments are verified on-chain in real-time
Protocol Flow
Understanding the x402 payment flow is essential for building autonomous agents:1
Initial Request
Client requests access to a protected resource.
2
Payment Required Response
Server returns HTTP 402 status with payment details including:
- Payment amount
- Recipient address
- Payment reference
3
Payment Execution
Client sends payment payload via HTTP header (e.g.,
X-PAYMENT
).The payment facilitator handles the on-chain settlement verification automatically.
4
Content Delivery
Client retries the original request, and server responds with:
- Requested content
- Payment confirmation header (e.g.,
X-PAYMENT-RESPONSE
)
Integrating x402 with XMTP Agents
Combining x402 with XMTP chat agents creates powerful autonomous economic agents. Here’s how to implement this integration:Agent Architecture
Your agent needs to handle three main scenarios:- Free requests - Standard queries that don’t require payment
- Payment-gated requests - Premium features that trigger x402 payments
- Payment failures - Graceful handling when payments fail
Basic Implementation
Here’s how your agent handles payment-gated requests:Payment Execution Function
Implement the payment logic using Coinbase’s x402 SDK:Complete Agent Example
Here’s a full implementation combining XMTP with x402:Real-World Examples
Several projects have successfully implemented x402 with XMTP agents:Stableburn x402
A live implementation showcasing:- XMTP v4 integration
- AgentKit functionality
- x402-based micro-payments (0.005 per request)
- Automatic revenue burning via on-chain token swaps
Stableburn x402
Autonomous agent that monetizes premium endpoints with micro-payments
Crossmint Worldstore Agent
An XMTP chat agent for shopping featuring:- x402 payment integration
- EIP-3009 gasless USDC transactions
- Base network integration
- Interactive content types in chat
Worldstore Agent
Shopping agent with embedded x402 payments and commerce functionality
Best Practices
Payment UX Guidelines
Always inform users about payment requirements before executing transactions. Transparency builds trust with your agent.
- Clear pricing - Display exact costs upfront
- Payment confirmation - Confirm successful payments
- Graceful failures - Handle payment errors elegantly
- Value communication - Explain what users get for their payment
Error Handling
Implement comprehensive error handling for common scenarios:Security Considerations
Never expose private keys in your code. Always use environment variables and secure key management practices.
- Key management - Use secure environment variables
- Payment limits - Set maximum payment thresholds
- Rate limiting - Prevent payment spam attacks
- Audit trails - Log all payment activities
- Network validation - Verify on-chain settlement
Resources
x402 Protocol Docs
Complete x402 protocol specification and implementation guide
x402.org
Official x402 protocol website with examples and tools
Coinbase SDK
Official Coinbase x402 SDK for JavaScript/TypeScript
XMTP Documentation
XMTP protocol documentation for messaging integration
Next Steps
Ready to build your autonomous payment agent? Here’s your roadmap:1
Set up development environment
Install the x402 SDK and XMTP client libraries in your project.
2
Implement basic agent
Start with a simple XMTP agent following our Chat Agents guide.
3
Add payment functionality
Integrate x402 payment handling using the examples above.
4
Test thoroughly
Test payment flows on testnet before deploying to production.
5
Deploy and monitor
Deploy your agent and monitor payment transactions and user interactions.