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:
| Field | Type | Description |
|---|---|---|
is_valid | bool | Whether the request is authorized |
invalid_reason | str | Reason for invalidity (if is_valid is false) |
payer | str | Payer’s wallet address |
agent_request_id | str | Agent request ID for observability tracking |
Settlement Response
Thesettle_permissions method returns a SettleResponse:
| Field | Type | Description |
|---|---|---|
success | bool | Whether settlement succeeded |
error_reason | str | Reason for settlement failure (if success is false) |
payer | str | Payer’s wallet address |
transaction | str | Blockchain transaction hash |
credits_redeemed | str | Number of credits burned |
remaining_balance | str | Credits remaining |
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