X402 Payment Protocol
Bindu implements the x402 payment protocol following the official A2A specification, enabling agents to monetize their services through secure on-chain payments.Overview
The x402 protocol allows agents to:- Monetize services through cryptocurrency payments
- Accept payments on multiple blockchain networks
- Verify transactions using payment facilitators
- Settle payments automatically after task completion
X402 is implemented as HTTP middleware, providing a clean and standard way to handle payments without modifying your agent logic.
How It Works
Payment Flow
The x402 payment flow consists of two requests:1
First Request - Payment Discovery
Client sends a request without payment. Agent responds with
402 Payment Required and payment details.2
Second Request - Payment Submission
Client includes payment proof in
X-PAYMENT header. Middleware verifies, executes task, and settles payment.Middleware Actions
When a payment is submitted, the middleware automatically:- β
Decodes the
X-PAYMENTheader - β Parses the payment payload
- β Matches against payment requirements
- β Verifies with payment facilitator
- β Allows request to proceed
- β Executes agent task
- β Settles payment on-chain
- β
Returns
X-PAYMENT-RESPONSEheader
Configuration
Enable Payments for Your Agent
Configure payment requirements in your agent manifest:Configuration Options
| Parameter | Description | Required |
|---|---|---|
enabled | Enable payment requirement | Yes |
network | Blockchain network (e.g., base-sepolia) | Yes |
asset | Token contract address | Yes |
amount | Payment amount in smallest unit | Yes |
pay_to | Recipient wallet address | Yes |
facilitator_url | Payment verification service URL | Yes |
timeout_seconds | Payment validity timeout | No (default: 600) |
Supported Networks
Bindu supports payments on multiple blockchain networks:- Base Sepolia (Testnet) -
base-sepolia - Base Mainnet -
base - Ethereum Mainnet -
ethereum - Polygon -
polygon
For testing, we recommend using Base Sepolia testnet with test USDC tokens.
Testing Payments
Quick Start
1
Generate Test Wallet
2
Send First Request
3
Generate Signed Payment
4
Send Payment Request
Include the signed payment in the
X-PAYMENT header and retry the request.Testing Scenarios
Test different payment scenarios:- β Successful payment flow - Valid payment with correct signature
- β Invalid signature - Payment with incorrect signature
- β Settlement failure - Payment verification succeeds but settlement fails
- β Agent without payment - Request to agent that doesnβt require payment
Client Integration
JavaScript/TypeScript
Python
Architecture
Middleware Design
X402 is implemented as HTTP middleware, providing clean separation of concerns:- β Clean separation - Payment logic separate from agent logic
- β Standard protocol - Follows official x402 specification
- β Easy integration - No changes to existing agent code
- β Secure - Cryptographic verification of all payments
Implementation Phases
The x402 implementation was completed in three phases:Phase 1: Payment Detection β
- Return
402 Payment Requiredwith payment details - Include payment requirements in task metadata
- Support payment configuration in agent manifest
Phase 2: Payment Verification & Settlement β
- Verify payment signatures and amounts
- Integrate with payment facilitators
- Settle payments on-chain after task completion
- Handle payment errors gracefully
Phase 3: Optimization & Polish β
- Performance optimization
- Enhanced error messages
- Comprehensive testing
- Documentation and examples
Security Considerations
Best Practices
- Use testnet first - Test thoroughly on Base Sepolia before mainnet
- Secure private keys - Use environment variables or key management services
- Validate amounts - Always verify payment amounts match requirements
- Monitor transactions - Track payment settlements on-chain
- Handle errors - Implement proper error handling for payment failures
Troubleshooting
Common Issues
Payment verification failed
Payment verification failed
Cause: Invalid signature or payment parameters donβt match requirements.Solution: Verify that the payment signature is correct and all parameters (network, asset, amount) match the agentβs requirements.
Payment settlement failed
Payment settlement failed
Cause: On-chain transaction failed or facilitator error.Solution: Check facilitator logs, verify wallet has sufficient gas, and ensure the payment hasnβt already been settled.
Payment required error persists
Payment required error persists
Cause: Missing or invalid
X-PAYMENT header.Solution: Ensure the X-PAYMENT header is included in the second request with a valid signed payment payload.Resources
- X402 Specification - Official protocol documentation
- Implementation Guide - Detailed implementation details
- Testing Guide - Complete testing instructions
- Phase Documentation - Implementation phases and progress