Skip to main content
x402 is a payment protocol built on the HTTP 402 Payment Required status code. It enables machine-to-machine micropayments for API access without manual billing, credit cards, or subscription management. Pay per request with USDC, and get instant API access.

How It Works

Detailed Flow

  1. Client sends a request to the ChainStream API without an API key or with an expired key.
  2. Gateway returns HTTP 402 with a message pointing to /x402/purchase.
  3. Client calls GET /x402/purchase?plan=<plan> (without payment header). The server returns HTTP 402 with the x402 payment requirements: The decoded JSON body follows the x402 v2 protocol:
  4. Client signs a USDC transfer using the @x402 SDK and retries GET /x402/purchase?plan=<plan> with the payment proof:
  5. Server verifies and settles the payment, then returns the subscription details:
    The client saves the apiKey for all future API calls.

CLI Integration

The ChainStream CLI handles x402 payments automatically via callWithAutoPayment. When any command hits a 402, the CLI guides you through plan selection and payment.

Automatic Flow

When the CLI encounters a 402 response, it:
  1. Fetches available plans from /x402/pricing and displays a selection table
  2. Prompts you to choose a plan
  3. Asks for payment method: x402 (USDC on Base/Solana) or MPP (USDC.e on Tempo)
  4. If x402: signs and sends payment via @x402/fetch, saves the returned API Key to config
  5. If MPP: prints the tempo request command for manual purchase
  6. Retries the original command with the new API Key
If you only have an API Key (no wallet), the CLI skips x402 and prints MPP instructions instead.

Wallet Setup

The CLI needs a funded wallet for x402 payments:

Manual Integration

For custom integrations, you can implement the x402 flow using the @x402 package family.

Dependencies

The simplest integration — wrap the standard fetch with x402 support:

Manual Flow (Advanced)

For full control over the payment flow:

Supported Chains for Payment

Zero Gas Fee

ChainStream operates its own x402 facilitator that submits the on-chain payment transaction on behalf of the agent. This means:
  • No gas fee — the facilitator covers all gas costs (Base ETH / Solana SOL)
  • Agent wallet only needs USDC — no need to hold native tokens for gas
  • The agent signs a USDC transfer authorization; the facilitator broadcasts and pays for execution
This removes the biggest friction point for AI agents: acquiring and managing native gas tokens across multiple chains.

Security Considerations

  • Payment caps: Always set a maxAmount when using @x402/fetch to prevent unexpected charges.
  • Verification: The facilitator verifies the signed payment on-chain before settling. Invalid signatures are rejected.
  • Idempotency: If a payment is settled but the response fails (network error), the same Payment-Signature can be resubmitted. Payments are only consumed once.
  • Compliance: Payer addresses are screened before settlement. Sanctioned addresses are rejected.