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:
| Field | Type | Description |
|---|---|---|
is_valid | bool | Whether the request is authorized |
subscriber_address | str | Subscriber’s wallet address |
plan_id | str | Plan being used |
balance | int | Current credit balance |
error | str | Error message if invalid |
Settlement Response
Thesettle_permissions method returns:
| Field | Type | Description |
|---|---|---|
success | bool | Whether settlement succeeded |
credits_redeemed | int | Number of credits burned |
tx_hash | str | Blockchain transaction hash |
remaining_balance | int | 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