How do I pay for an x402 API?
Short answer
Install an x402 client library, give it a wallet that holds the token the seller accepts (usually USDC), and make your request through it. The library reads the 402 reply, signs the payment and repeats the request for you. Official clients exist for JavaScript, Python and Go.
What you need
- A wallet with the right token. Most endpoints charge in USDC. The wallet must hold it on a network the seller accepts. You do not normally need the network's own coin for fees, because the facilitator or a sponsor pays them.
- An x402 client library for your language.
- The address of a paid endpoint.
Before writing code, paste the endpoint into the 402 checker. It shows the price and which networks are accepted, so you know what to fund.
Install a client
# JavaScript or TypeScript
npm install @x402/fetch @x402/evm # add @x402/svm for Solana
# Python
pip install "x402[httpx]" # or "x402[requests]"
# Go
go get github.com/x402-foundation/x402/go/v2
Make a paid request (JavaScript)
This is the pattern from the official quickstart. It wraps fetch so that a 402 reply is paid and retried automatically.
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
// Load the key from the environment. Never paste it into code.
const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`);
const client = new x402Client();
client.register("eip155:*", new ExactEvmScheme(signer));
const fetchWithPayment = wrapFetchWithPayment(fetch, client);
const response = await fetchWithPayment("https://api.example.com/paid-endpoint");
console.log(await response.json());
"eip155:*" means "any EVM network". The client will pay whichever offer in the 402 reply it has a scheme and funds for.
The official documentation has the same flow for Axios, Python and Go, and for Solana and other networks.
Ways to hold the wallet
- A private key in an environment variable. Simplest, and fine for a test wallet holding a few dollars.
- A managed or embedded wallet. A provider keeps the key and signs for you, often with spending rules. See wallets in the directory.
- A wallet your AI assistant can use. Some wallets connect to assistants through MCP with a budget. See x402 with MCP.
Keep it safe
- Use a separate wallet for paying APIs and keep only a small balance in it.
- Set a maximum price per request in your client where it offers one, so a mispriced or hostile endpoint cannot drain the wallet.
- Read is x402 safe? before giving an autonomous agent a funded wallet.
If it fails
A rejected payment comes back as another 402 with an error code. The usual causes are not enough tokens on that network, or an authorization that expired before it was settled. See x402 error codes.
No wallet?
Some services can be used without one. See using x402 services without a crypto wallet.
Sources
- Quickstart for buyers, x402 documentation
- Quickstart for buyers, Coinbase CDP documentation
- Client examples, x402 repository
Facts checked against these sources on . Protocols change, so the official specification wins if it disagrees with this page. Read this page as Markdown.
Related questions
Who writes this. Ask402 is published by Havok Holdings LLC, which also runs Agent402, one of the x402 services covered here. Agent402 is labelled wherever it appears. Everything else is described from each project’s own documentation, and nobody pays to be listed.