How AI agents pay for tools with x402
An x402-enabled agent needs three things: a wallet with some USDC, a client that knows how to answer a 402 response, and a way to discover which paid tools exist.
1. Give the agent a wallet
Any EVM or Solana wallet works, but for agents the usual choices are:
- A server wallet from a wallet provider (for example Coinbase CDP Wallets or similar), so the private key never sits in your agent's code.
- A dedicated hot wallet holding a small balance. Treat its balance as the agent's spending limit.
Fund it with a few dollars of USDC on Base. Most x402 tools cost between $0.001 and $0.10 per call.
2. Wrap your HTTP client
The x402 client libraries wrap fetch or axios. When a request comes back with 402, they read the payment requirements, sign the payment with your wallet and retry. In TypeScript the pattern looks like this:
import { wrapFetchWithPayment } from "x402-fetch";
import { privateKeyToAccount } from "viem/accounts";
const account = privateKeyToAccount(process.env.AGENT_PRIVATE_KEY as `0x${string}`);
const paidFetch = wrapFetchWithPayment(fetch, account);
const res = await paidFetch("https://some-x402-api.example/endpoint");
console.log(await res.json());
Python, Go and Rust clients follow the same idea. Check the package docs for the current names, since the official SDK moved to scoped @x402/* packages in v2.
Always set a maximum price. The libraries let you cap the amount a single request may cost, so a misbehaving server cannot drain the wallet.
3. Let the agent discover tools
Hard-coding endpoints does not scale. x402-tools.com exposes the whole directory in agent-friendly formats:
- JSON API:
GET https://x402-tools.com/api/tools?q=weatherreturns matching tools with price, network, method and input schema. See the API docs. - MCP server: add
https://x402-tools.com/mcpto Claude, Cursor or any MCP client and the agent getssearch_x402_toolsandget_x402_tooltools. - llms.txt:
https://x402-tools.com/llms.txtis a plain-text summary for models that browse.
A good agent loop is: search the directory for the capability it needs, compare price and recent usage, call the tool with the paid client, and fall back to the next candidate if it fails.
Safety checklist
- Use a separate wallet per agent, with only the balance you are willing to lose.
- Cap per-request and per-day spend.
- Prefer tools with real recent usage (shown as payers in the last 30 days in the directory) and a passing health check.
- Log every payment receipt so you can audit what the agent bought.