Pay-per-call crypto data for AI agents with x402: a working example
Description and use
AI agents can request crypto data with per‑call pricing via x402, which handles USDC payments automatically and returns data without signup or API keys.
Problem
Crypto data APIs require accounts, keys, and monthly plans, which is inconvenient for AI agents that need a single price per call.
Outcome
Agents can pay per data call in USDC, avoiding monthly plans and key management, with a simple JavaScript wrapper.
Workflow
- 1. Call the endpoint and receive HTTP 402 with price details.
- 2. Use x402-fetch to sign an EIP‑3009 USDC authorization and retry with X‑PAYMENT header.
- 3. Receive the paid response and optional receipt.
- 4. Wrap the fetch call as a tool for the agent.
- 5. Optionally set a spending cap in the agent loop.
Evidence
Most crypto data APIs want an account, an API key and a monthly plan. That's awkward when the caller is an AI agent that needs one price, once.
x402 fixes this by reusing the HTTP 402 Payment Required status: the server answers with a price, the client pays in USDC and retries, and the data comes back. No signup, no key.
The wallet needs a little USDC on Base. It doesn't need ETH: the facilitator submits the transfer.
- Record date: 2026-10-21T08:41:04.920Z