How can an AI agent pay for an API call with USDC?
Last reviewed: August 2026
An agent pays for an API call with the x402 protocol. The API replies 402 Payment Required with a price, the agent signs a USDC payment for exactly that price, and it repeats the request with the payment attached. Use an x402 client library if the agent holds its own wallet, or call Laso’s agentX402Pay to pay from a managed wallet with a per-call price cap and no private key in the agent’s environment.
What happens on the wire
- The agent calls the API normally. A paid endpoint answers HTTP 402 with a JSON challenge listing the price in atomic USDC units (six decimals, so
10000is $0.01), the network, and the address to pay. - The agent’s wallet signs a USDC payment for that amount on one of the offered networks, typically Base or Solana.
- The agent repeats the same request with the signed payment in a header. The API verifies it, settles on-chain, and returns the data.
No account, subscription, or API key is needed on the API side. The price is visible before anything is paid, so the agent can decide whether a call is worth it.
Option 1: the agent holds its own wallet
If your agent controls a wallet with USDC on Base, an x402 client library handles the challenge and replay for you:
import { wrapFetchWithPayment } from "x402-fetch";
import { privateKeyToAccount } from "viem/accounts";
const account = privateKeyToAccount(process.env.WALLET_PRIVATE_KEY);
const fetchWithPay = wrapFetchWithPayment(fetch, account);
const res = await fetchWithPay("https://api.example.com/v1/paid-endpoint");
const data = await res.json();
This is the most direct path, but the private key now lives wherever the agent runs, and nothing outside your own code limits what it pays.
Option 2: pay from a managed wallet with a cap
Laso’s managed agent wallet is a USDC wallet on Solana that Laso custodies for your account. The agent makes ordinary authenticated HTTP calls and Laso settles the x402 payment, so no key sits in the agent’s environment. A human creates a lasoak_ API key once at the agent dashboard and funds the wallet.
1. Exchange the API key for a session
curl -X POST https://laso.finance/agentAuth \
-H "Content-Type: application/json" \
-d '{"api_key":"lasoak_..."}'
# Returns an id_token (valid one hour), a refresh_token, and an auth_url
# your human can open to watch what the agent does.
2. Check the wallet has USDC
curl https://laso.finance/getAgentWallet \
-H "Authorization: Bearer $LASO_ID_TOKEN"
# { "user_id": "usr_...", "has_wallet": true,
# "wallet_address": "...", "balance_usdc": 25, "needs_funding": false }
If needs_funding is true, ask your human to send USDC on Solana to wallet_address. Never send funds to user_id: it is an account id, not an address.
3. Pay the endpoint
curl https://laso.finance/agentX402Pay \
-H "Authorization: Bearer $LASO_ID_TOKEN" \
-H "Content-Type: application/json" \
-d '{"data":{
"userId":"usr_...",
"url":"https://api.example.com/v1/paid-endpoint",
"note":"Market data for the portfolio summary you asked for",
"maxAmountUsdc":0.01
}}'
# { "result": { "status": 200, "body": { ... }, "txHash": "..." } }
maxAmountUsdc is required for an external URL and is the most the agent will pay for this call. Add expectedAsset, expectedNetwork, and expectedPayTo to refuse any challenge that quotes a different token, chain, or receiver. A refused challenge signs nothing. The account owner also sets a per-payment spend limit that the agent can read but not raise.
Reading the result
- Check the inner
status, not the HTTP code. The call returns HTTP 200 whenever it completed, including when the paid endpoint refused. txHashis the Solana signature of the payment and is present only when a payment settled. Use it to confirm payment rather than a balance read, which can trail by up to a minute.- An underfunded wallet comes back as an inner 402 with
errorReason: "insufficient_funds", and nothing is signed.
Which option to pick
| What matters | Own wallet + x402 client | Laso managed wallet |
|---|---|---|
| Private key in the agent’s environment | Yes | No |
| Networks | Whatever your wallet and the API share | Pays from USDC on Solana |
| Price cap enforced outside your code | No | Yes, per call and per account |
| Spending visible to a human | Only if you build it | Activity feed on the dashboard |
| Custody | Self-custody | Laso custodies the wallet |
Full reference: paying an endpoint from the managed wallet, guardrails, and how x402 works.
Frequently asked questions
How do I make my agent pay per request instead of using an API key?
Call an API that supports x402. The agent pays in USDC for each call, so there is no account, subscription, or key to manage on the API side.
What does an agent need to hold to pay for an API call?
USDC on a network the API accepts, usually Base or Solana. With a Laso managed wallet it is USDC on Solana, sent to the wallet_address that getAgentWallet returns.
How do I stop my agent from overpaying for an API call?
Pass maxAmountUsdc on every payment and pin the asset, network, and receiver. A challenge that exceeds the cap or does not match is refused before anything is signed.
Can an agent pay for an API call without a private key?
Yes. With a managed wallet the agent only makes authenticated HTTP calls, and Laso signs and settles the payment on its behalf.
How do I know the payment went through?
Look for txHash in the result. It is the on-chain signature of the settled payment and is absent when nothing was paid.
More guides
- How to give your AI agent a credit card — An AI agent cannot pass credit underwriting, but it can issue itself a virtual prepaid card over an API in about a minute. The working pattern, with real requests: x402 payment in, card number out.
- x402 vs AP2 vs ACP vs MPP vs UCP: which should your agent use? — All five agent payment protocols compared honestly: x402 and MPP for machine-to-machine HTTP payments, ACP for ChatGPT checkout, UCP for Google surfaces, AP2 for authorization. Which layer each one is, when to use each, and a worked x402 example.
- x402 vs Stripe Issuing for AI agents — Two real ways to put a card in an AI agent’s hands: Stripe Issuing behind a business account, or x402 card issuance the agent drives itself. An honest comparison of onboarding, funding, controls, and when each wins.
- Issue a virtual card with one x402 call — A complete, runnable walkthrough: pay one x402 request in USDC and receive a virtual prepaid card with number, expiry, and CVV. Real endpoints, real response shapes, error handling included.
- All agent guides, or how Laso compares to the alternatives
Try it: connect a wallet at laso.finance and you'll have a card, gift card, or payout in minutes.
Building an agent? Point it at laso.finance/SKILL.md — it can set itself up.