MCP serversx402 APIsAI dev toolsFree APIsSearchGuidesSubmit a toolGet listed / Advertise
Home › Guides › How to let your AI agent pay per API call with x402 (about 10 lines of code)

How to let your AI agent pay per API call with x402 (about 10 lines of code)

Published 2026-10-18

More and more APIs answer an unpaid request with HTTP 402 Payment Required and a machine-readable price. That is the x402 protocol. Your agent reads the price, signs a small USDC payment, sends the request again and gets the data. There is no signup, no API key and no monthly plan. This guide shows the client side: how to let your own script or agent pay per call.

What happens on the wire

  1. The agent calls the endpoint as usual.
  2. The server answers 402. The PAYMENT-REQUIRED header (x402 v2) says how much, in which token, on which network and to which address.
  3. The client signs a USDC transfer authorization (EIP-3009) with its wallet key. Nothing is sent onchain by the client itself.
  4. The client repeats the request with a PAYMENT-SIGNATURE header.
  5. The server has a facilitator verify and settle the payment, then returns the data and a PAYMENT-RESPONSE receipt header.

The code (Node 20+, TypeScript or plain ES modules)

Install the official client packages:

npm install @x402/fetch @x402/evm viem

Then the whole client is about 10 lines:

import { x402Client, wrapFetchWithPayment } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY);
const client = new x402Client();
client.register("eip155:*", new ExactEvmScheme(signer));

const payFetch = wrapFetchWithPayment(fetch, client);
const res = await payFetch("https://freetier-agent-api.pages.dev/api/country?q=DK");
console.log(res.status, await res.json());

I ran this code (with a freshly generated key instead of an environment variable) on 18 Oct 2026 (@x402/fetch and @x402/evm 2.28.0) against a live endpoint, using a fresh wallet with no funds. The library read the 402, signed a payment and retried, and the facilitator rejected it with "insufficient balance". That is the expected result for an empty wallet, and it shows the client side (reading the price, signing, retrying) works. With USDC on Base in the wallet, the same call returns the data.

Rules I would follow before giving an agent a wallet

Endpoints you can try it on

Our own FreeTier Agent API has 18 pay-per-call endpoints, from $0.002 to $0.05 per call, plus 6 full dataset downloads ($5 to $9). Examples:

The OpenAPI spec is at /openapi.json, and agents can discover the endpoints at /.well-known/x402 and /llms.txt.

To find other x402 APIs, browse the x402 section of Tools for Agents, a free directory that lists 2,393 x402 APIs from the public Bazaar index. If you want a cheat sheet of AI model prices and free tiers to go with it, the free PDF has 20 models on two pages.


Disclosure: I wrote this with help from AI tools. The FreeTier Agent API, the directory and the PDF are my own projects; the API is paid per call. The code in this guide uses the open-source x402 client packages, which I do not maintain.