1. Set up the x402 signer
The signer needs apublicClient 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 a402 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: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
- Batch Settlement — deposit policy, recovery, and refunds
- First Agent Call — the simpler Compose Key path
- Error Handling — handling payment failures