Validation of Requests
This guide explains how AI agents validate incoming requests and settle payments using the Nevermined Facilitator. Agent builders use these methods to verify subscriber access and burn credits.Overview
The validation flow consists of:- Extract Token: Read X402 access token from request headers
- Build Payment Required: Create payment requirement specification
- Verify Permissions: Check if subscriber has valid access
- Execute Task: Process the agent request
- Settle Permissions: Burn credits after successful execution
- Return Response: Send result or 402 error to subscriber
Receiving Requests
Extract the X402 access token from request headers. The X402 v2 spec defines thePAYMENT-SIGNATURE header:
Build Payment Required
Use thebuildPaymentRequired helper to create the payment specification:
Verify Permissions
Before executing the request, verify the subscriber has valid access:Settle Permissions
After successfully processing the request, burn the credits:Return 402 Payment Required
When payment is required, return HTTP 402 with thePAYMENT-REQUIRED header:
Complete Validation Example
Dynamic Credit Burning
For agents with variable credit costs, calculate credits based on the request:Settle Options
ThesettlePermissions method accepts additional options:
Best Practices
- Always Verify First: Call
verifyPermissionsbefore executing tasks - Settle After Success: Only burn credits after successful task completion
- Handle Errors: Wrap verification/settlement in try-catch blocks
- Return 402 Properly: Include
PAYMENT-REQUIREDheader with payment details - Log Transactions: Record transaction hashes for audit trails
- Dynamic Pricing: Calculate credits based on actual resource usage
- Token Validation: Never skip verification even if token looks valid
Related Documentation
- Querying an Agent - How subscribers generate and use tokens
- X402 Protocol - Complete X402 specification
- MCP Integration - Automatic validation in MCP servers
- A2A Integration - Automatic validation in A2A servers
Source References:
src/x402/facilitator-api.ts(buildPaymentRequired, verifyPermissions, settlePermissions)tests/e2e/test_payments_e2e.test.ts(MockAgentServer class, complete validation flow)tests/e2e/test_x402_e2e.test.ts(lines 135-150, verification examples)