Overview
When an agent receives a request, it needs to:- Extract the x402 access token from the request
- Verify the subscriber has valid permissions
- Optionally settle (burn) credits after processing
Receiving Requests
Agents receive requests with the x402 access token in thepayment-signature header (per x402 v2 HTTP transport spec):
Validating Requests with Facilitator
Build Payment Required Object
First, build the payment requirement specification:Verify Permissions
Check if the subscriber has valid permissions without burning credits:Settle Permissions
After successfully processing a request, burn the credits:Complete Example: Flask Agent
FastAPI Example with Manual Validation
Using x402 FastAPI Middleware
For FastAPI applications, use the built-in x402 middleware:Verification Response
Theverify_permissions method returns a VerifyResponse:
Settlement Response
Thesettle_permissions method returns a SettleResponse:
Best Practices
- Always verify before processing: Don’t process expensive operations without verification
- Handle errors gracefully: Return 402 Payment Required with helpful error messages
- Settle after completion: Only burn credits after successfully completing the request
- Log transactions: Keep records of verification and settlement for debugging
- Use middleware for consistency: Apply validation uniformly across all endpoints
Next Steps
MCP Integration
Build MCP servers with payment validation
x402 Protocol
Deep dive into x402 protocol