Let an AI agent pay for APIs with USDC
Give an AI agent its own wallet and let it pay for API calls in USDC on Base, one call at a time, with hard limits and no API keys.
5 min read · updated Nov 26, 2026
API keys and monthly plans were built for people. An agent that discovers a useful API halfway through a task can't sign up for it. x402 fixes that: the API answers with HTTP 402 and a price, the agent signs a USDC transfer for exactly that amount, and calls again. No account, no key, no invoice.
1. Give the agent a wallet
Create a new wallet just for the agent and send it a few dollars of USDC on Base. It needs no ETH: x402 payments are signed transfers (EIP-3009), and the facilitator pays the gas. Keep the key in an environment variable, never in the prompt.
npm install fourohtwo
import { createPayer } from "fourohtwo";
const pay = createPayer({
privateKey: process.env.AGENT_KEY!,
networks: ["base"], // pay in USDC on Base only
maxPerCallUsd: "0.05", // refuse anything pricier
maxDailyUsd: "2", // and stop for the day after $2
});2. Call an API like fetch
pay works like fetch. When the API asks for payment it checks your limits, signs, and retries. On Fourohtwo's own APIs the first 5 calls of a new wallet are free, and a call that fails is never charged.
const res = await pay("https://fourohtwo.xyz/x/gas");
console.log(res.status, await res.json());
console.log(res.headers.get("x-fourohtwo-charged")); // "true" once paid, "false" for free or failed calls3. Find APIs it hasn't seen
The same payer pays any x402 API on Base, not only Fourohtwo's. Search the checked directory first: it shows the price, the networks and whether the API answered its last daily check.
const found = await (await fetch("https://fourohtwo.xyz/api/search?q=weather&network=base")).json();
const pick = found.x402.find((a: { alive: boolean }) => a.alive);
const forecast = await pay(pick.url);4. Put the limits outside the code too
Link the agent wallet at fourohtwo.xyz/agents to set a daily limit, a per-call limit, a per-API limit and an allowlist on the server. The SDK reads them before paying, so a bug or a confused model can't spend past them. Add a webhook and you'll hear when the agent reaches 80% of its daily limit.
APIs in this guide
Try it freeQuestions
- Does the agent need ETH for gas?
- No. It signs a USDC transfer authorization and the facilitator submits it on-chain. Only USDC leaves the wallet.
- What if the API fails?
- On Fourohtwo you're charged only after the API answers with a success. For other x402 APIs, check the directory's uptime before paying.
- Can it pay in something other than USDC on Base?
- Yes: USDG on Robinhood Chain. Leave networks unset and the payer uses whichever the API accepts, trying Robinhood Chain first.
- Is there a Python version?
- pip install fourohtwo gives the same payer for Python, with the same limits and free calls.
More guides
- Give your AI agent live stock prices
- Check a token for rug risk from an AI agent
- Pay for x402 APIs from Python
- Let a LangChain agent find and pay for APIs
- x402 on Base vs Robinhood Chain: which should your agent pay on?
- Track whale trades and DEX volume on Robinhood Chain
- Add paid APIs to Claude, Cursor or Codex with MCP
