x402 services / for agent builders

x402 for agents: find a service, judge it, pay it

An agent that can pay needs three things before it spends: a service that fits, some evidence the service works, and a safe way to pay. The directory answers the first two for free, with no key and no sign-up, and this page shows how to use it from code.

1. Find a service

Two ways in, same ranking. find_tool takes plain words for what you need and returns options from our own tools, the x402 directory and the marketplace, each with a price, why it was picked, and how to call it. Over MCP it is the find_tool tool at https://aipaygen.com/mcp; over HTTP it is POST /v1/find_tool. With pay_with_x402: true only options you can pay by x402 come back, and each carries a directory block for its site. /x402/services.json is the directory itself: filter by cat, verified, network, sort by trust, price or up, or pass need for the find_tool order. Every parameter and field is on the API page; two Atom feeds announce new sites and changes.

# 1. Ask what fits a need (free, no key). Each option says how to call it and what our directory knows about its site.
curl -s -X POST https://aipaygen.com/v1/find_tool -H 'content-type: application/json' \
  -d '{"need": "weather forecast for a city", "pay_with_x402": true, "limit": 5}'

# The same ranking as a list of sites (free; limited to about 60 searches a minute):
curl -s "https://aipaygen.com/x402/services.json?need=weather%20forecast%20for%20a%20city"

# Or browse: one category, only sites that proved their domain, best trust first:
curl -s "https://aipaygen.com/x402/services.json?cat=weather-geo&verified=true&sort=trust"

2. Judge it before you pay

None of these is a guarantee, and each says where it comes from. Use the ones that matter to your agent and set your own bar.

What the directory tells you about a site, and what it does not.
SignalWhereWhat it means
Healthhealth.pct, health.checksThe share of our own checks that got a valid price quote from the site's listings. Not uptime of the paid answer.
Last checkresources[].last_checkWhat our latest check of each listing found, in words, with its time.
Paid test purchasesdelivery_checksEach week we buy one answer from a sample of listings with our own money: how many were delivered of how many tried. A small sample, shown as such.
Domain verifieddomain.verifiedWhoever runs the domain also named the wallet its listings are paid to (a DNS record or a file). It proves control, not quality.
Trusttrust (0 to 100)Our score from checks, delivery and reviews. Empty until there is enough to score.
Priceprice_usd / the quoteThe service's own quote, in USD per call. The quote itself is the only price that binds: read it unpaid first.
# 2. Everything we know about one site (replace the host)
curl -s https://aipaygen.com/x402/service/example.com.json

# 3. Read the price before you pay: ask the service itself, unpaid. It answers 402 and the quote is in the payment-required header.
curl -s -D - -o /dev/null "https://example.com/some/endpoint" | grep -i '^payment-required' | cut -d' ' -f2 | tr -d '\r' | base64 -d

