AgentFuelblog

Let your bot pay per call with x402

Most data APIs want an account, an API key and a monthly plan before your bot can make a single call. With x402 your bot just calls the endpoint, gets a price back, pays it from its own wallet and gets the answer. This post walks through one paid rug-check, step by step.

Why pay per call

The flow in three steps

  1. Your bot calls the endpoint as usual. Without payment the API answers 402 Payment Required, with the price in a PAYMENT-REQUIRED header (base64 JSON).
  2. Your x402 client picks one of the payment options and signs a payment for it. Nothing is sent on-chain yet.
  3. Your bot repeats the same request with the signed payment in the PAYMENT-SIGNATURE header. An x402 facilitator checks the payment, the API answers 200 with the data, and the payment is settled.

The payment is only settled when the call succeeds. A malformed request gets a 400 and costs nothing. AgentFuel never holds your funds: the facilitator moves them straight from your bot's wallet.

What is in the 402

Try it yourself; the first call is free because it only returns the price:

curl -si https://api.agentfuel.eu/v1/rugcheck/So11111111111111111111111111111111111111112 | grep -i payment-required

Decode the header value from base64 and you get the options. AgentFuel offers three, in this order:

OptionNetworkRug-check costs
USDCSolana0.002 USDC
$AFUELSolanathe same value minus 25%
USDCBase0.002 USDC

Each option says who gets paid (payTo), in which token (asset) and how much in base units (amount: 2000 means 0.002 USDC, because USDC has 6 decimals). On Solana the option also names the facilitator's fee payer, so your bot does not need SOL for fees. The full format is in the docs.

The code

The whole loop is a few lines. pay is your x402 client: it takes the requirements, signs a transfer for the option you want and returns the header value. Any x402 client library for your chain can fill that role; see x402.org for the current ones.

const API = "https://api.agentfuel.eu";

async function rugcheck(mint, pay) {
  const url = `${API}/v1/rugcheck/${mint}`;
  let res = await fetch(url);
  if (res.status === 402) {
    const required = JSON.parse(atob(res.headers.get("payment-required")));
    res = await fetch(url, { headers: { "PAYMENT-SIGNATURE": await pay(required) } });
  }
  if (!res.ok) throw new Error(`rugcheck failed: ${res.status}`);
  return res.json();
}

Older x402 v1 clients work too: the 402 body carries the same options in v1 form, and the API accepts a v1 payment in the X-PAYMENT header.

Tips before you go live

No wallet yet? Start with MCP

If your bot is an AI agent, you can skip payment while you try things out. The same checks are tools on our MCP server, with 10 free calls a day:

claude mcp add --transport http agentfuel https://api.agentfuel.eu/mcp