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.Documentation Index
Fetch the complete documentation index at: https://docs.chainstream.io/llms.txt
Use this file to discover all available pages before exploring further.
How It Works
Detailed Flow
- Client sends a request to the ChainStream API without an API key or with an expired key.
-
Gateway returns HTTP 402 with a message pointing to
/x402/purchase. -
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:Response Header Description X-Payment-RequiredBase64-encoded JSON with payment details Payment-RequiredSame value (for x402 client compatibility) -
Client signs a USDC transfer using the
@x402SDK and retriesGET /x402/purchase?plan=<plan>with the payment proof:Request Header Description Payment-SignatureBase64-encoded signed payment payload -
Server verifies and settles the payment, then returns the subscription details:
The client saves the
apiKeyfor all future API calls.
CLI Integration
The ChainStream CLI handles x402 payments automatically viacallWithAutoPayment. 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:- Fetches available plans from
/x402/pricingand displays a selection table - Prompts you to choose a plan
- Asks for payment method: x402 (USDC on Base/Solana) or MPP (USDC.e on Tempo)
- If x402: signs and sends payment via
@x402/fetch, saves the returned API Key to config - If MPP: prints the
tempo requestcommand for manual purchase - 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
| Package | Purpose |
|---|---|
@x402/core | Protocol types, header parsing, verification logic |
@x402/evm | EVM payment execution (built on viem) |
@x402/svm | Solana payment execution (built on @solana/kit) |
@x402/fetch | Drop-in fetch wrapper with automatic 402 handling |
Using @x402/fetch (Recommended)
The simplest integration — wrap the standardfetch with x402 support:
Manual Flow (Advanced)
For full control over the payment flow:Supported Chains for Payment
| Chain | Token | Confirmation Time |
|---|---|---|
| Base | USDC | ~2 seconds |
| Solana | USDC | ~400ms |
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
Security Considerations
- Payment caps: Always set a
maxAmountwhen using@x402/fetchto 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-Signaturecan be resubmitted. Payments are only consumed once. - Compliance: Payer addresses are screened before settlement. Sanctioned addresses are rejected.