The payment-required header is base64 JSON: accepts[] lists each way to pay (scheme, network, amount in USDC's 6 decimals, asset, payTo). Asking for the price pays nothing.

3. Pay

With an x402 client and your own wallet

The client asks, gets the 402, signs the quoted amount (EIP-3009 USDC authorization, so no gas) and asks again. The service's facilitator settles it and the answer comes back with a receipt in payment-response. You pay the site directly; AIPayGen is not in the payment.

// npm i @x402/fetch @x402/evm viem
import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm";
import { privateKeyToAccount } from "viem/accounts";

// A wallet with a few dollars of USDC on Base. It needs no ETH: the facilitator pays the gas.
const account = privateKeyToAccount(process.env.AGENT_KEY);
const pay = wrapFetchWithPaymentFromConfig(fetch, {
  schemes: [{ network: "eip155:8453", client: new ExactEvmScheme(account) }],
});

// choice.call.url is the address find_tool gave you. On a 402 the client signs the quoted payment and asks again.
// The parameters are the service's own: its listing page (https://aipaygen.com/market) shows its input.
const res = await pay(choice.call.url + "?city=London");
console.log(res.status, await res.json(), res.headers.get("payment-response"));
# pip install "x402[requests,evm]"   (x402 2.x, Python 3.10+)
import os
from eth_account import Account
from x402 import x402ClientSync
from x402.mechanisms.evm.exact import ExactEvmScheme
from x402.http.clients import x402_requests

account = Account.from_key(os.environ["AGENT_KEY"])  # USDC on Base; no ETH needed
client = x402ClientSync()
client.register("eip155:8453", ExactEvmScheme(account))
session = x402_requests(client)  # on a 402 it signs the quoted payment and asks again

url = options[0]["call"]["url"]  # from the example above
r = session.get(url, params={"city": "London"})
print(r.status_code, r.json(), r.headers.get("payment-response"))

With paid_fetch and prepaid credit (no wallet)

If your agent has an AIPayGen key with credit, paid_fetch pays the site from our wallet and charges your credit the site's price plus 15%, plus $0.001 a call. The site's price comes only from credit you bought; trial and referral credit pays just the $0.001 fee. It pays only USDC on Base with the exact scheme, never more than your max_price_usd. If a site takes the payment but doesn't send the page, its price isn't refunded (our fee is). Details on the docs.

curl -X POST https://aipaygen.com/v1/paid_fetch -H "Authorization: Bearer YOUR_KEY" -H "content-type: application/json" \
  -d '{"url": "https://example.com/some/endpoint?city=London", "max_price_usd": 0.01}'

Full examples that find and judge, unpaid

JavaScript (Node 20+, no packages)

// Node 20+, no packages. Finds options, checks each one's site, reads the quote. Nothing is paid.
const base = "https://aipaygen.com";
const found = await (await fetch(base + "/v1/find_tool", {
  method: "POST", headers: { "content-type": "application/json" },
  body: JSON.stringify({ need: "weather forecast for a city", pay_with_x402: true, limit: 5 }),
})).json();

// Options we can reach with x402 (each has call.url, price_usd and the directory facts for its site).
const options = found.data.options.filter((o) => o.call?.protocol === "x402");

// Your own bar, for example: answering at least 90% of our checks, trust 50 or more, price under 1 cent.
const fit = options.filter((o) => (o.directory?.health_pct ?? 0) >= 90 && (o.directory?.trust ?? 0) >= 50 && o.price_usd < 0.01);
const choice = fit[0] ?? options[0];

// More about its site: last check, our paid test purchases, whether the owner proved the domain.
const site = await (await fetch(base + "/x402/service/" + choice.directory.site + ".json")).json();
console.log(site.health.pct + "% of checks passed, trust " + site.trust + ", domain verified: " + site.domain.verified);
console.log(site.delivery_checks.delivered + " of " + site.delivery_checks.attempts + " paid test purchases delivered");
console.log("last check: " + site.resources[0].last_check.result);

// The quote, unpaid: the service answers 402 with its price in the payment-required header (base64 JSON).
const quote = await fetch(choice.call.url);
const required = JSON.parse(atob(quote.headers.get("payment-required")));
console.log(quote.status, required.accepts[0].network, required.accepts[0].amount, "(USDC has 6 decimals)");

Python (standard library)

# Python 3.8+, standard library only. Finds options, checks each one's site. Nothing is paid.
import json, urllib.request

BASE = "https://aipaygen.com"

def post_json(path, body):
    req = urllib.request.Request(BASE + path, data=json.dumps(body).encode(), headers={"content-type": "application/json"})
    return json.load(urllib.request.urlopen(req))

def get_json(path):
    return json.load(urllib.request.urlopen(BASE + path))

found = post_json("/v1/find_tool", {"need": "weather forecast for a city", "pay_with_x402": True, "limit": 5})
options = [o for o in found["data"]["options"] if o.get("call", {}).get("protocol") == "x402"]
for o in options:
    d = o.get("directory") or {}
    print(o["name"], o["price_usd"], d.get("health_pct"), d.get("trust"), d.get("domain_verified"))

site = options[0]["directory"]["site"]
rec = get_json(f"/x402/service/{site}.json")
print(rec["health"]["pct"], rec["trust"], rec["domain"]["verified"], rec["delivery_checks"]["delivered"], "of", rec["delivery_checks"]["attempts"])
print(rec["resources"][0]["last_check"]["result"])

Both ran against this site's live endpoints when this page was written. The services in the answers change; the fields do not (see the stability promise).

Good to know