Book a free call

How x402 Lets AI Agents Pay for APIs

x402 is a payment protocol that lets an AI agent pay for an API call with a stablecoin, right inside the HTTP request. Here is how it works and when to use it.

Hertiv engineering teamReviewed by a senior engineer
6 min read
How x402 Lets AI Agents Pay for APIs
On this page

Short answer

x402 is a payment protocol that lets an AI agent pay for an API call with a stablecoin. When the agent requests a paid endpoint, the server answers with a 402 status and a payment request, the agent signs a small crypto payment, and the server then returns the data. It removes the need for API keys, accounts and monthly billing for machine-to-machine use.

What is the x402 protocol?

The x402 protocol is a way for software to pay for web requests using cryptocurrency, built on the HTTP 402 Payment Required status code. A server that charges for an endpoint answers a request with a 402 response that says how much to pay, in which token, and to which wallet address. The client then sends a signed payment and repeats the request, and the server returns the result once the payment clears.

It was designed for AI agents because agents do not have credit cards, email addresses or accounts. An agent that needs a weather forecast, a stock price or a search result can pay a few cents for that single call, in stablecoins, without signing up for anything. The protocol keeps the payment inside the normal request and response cycle, so no separate checkout page or dashboard is involved.

For teams building agents, AI agents for operations covers the wider question of how agents connect to real systems and stay within their limits.

How does an x402 payment work, step by step?

An x402 payment follows a short sequence that any HTTP client can run. First, the agent sends a normal request to a paid endpoint. Second, the server returns a 402 response with the payment details: the amount, the token, the recipient wallet and a deadline. Third, the agent checks the price against its budget, signs the payment with its wallet key, and sends the signed payment in a header on a second request.

  1. The agent requests a paid endpoint and receives a 402 response with the price and recipient.
  2. The agent checks the price against its spending limit and the value of the answer it needs.
  3. The agent signs the payment with its wallet and repeats the request with the payment attached.
  4. The server verifies the payment on the blockchain and returns the data.
  5. The agent logs the payment, the price and the result for later review.

The server does not need to trust the agent, and the agent does not need to trust the server with its funds. The payment is only released when the signed transaction is valid, and the server only hands over the data after it sees the payment.

Why do AI agents need a payment protocol?

AI agents hit a practical wall with paid APIs: most APIs expect a human to create an account, enter card details and manage an API key. An agent running on its own cannot do that, and a shared API key is hard to control, since anyone with the key can spend without limit. x402 gives each agent its own wallet, so spending is tied to a key the team controls and can revoke.

Pay-per-request pricing also fits how agents work better than monthly plans. An agent may call an API ten times one day and a thousand times the next, and a flat plan either wastes money or blocks the work. Paying a few cents per call matches cost to use, and the agent can stop spending the moment its budget runs out.

Approach Setup needed Cost model Control
x402 payment A wallet with stablecoins Per request Per-agent budget, revocable key
API key Account, card, key Monthly plan or usage tier Shared key, hard to limit
Free tier Account and key Free up to a limit Rate limits, no payment path
Invoice billing Contract, account Monthly invoice Manual, slow to set up

Payment rails for agents are still early, and the token and chain support varies by provider. Stablecoin payments are the common case, and stablecoin payments for business explains how those payments are settled in practice.

What does an agent need before it can pay with x402?

An agent needs four things before it can make its first x402 payment. It needs a wallet funded with the token the server accepts, usually a stablecoin on a specific chain. It also needs a spending limit set by the team, so one bad loop cannot drain the wallet.

The other two are the server's payment details, which arrive in the 402 response, so nothing has to be configured in advance, and logging, so every payment is recorded with the request that caused it.

The wallet should hold only what the agent is expected to spend in a short period, and it should be topped up from a separate treasury wallet. That way a compromised agent key costs at most the balance in the spending wallet. Teams that give agents payment ability also set per-call price caps, daily caps and an allowlist of endpoints the agent may pay for.

Example (hypothetical): an agent that pays for weather data

