x402 Protocol Reference

HTTP 402 Payment Flow

The x402 protocol enables HTTP payments using blockchain transactions. Sigwei implements this protocol to monetize API access.

Multi-Chain Support: The x402 protocol supports both EVM (Ethereum Virtual Machine) networks like Base and Solana networks, providing a unified payment interface across multiple blockchains.

Standard Payment Flow

  1. Initial Request: Client requests PayGate URL without payment

    GET /abc123
  2. Payment Required: Server responds with HTTP 402 and payment token

    HTTP/1.1 402 Payment Required
    Content-Type: application/json
    
    {
      "x402": {
        "token": "payment_token_here",
        "amount": "0.01",
        "address": "0x742d35Cc6634C0532925a3b8c2414F4e456C10F4"
      }
    }
  3. Payment Execution: Client creates blockchain payment using wallet

  4. Payment Submission: Client retries request with payment proof

    GET /abc123
    X-Payment: <base64-encoded-payment-data>
  5. Payment Verification: Server verifies signature and blockchain transaction

  6. Access Granted: Client receives proxied response from target URL

Authentication + Payment Flow

For PayGates with requireAuth: true:

  1. Initial Request: Client requests without authentication

  2. Authentication Required: Server returns 401

  3. Authenticated Request: Client includes JWT token

  4. Payment Required: Server returns 402 with payment requirements

  5. Final Request: Client includes both auth and payment

x402 Headers

Request Headers

Response Headers

Payment Data Structure

The x402 protocol supports two payment payload structures: EVM and Solana.

EVM Payment Structure

The X-Payment header contains base64-encoded JSON with EVM payment information:

Solana Payment Structure

For Solana networks, the payment structure uses a pre-signed transaction:

Key Differences from EVM:

  • No authorization object: Solana uses a pre-signed transaction instead

  • Transaction field: Contains a base64-encoded Solana transaction

  • Fee payer: The transaction includes a fee payer who co-signs the transaction

  • No EIP-712: Solana doesn't use EIP-712 typed signatures

EVM Field Descriptions

Field
Description

x402Version

Protocol version (always 1)

scheme

Payment scheme (always "exact")

network

Blockchain network (e.g., "base", "base-sepolia")

signature

EIP-712 signature of the authorization

from

Payer's wallet address

to

Payment recipient address (from PayGate configuration)

value

Payment amount in USDC microdollars (6 decimals)

validAfter

Earliest valid timestamp (usually "0")

validBefore

Latest valid timestamp

nonce

Unique 32-byte identifier to prevent replay attacks

Solana Field Descriptions

Field
Description

x402Version

Protocol version (always 1)

scheme

Payment scheme (always "exact")

network

Blockchain network (e.g., "solana-devnet", "solana-mainnet")

transaction

Base64-encoded pre-signed Solana transaction

EIP-712 Typed Data Signature

Payments use EIP-712 structured signatures for security:

Network Configurations

EVM Networks

Base Sepolia (Testnet)

  • Chain ID: 84532

  • USDC Contract: 0x036CbD53842c5426634e7929541eC2318f3dCF7e

  • RPC: https://sepolia.base.org

Base Mainnet

  • Chain ID: 8453

  • USDC Contract: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913

  • RPC: https://mainnet.base.org

Solana Networks

Solana Devnet

  • Network: solana-devnet

  • USDC Mint: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v

  • RPC: https://api.devnet.solana.com

Solana Mainnet

  • Network: solana-mainnet

  • USDC Mint: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v

  • RPC: https://api.mainnet-beta.solana.com

JavaScript Implementation Example

Error Handling

Common payment-related errors:

Client Libraries

For automatic x402 payment handling, use compatible HTTP clients:

  • JavaScript: x402-axios - Intercepts 402 responses and handles payments automatically

  • Python: x402-requests - Python requests library with x402 support

  • Go: Manual implementation or x402-compatible HTTP transport

  • cURL: Manual payment header creation for testing

These libraries abstract away the complexity of payment creation and signature handling, making x402 integration seamless for developers.

Last updated