Skip to main content
This guide explains how AI agents can validate incoming requests using the Nevermined Payments Python SDK.

Overview

When an agent receives a request, it needs to:
  1. Extract the x402 access token from the request
  2. Verify the subscriber has valid permissions
  3. Optionally settle (burn) credits after processing
The SDK provides the Facilitator API for these operations.

Receiving Requests

Agents receive requests with the x402 access token in the payment-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

The verify_permissions method returns a VerifyResponse:

Settlement Response

The settle_permissions method returns a SettleResponse:

Best Practices

  1. Always verify before processing: Don’t process expensive operations without verification
  2. Handle errors gracefully: Return 402 Payment Required with helpful error messages
  3. Settle after completion: Only burn credits after successfully completing the request
  4. Log transactions: Keep records of verification and settlement for debugging
  5. 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