Example (hypothetical): a delivery-planning agent checks the weather forecast for 40 routes each morning. The forecast API charges $0.02 per call through x402. On a normal day the agent makes 40 calls, which costs 40 x $0.02 = $0.80. If rain is forecast and the agent re-checks 20 routes in the afternoon, that adds 20 x $0.02 = $0.40, for a daily total of $1.20.

Over a 22-day working month that is 22 x $1.20 = $26.40, which the team can see in the payment log next to each route it planned. If the agent's daily cap is $2.00, it can never spend more than $44.00 in a month even if a bug makes it retry.

The arithmetic is simple, and that is the point: the team can predict the cost from the number of calls, and the agent can check the price before every single one.

What are the limits of x402 today?

x402 is new, and its limits matter for anyone planning around it. Few public APIs accept it yet, so most agents will still need API keys for the services they use today. The chains and tokens supported differ between providers, so an agent funded on one chain may not be able to pay a server that wants another.

Refunds are also awkward: once a payment is signed and the data is returned, there is no built-in way to take it back if the data was wrong.

Price discovery is another gap. The agent learns the price only when it receives the 402 response, so a server can raise its price and the agent has to decide on the spot whether to pay. A price cap in the agent's policy handles that, but it means the agent may fail a task rather than pay an unexpected price. For now, x402 works best for low-value, repeatable calls where a single failure is cheap.

Common mistakes with x402 and agent payments

The most common mistakes come from treating an agent wallet like a company card. Teams fund a wallet with a large balance, share one wallet across many agents, or skip logging because the amounts are small. Each of these turns a small bug into a real loss, because an agent in a retry loop can spend hundreds of times before anyone notices.

  • Funding an agent wallet with a month of spend instead of a day or two
  • Sharing one wallet across agents, so no one can tell who spent what
  • No per-call price cap, so a price rise is paid without a second thought
  • No payment log, so cost overruns are found only on the monthly statement
  • Letting agents pay for endpoints that are not on an allowlist
  • Skipping a kill switch that stops all payments when spend spikes

Payment ability is one of the strongest permissions an agent can have. Treat it like write access to a production database: scoped, capped, logged and easy to switch off. Crypto payment gateway development covers the server side, including how a business accepts crypto payments in the first place.

Key takeaways

  • x402 lets an agent pay per API call with a stablecoin, using the HTTP 402 status.
  • The server states the price in its 402 response; the agent signs and resends the request.
  • Per-request payment fits agents better than monthly plans and shared API keys.
  • Fund agent wallets with a day or two of spend, with per-call caps and logging.
  • Few APIs accept x402 yet, and refunds and price changes are still awkward.

Frequently asked questions

What is x402 in simple terms?

x402 is a protocol that lets software pay for a web request with cryptocurrency. The server answers with a 402 status that states the price, the client signs a payment, and the server returns the data once the payment clears.

Which token do x402 payments use?

Most x402 payments today use stablecoins, since their value stays close to a dollar and the price is easy to check. The exact token and chain depend on the server, and the 402 response states both.

Does an agent need an account to use x402?

No. The agent needs a funded wallet and the server's payment details, which arrive in the 402 response. There is no signup, no API key and no monthly plan to manage.

Is x402 the same as an API key?

No. An API key proves who you are and usually bills a monthly plan. x402 proves payment for one request at a time, so cost follows use and no account is needed.

What happens if the agent pays and the data is wrong?

There is no built-in refund in x402. The payment is final once signed, so teams should cap prices per call, log every payment, and only use it for low-value calls where a bad answer is cheap.

Can x402 replace all API billing?

Not yet. Few public APIs accept it, and chains, tokens and refund handling vary between providers. It works best for low-value, repeatable machine-to-machine calls while the ecosystem grows, and most teams will keep API keys for the services that do not accept it.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: AI and agent systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building an AI agent that needs to pay for the data it uses? Talk to Hertiv.

A senior engineer replies within one business day. Or see our AI agents for operations service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call