How an AI agent pays for your API: x402 with no accounts and no API keys
An AI agent cannot sign up for your API. It has no email address. It will not click a confirmation link. It cannot enter a credit card, and it definitely cannot wait for an account to be approved. Every SaaS billing mod
An AI agent cannot sign up for your API.
It has no email address. It will not click a confirmation link. It cannot enter a credit card, and it definitely cannot wait for an account to be approved. Every SaaS billing model assumes a human on the other side of the funnel, and an agent is not one.
That is the gap x402 fills, and it changes how you design the API itself.
The idea in one sentence
Instead of proving who you are, the caller proves they paid — the payment is the credential.
What happens on the wire
Your endpoint is called with no headers at all. Instead of 401 Unauthorized, you return 402 Payment Required with a machine-readable description of what it costs:
{
"x402Version": 1,
"accepts": [{
"scheme": "exact",
"network": "base",
"asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
"payTo": "0xYourAddress",
"maxAmountRequired": "50000"
}]
}
The agent signs a USDC transfer for exactly that amount, puts it in an X-PAYMENT header, and calls again. The server verifies, settles, and returns the data.
No session. No cookie. No account. Two requests and the agent is done.
The client is about twenty-five lines
This is the whole thing, using Coinbase's x402-fetch helper:
import { wrapFetchWithPayment } from "x402-fetch";
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";
const wallet = createWalletClient({
account: privateKeyToAccount(process.env.AGENT_PRIVATE_KEY),
chain: base,
transport: http(),
});
const payFetch = wrapFetchWithPayment(fetch, wallet);
// free route first — costs nothing, proves the endpoint is alive
const free = await fetch(`${API_BASE}/api/free/token-safety/${address}`);
// paid route — 0.05 USDC, settled on Base
const paid = await payFetch(`${API_BASE}/api/security/token/${address}`);
Three design decisions this forces
1. Free routes are not a free tier — they are a handshake.
An agent should be able to verify your service exists, is up, and returns the shape it expects, before it spends. Keep at least one free route per paid route, and make it return real data, not a liveness stub.
2. Price per call, not per month.
An agent does not have a billing cycle. It decides at call time. Prices from $0.01 to $0.25 per call work, because the agent is comparing your call against the cost of doing it itself.
3. Fail closed, always.
Malformed payload, wrong chain, wrong asset, underpayment, failed simulation, RPC outage — every one of those returns 402, never partial access. An agent will retry; it will not forgive a service that leaks data on error paths.
What we ran into building this
Public facilitators are testnet-only. The free facilitator at x402.org does not settle Base mainnet. For production you either verify on-chain yourself, or run your own facilitator. We verify with eth_call simulation against public RPCs, then broadcast and wait for a receipt — no third party in the trust path.
Gas is a real onboarding cost. The paying wallet needs USDC and a little ETH on the same chain. Any agent operator hits this in the first five minutes, so put it in your docs above the code, not in a footnote.
Idempotency matters more than auth. With no accounts, a retried request is indistinguishable from a new one. Key it on the payment payload, not on a user id.
Why this matters beyond crypto
The interesting part is not the token. It is that a service can now be sold to software without a human in the loop.
An agent that needs a token risk score, a price feed, an address label, a translation, a rendering job — it can buy it at the moment it needs it, for the price of the call. No onboarding, no sales call, no invoice.
That is a new distribution channel, and most APIs are still invisible to it, because they are behind a signup form.
If you want to try it
Our own implementation is open: onchain-x402-api — free routes, paid routes, a 25-line client in examples/, and the payment verification documented line by line.
Start with one paid route and one free route. That is enough to learn whether agents will pay for what you have.
I write about on-chain security and the infrastructure around it. 61 case investigations are indexed at qanzhi111.github.io/intel.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.