Skip to main content
This guide shows the raw x402 payment path — no Compose Key, no bearer token. You sign each request with an EVM wallet using batch-settlement, the lowest-cost scheme for repeated calls. You deposit USDC into an onchain escrow channel once, then each request signs an off-chain voucher the server verifies instantly.

1. Set up the x402 signer

The signer needs a publicClient with readContract so it can recover channel state from onchain. Without an RPC URL, you’ll hit stale-voucher errors after a restart.

2. Initialize the SDK

Pass your wallet address and chain. The SDK uses the x402 signer whenever no Compose Key is present.

3. Stream an agent

Send a message like normal. Behind the scenes, the first request gets a 402 Payment Required response with payment requirements. The SDK signs a batch-settlement voucher, retries the request with the payment header, and the server verifies it instantly — no onchain transaction per call.

4. Parse events

5. Read the receipt

After the stream ends, read the settlement receipt from the final result. It includes the amount charged, the fee breakdown, and token usage per model.

How the 402 challenge works

The SDK handles this automatically, but it helps to know the flow:
If the channel needs funding, the SDK deposits USDC onchain first (5× the per-request max by default), then continues.

Refund unused balance

When you’re done, refund the remaining channel balance back to your wallet:

When to use this over a Compose Key

Use raw x402 with batch-settlement for agent-to-agent automation, server scripts, and any scenario where no human is in the loop. Use a Compose Key for human-facing apps where a bearer token is simpler. See Payments Overview for the full comparison.

Next