X402 Protocol
The X402 protocol is a payment-aware HTTP extension that enables AI agents to require and verify payments for API access. This guide provides a complete overview of X402 implementation in the Nevermined Payments Library.Overview of X402
X402 is an HTTP-based protocol for payment-protected resources:- 402 Payment Required: HTTP status code for payment requests
- PAYMENT-SIGNATURE: Header containing payment credentials (X402 v2)
- PAYMENT-REQUIRED: Header with payment requirements in 402 responses
- PAYMENT-RESPONSE: Header with payment settlement details in success responses
- Cryptographic Signatures: ERC-4337 account abstraction for secure payments
X402 Version 2 Specification
The Nevermined Payments Library implements X402 v2, which uses:PAYMENT-SIGNATUREheader for access tokens (replaces Authorization)PAYMENT-REQUIREDheader for payment requirements (replaces custom formats)PAYMENT-RESPONSEheader for settlement receipts- Structured payment credentials with cryptographic signatures
Generate X402 Access Tokens
Subscribers generate access tokens to query agents:Generate Tokens via Nevermined App
Users can also generate X402 access tokens through the Nevermined App:- Visit nevermined.app/permissions/agent-permissions
- Select the plan you’ve purchased
- Configure token parameters (agent, expiration, limits)
- Generate and copy the X402 access token
- Use the token in API requests
X402 Access Token Structure
The access token is a JWT containing an X402 v2 payment credential:Token Components
- x402Version: Protocol version (2 for current spec)
- accepted: Payment method specification
- scheme:
nvm:erc4337(Nevermined ERC-4337 account abstraction) - network: Blockchain network (e.g.,
eip155:84532for Base Sepolia) - planId: The payment plan being used
- extra: Additional metadata (version, agentId, etc.)
- scheme:
- payload: Payment authorization
- signature: Cryptographic proof of payment authorization
- authorization: Subscriber identity and session keys
- extensions: Optional protocol extensions
Verify X402 Permissions
Agents verify tokens before executing requests:Verification Response
Settle X402 Permissions
After successful execution, burn credits:Settlement Response
HTTP Headers (X402 v2)
PAYMENT-SIGNATURE Header
Subscribers include this header in requests:PAYMENT-REQUIRED Header (402 Response)
Agents return this header when payment is required:PAYMENT-RESPONSE Header (Success)
Agents include this header in successful responses:Complete X402 Flow
Subscriber Side
Agent Side
buildPaymentRequired Helper
Simplifies creating X402PaymentRequired objects:Best Practices
- Always Verify Before Execute: Never skip token verification
- Settle After Success: Only burn credits after successful execution
- Use X402 v2 Headers: Prefer
PAYMENT-SIGNATUREover Authorization - Return 402 Properly: Include
PAYMENT-REQUIREDheader with details - Log Transactions: Record settlement transaction hashes
- Handle Errors: Provide clear error messages in 402 responses
- Token Reuse: Subscribers can reuse tokens for multiple requests
Related Documentation
- Querying an Agent - How subscribers use X402 tokens
- Validation of Requests - How agents verify and settle
- MCP Integration - Automatic X402 handling in MCP
- A2A Integration - Automatic X402 handling in A2A
Source References:
src/x402/token.ts(getX402AccessToken)src/x402/facilitator-api.ts(verifyPermissions, settlePermissions, buildPaymentRequired)tests/e2e/test_x402_e2e.test.ts(complete X402 flow)