Build your first x402 buyer in 20 minutes
Your agent can call an API. It still can't pay for one.
That's the awkward gap in agent tooling right now: every paid API assumes a human with a card, a checkout page, and an API key. An agent has none of those. It has an HTTP client and a wallet address.
x402 closes that gap by putting payment inside the HTTP request. The server answers 402 Payment Required, tells you the price and where to send dollars, you pay in USDC, you retry the same request with a signature header. No signup, no key, no dashboard.
Here's the whole thing, end to end, in about twenty minutes.
Step 1 — Ask, and get told the price
Call any paid endpoint with no payment. This is the real response from https://api.xynaptic.io/v1/crypto-price:
curl -i https://api.xynaptic.io/v1/crypto-price { "x402Version": 2, "error": "Payment required", "accepts": [ { "scheme": "exact", "network": "eip155:8453", "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", "amount": "10000", "payTo": "0x4ff893dF9aB8603d5460FbbAeE0D9B8c0fa4D5e9", "maxTimeoutSeconds": 60, "extra": { "name": "USD Coin", "version": "2" } }, { "scheme": "exact", "network": "eip155:137", "..." : "..." } ] } Read that carefully, because everything else is just plumbing:
-
network: eip155:8453— Base mainnet (the second entry is Polygon). -
asset— the USDC contract, not a made-up token. -
amount: "10000"— USDC has 6 decimals, so this is $0.01. -
payTo— the recipient. -
scheme: exact— the price is the price. No bidding, no negotiation.
The endpoint just told your agent the price, in machine-readable form, before any signup. That alone is worth the round trip.
Step 2 — Pay it with a signed authorization
x402 v2 uses EIP-3009 (TransferWithAuthorization): instead of broadcasting a transfer and waiting, you sign a message authorising the transfer, and the server (or its facilitator) settles it. The signature is the payment proof.
Don't hand-roll this on day one. Use the reference client:
npm i x402-fetch viem import { wrapFetchWithPayment } from "x402-fetch"; import { privateKeyToAccount } from "viem/accounts"; const account = privateKeyToAccount(process.env.AGENT_KEY); const fetchWithPay = wrapFetchWithPayment(fetch, account); const res = await fetchWithPay("https://api.xynaptic.io/v1/crypto-price"); const data = await res.json(); console.log(data); That's the twenty-minute version. wrapFetchWithPayment does three things you'd otherwise write yourself:
- calls the URL, sees the
402; - picks an
acceptsentry it can pay, signs the EIP-3009 authorization, encodes it into the x402 payment payload; - retries the same request with that payload in the
PAYMENT-SIGNATUREheader.
The server verifies the signature, settles on-chain, and returns the 200 you originally asked for.
The part people get wrong
Look at maxAmountRequired / amount before you sign. A malicious or misconfigured endpoint can quote anything. Your buyer should carry a price ceiling and refuse anything above it — one line in your wrapper, and it's the difference between "agent with a wallet" and "agent that can be drained".
Same for networks: if your treasury is on Base, filter accepts to eip155:8453 and ignore the rest. Don't let the remote server choose your chain.
Step 3 — Verify you got what you paid for
Payment is not the point; the data is. A healthy x402 service returns exactly the payload its 402 promised, and it should be checkable without paying. That's why a catalogue-wide audit matters: every endpoint here answers 402 with a challenge whose price matches the published one, or it gets flagged.
So keep two habits:
- Check the challenge before paying. Price, network, asset, recipient.
- Check the response after paying. If a service takes your money and returns a shape it never advertised, you want that in a log, not in a support ticket.
What it actually costs
Per-call pricing in USDC is unglamorous and that's the appeal: no seats, no minimums, no annual commitment for something your agent calls eleven times.
On Xynaptic the range runs from $0.01 (crypto prices, route probes) to $0.50–$2 (repository security audits), across 220 endpoints. An agent doing 200 calls a month at $0.02 spends $4. Compare that to the pricing page of any "agent data platform" and the difference isn't the model, it's the billing unit.
If your call volume is real, subscriptions exist too (POST /v1/sub-buy returns a sub_... key that works as a Bearer token), and the quota check is free: POST /v1/sub-status. But start per-call. You can't reason about your costs until you've seen them itemised.
The mental model, in one paragraph
A paid API is now: a URL that answers 402 with a price, a signature that settles it, and a response you can verify. Your agent needs a key pair and a spending ceiling. It does not need a signup flow, an invoice, or a human in the loop for a one-cent call.
Twenty minutes is enough to build the buyer. The interesting work starts after that: budgets per task, refusing overpriced routes, logging what each agent spent and what it got for it.
I run Xynaptic, a catalogue of 220 paid endpoints for agents — the 402 above is live, you can call it right now. The buyer code in this post is the one I use myself; if something in it doesn't work against your setup, tell me and I'll fix the tutorial.