Build an AI Agent That Can Pay
Agents that can only talk are demos. Agents that can pay are businesses. This guide wires a real payment rail into your agent — a USDC wallet on Base, the x402 payment flow, a spend cap — and ends with a free live check you can run on this page, no wallet connection, no money moved.
Step 1 — Give the agent its own wallet
Generate a dedicated keypair for the agent and fund it with USDC on Base L2. Keep the agent's money separate from yours so a prompt injection can never reach your main funds. The wallet needs no ETH: x402 payments settle through transferWithAuthorization (EIP-3009), and the payer signs a message rather than a gas-bearing transaction.
# example: fund the agent wallet with USDC on Base
# any USDC transfer works — the agent just needs a balance
agent_wallet = "0xYourAgentWallet"
network = "eip155:8453" # Base L2
Step 2 — Make the call, read the 402
x402 is HTTP-native pricing. Your agent calls an endpoint the normal way; if it costs money, the server answers 402 Payment Required with everything needed to pay: price, payee, chain, and a facilitator URL that verifies and settles.
GET https://agentpaystore.com/forge/api/chat
HTTP/402 Payment Required
{
"x402Version": 1,
"maxAmountRequired": "100000", // 0.10 USDC, 6 decimals
"payTo": "0x367F1b3D8Ca90D1e087481a9A40d585Bf3451a03",
"asset": "0x833589fCD5eDb6e08F4c7C32D4F71b54bdA02913", // USDC on Base
"network": "base",
"description": "FORGE agent chat"
}
Step 3 — Sign, attach, retry
The agent signs an EIP-712 transferWithAuthorization for that exact price and payee, base64-encodes it into the X-Payment header, and retries the call. A facilitator verifies the signature against the chain and settles in the background — the response itself returns instantly. Most agent frameworks wrap this in one function call.
import { fetchWithPayment } from "x402-fetch";
const agent = privateKeyToAccount(process.env.AGENT_WALLET_KEY);
const res = await fetchWithPayment(url, {
method: "POST",
body: JSON.stringify({ query: "Summarize today's crypto news" }),
x402Version: 1,
accountAddress: agent.address,
signMessage: (m) => agent.signMessage({ message: m }),
});
// res is the real answer — paid, verified, settled on Base
Retry on 402 rather than prepaying: the authorization is bound to one nonce, so a failed call never burns funds. Coinbase's x402 reference client and our live facilitator both follow this exact flow on Base.
Step 4 — Bound the spend, then test before you spend
Two safety rails before production:
- Spend authority. Publish a signed grant that caps the wallet: a budget per rolling hour/day/month, a no-confirm threshold under which the agent pays freely, and an expiry. A facilitator that enforces authorities refuses anything outside the envelope before it can settle. See the authority routes on our facilitator.
- Readiness check. Before the agent ever signs a real payment, confirm the whole buyer side works: terms signable, facilitator answering, wallet funded. Ours is free — try it right now.
This calls our facilitator's free readiness handshake end to end: it reads the endpoint's payment terms, checks the facilitator answers, and reads the wallet's live USDC balance on Base.
The prefilled wallet is our public ops wallet — swap in any Base address, including one you own. The check is read-only: it signs nothing and moves nothing.
What the check proves
The handshake answers one question a buyer actually has: could this payment be constructed and funded right now? It names the endpoint's price, confirms the USDC balance covers it (with the exact shortfall if not), and flags defects that would bite later — a facilitator that does not answer, or terms too incomplete to sign. Gas never blocks the verdict, because settling through a facilitator costs the payer no gas.
Common questions
What does an AI agent need to make payments on its own?
Three things: a wallet it controls, USDC on Base, and an x402 client that signs the payment when an endpoint answers 402. No bank account, card, or gateway API key required.
Does the agent need ETH for gas?
No. The agent signs an EIP-3009 authorization; the facilitator broadcasts the settlement. The payer spends no gas.
How do I stop my agent overspending?
A spend-authority grant: budget per period, no-confirm threshold, expiry. The facilitator refuses payments outside the envelope before settlement.
Can I test without spending money?
Yes — the readiness check above is exactly that, and our facilitator also verifies signed payments for free so you can dry-run the full flow.