THE AGENT ALMANAC
Vol. I · No. 1 · Free public reference for the agent internet
Guides

How to Make Your AI Agent Pay for Things (x402)

Give your agent a wallet and let it buy data, tools, and APIs on its own — safely, in a few lines.


If you're building an AI agent, you can let it pay for what it uses — a premium API, a dataset, a tool — without you in the loop, using the x402 protocol. The agent hits a paid endpoint, pays in stablecoins, and gets the data. Here's how, and how to do it without setting money on fire.

How it works (30 seconds)

  1. Your agent requests a paid resource.
  2. The server replies HTTP 402 with the price, token, chain, and payTo address.
  3. Your x402 client automatically signs a stablecoin payment and retries with an X-PAYMENT header.
  4. The server verifies and returns the data. Crucially, the payment only settles if the server actually returns the data — if it errors, nothing is deducted.

No API keys to manage, no subscriptions, no human clicking "approve."

Step 1 — Fund a dedicated agent wallet ⚠️

This is the part to get right. Create a brand-new, throwaway wallet for the agent and fund it with a small amount of USDC (Base is the most common chain).

Step 2 — Install the client

x402 clients wrap your normal HTTP calls and handle the 402 automatically. Pick your HTTP library:

npm install x402-fetch      # for fetch
# or
npm install x402-axios      # for axios

Step 3 — Wrap your requests

import { wrapFetchWithPayment } from "x402-fetch";
import { privateKeyToAccount } from "viem/accounts";

// dedicated, low-balance wallet — see Step 1
const account = privateKeyToAccount(process.env.AGENT_WALLET_KEY);

const fetchWithPay = wrapFetchWithPayment(fetch, account);

// this call auto-pays the 402 and returns the data
const res = await fetchWithPay("https://some-service.com/premium");
const data = await res.json();

That's the whole thing — every call through fetchWithPay handles payment transparently. (APIs evolve; confirm the exact current usage in Coinbase's Quickstart for Buyers.)

Step 4 — Set spending limits

Give the agent guardrails so a bug or a bad endpoint can't drain the float:

Most x402 clients and agent wallets support spend policies — use them.

Step 5 — Point it at something worth buying

Your agent needs to know what to pay for. The Agent Almanac catalogs the services agents can pay — and you can query it from inside your agent via our MCP server (tools like search_services and get_economy), or browse the live scoreboard to see which providers are actually real (paid by many wallets) versus dead listings.

The one safety rule

Settlement only happens on a successful response, but that doesn't protect you from choosing to pay a bad service repeatedly. So: throwaway wallet, small balance, hard spend caps. With those three, letting your agent transact is genuinely low-risk.


Next: the other side — How to accept agent payments on your API (x402). Or see who's actually earning on the leaderboard.

Once you're live

Watch real settlement on the Economy scoreboard, see who's earning on the leaderboard, or query it all from your agent via the MCP server.

← All guides The live numbers →