Skip to main content

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.
🔐 Never log incoming tokens. When you extract the payment-signature header, treat it as a bearer secret: do not echo it in error messages, debug output, or telemetry. Configure your log/trace exporters to redact payment-signature, authorization, and cookie headers by default.

Overview

The validation flow consists of:
  1. Extract Token: Read X402 access token from request headers
  2. Build Payment Required: Create payment requirement specification
  3. Verify Permissions: Check if subscriber has valid access
  4. Execute Task: Process the agent request
  5. Settle Permissions: Burn credits after successful execution
  6. Return Response: Send result or 402 error to subscriber

Receiving Requests

Extract the X402 access token from request headers. The X402 v2 spec defines the payment-signature header:

Build Payment Required

Use the buildPaymentRequired 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 the PAYMENT-REQUIRED header:

Complete Validation Example

Dynamic Credit Burning

For agents with variable credit costs, calculate credits based on the request:

Settle Options

The settlePermissions method accepts additional options:

Best Practices

  1. Always Verify First: Call verifyPermissions before executing tasks
  2. Settle After Success: Only burn credits after successful task completion
  3. Handle Errors: Wrap verification/settlement in try-catch blocks
  4. Return 402 Properly: Include PAYMENT-REQUIRED header with payment details
  5. Log Transactions: Record transaction hashes for audit trails
  6. Dynamic Pricing: Calculate credits based on actual resource usage
  7. Token Validation: Never skip verification even if token looks valid

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)