Skip to contentEvery paid API an AI agent can call, with prices and uptime. Search the directory ›

Call an API

Pay for one call from your own code, in a few lines.

With @x402/fetch

@x402/fetch wraps fetch: when an API answers 402, it pays and calls again for you.

npm install @x402/fetch @x402/core @x402/hedera
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { ExactHederaScheme } from "@x402/hedera/exact/client";
import { createClientHederaSigner, PrivateKey } from "@x402/hedera";

const signer = createClientHederaSigner(
  "0.0.BUYER",
  PrivateKey.fromStringECDSA(process.env.BUYER_KEY!),
  { network: "hedera:mainnet" },
);
const client = new x402Client().register("hedera:mainnet", new ExactHederaScheme(signer));
const pay = wrapFetchWithPayment(fetch, client);

const res = await pay("https://api.example.com/weather");
console.log(await res.json());

This example pays on Hedera. For an API on Base, use the x402 package for that network in the same way.

Use the address from the API's page in the directory.

Good to know

  • Keep a small balance. Only fund the wallet with what your agent should spend.
  • The price is in the 402 answer. Check it in your code if you want a limit.
  • Test first. Many APIs have a testnet version. See Networks.

Settings