Build an agent
The Router
With the Router you ask for a service, not an API. You say what you need, such as a token's risk, and Autonomo picks the API with the best record for it. You pay that API's own price, and the payment only goes through once its answer has passed a check. If the API fails, you are not charged and you are handed the next one.
It is one address, https://autonomo.fun/route/<service>, and it speaks the same x402 payment as every API here. So anything that can pay over plain HTTP can use it: the only new things are a route id and three answers to handle.
Where it stands
The Router is switched on. An API is only picked once it has a record: its last 20 measured calls, with at least 95% of them answered properly. A service whose APIs are still building that record answers 503 with no API can serve this right now. That costs nothing, and you can call the API directly meanwhile: every one is on the official APIs page.
The services
The service goes in the path and its inputs in the query. chain is robinhood (the default) or base. An unknown service answers 404 with this list.
| token-price | The live price of a token, in dollars.Needs token. Also takes chain. Answers price_usd, source, as_of. |
| token-risk | A rug-risk score for a token, with the reasons behind it.Needs token. Also takes chain. Answers score, level, flags. |
| token-holders | Who holds a token: the top holders, wallets that look linked, and snipers.Needs token. Also takes chain. Answers top_holders, linked_groups, snipers. |
| website-check | A trust check of a website: its domain, its certificate and what stands out.Needs url. Answers verdict, reasons. |
| swap-quote | A checked quote for a swap. To build the swap, call the API that quoted it directly: only that API can build its own quote.Needs token_in, token_out, amount, taker. Also takes slippage_bps. Answers quote_id, min_out, risk. |
| dev-wallet | What a token's creator did with it.Needs token. Also takes chain. Answers dev, claimed_fees, sells, funded_by. No API serves it yet. |
| payment-check | Whether a payment in a transaction went through.Needs tx_hash. Also takes chain. Answers confirmed, amount, from, to. No API serves it yet. |
| job-finder | Open jobs an agent can take.Needs no input. Answers jobs. No API serves it yet. |
An API may name its fields its own way, so the Router puts the standard fields above at the top of the answer and the API's whole answer beside them, under raw. Nothing is left out.
A call, step by step
Ask for a service
Send the request with no payment: the service in the path, its inputs in the query.
curlcurl -i "https://autonomo.fun/route/token-risk?token=0xe83c85e1ead16fcfe85401e2b337fb4fb5b5cc96"The answerHTTP/1.1 402 Payment Required PAYMENT-REQUIRED: <the price list, base64> X-Autonomo-Route-Id: Zk3vQ1b8... X-Autonomo-Routed-To: token-risk { "x402Version": 2, "accepts": [ { "scheme": "exact", "network": "eip155:4663", "amount": "10000", "payTo": "0x...", "...": "..." } ], "resource": { "...": "..." }, "route_id": "Zk3vQ1b8...", "routed_to": "token-risk" }Read the price and the route id
The Router picks an API and answers
402with that API's price list, exactly as the API would send it, and a route id. The price and the address to pay are the API's own.A route id works once and for 60 seconds, so pay right after you get it.
Pay, with the route id
Sign a payment for one option on that price list, the way the plain HTTP guide shows. Then send the same request again with the payment and the route id. In the JavaScript,
signis the function in the whole example.A client that can't send the route id still works: the Router finds the route from the request and the API the payment is made out to.
curlcurl -i "https://autonomo.fun/route/token-risk?token=0xe83c85e1ead16fcfe85401e2b337fb4fb5b5cc96" \ -H "PAYMENT-SIGNATURE: <your signed payment, base64>" \ -H "X-Autonomo-Route-Id: Zk3vQ1b8..."The answerHTTP/1.1 200 OK PAYMENT-RESPONSE: <the receipt, base64> X-Autonomo-Routed-To: token-risk X-Autonomo-Bonded: false { "score": 12, "level": "low", "flags": [], "raw": { "...the API's whole answer, as it gave it...": "" } }Read the checked answer
The Router calls the API, checks that the answer has every field the service promises, and only then lets the payment through.
X-Autonomo-Routed-Tonames the API that answered.X-Autonomo-Bondedsays whether its creator has a bond locked.
Ask for a service
curl -i "https://autonomo.fun/route/token-risk?token=0xe83c85e1ead16fcfe85401e2b337fb4fb5b5cc96"HTTP/1.1 402 Payment Required
PAYMENT-REQUIRED: <the price list, base64>
X-Autonomo-Route-Id: Zk3vQ1b8...
X-Autonomo-Routed-To: token-risk
{
"x402Version": 2,
"accepts": [
{ "scheme": "exact", "network": "eip155:4663",
"amount": "10000", "payTo": "0x...", "...": "..." }
],
"resource": { "...": "..." },
"route_id": "Zk3vQ1b8...",
"routed_to": "token-risk"
}The three answers to handle
502 with next
The API failed
Nothing was charged, and
nextis the price list and route id of another API. Sign a new payment for it, since each API is paid at its own address, and send the request again. After three APIs the Router stops and says so.The answerHTTP/1.1 502 Bad Gateway X-Autonomo-Route-Id: p9Xc2LrT... X-Autonomo-Routed-To: another-risk-api { "error": "token-risk failed (status 500), nothing was charged", "next": { "x402Version": 2, "accepts": [ { "...": "the next API's own price and address" } ], "route_id": "p9Xc2LrT...", "routed_to": "another-risk-api" } }503 with Retry-After and next
The API isn't ready yet
Nothing was charged. Wait the seconds it names, at most two minutes, then pay
next, which is the same API. Coming back early only gets the same answer.The answerHTTP/1.1 503 Service Unavailable Retry-After: 20 X-Autonomo-Route-Id: Hm4tW7yQ... { "error": "safe-swap is not ready yet, nothing was charged: call again in 20 seconds", "retry_after_seconds": 20, "next": { "...": "the same API's price list", "route_id": "Hm4tW7yQ...", "routed_to": "safe-swap" } }503 with no next
Nothing can serve it
On the very first request: no API has the record to be picked yet. Call an API directly, or try later.
The answerHTTP/1.1 503 Service Unavailable { "error": "no API can serve this right now", "capability": "token-risk" }
The API failed
HTTP/1.1 502 Bad Gateway
X-Autonomo-Route-Id: p9Xc2LrT...
X-Autonomo-Routed-To: another-risk-api
{
"error": "token-risk failed (status 500), nothing was charged",
"next": {
"x402Version": 2,
"accepts": [ { "...": "the next API's own price and address" } ],
"route_id": "p9Xc2LrT...",
"routed_to": "another-risk-api"
}
}| 200 | A checked answer. The payment went through. |
| 400 | An input is missing (the answer lists the inputs), or the route id is unknown, used or older than 60 seconds: ask again. |
| 402 | With no payment: the price list and a route id. After a payment: it wasn't valid, and a new route id for the same API comes with it. |
| 403 | This wallet is paused for 24 hours, because an earlier payment of its own could not be collected after the answer was ready. |
| 404 | No such service. The answer lists them all. |
| 429 | Too many requests: 240 a minute from one address, or 60 paid calls a minute from one wallet. Wait a minute. |
| 502 | The API failed and nothing was charged. With next: pay the next API. Without it: no other API is left. |
| 503 | With next: the API isn't ready, wait and pay again. Without it: no API can serve this service right now. |
The whole thing in one file
About 130 lines, with one package for signing. It asks, pays the API that was picked, moves to the next one when an API fails, waits when one isn't ready, and stops after three tries. It refuses any price over 50 cents and any token other than the one named in it.
npm install viem
AUTONOMO_WALLET_KEY=0x... node route.mjs token-risk token=0xe83c85e1ead16fcfe85401e2b337fb4fb5b5cc96// Ask Autonomo's router for a service and pay the API it picks. Plain HTTP, and one signature for each try.
// Needs Node 18 or newer and one package, for signing: npm install viem
// Run: AUTONOMO_WALLET_KEY=0x... node route.mjs token-risk token=0xe83c85e1ead16fcfe85401e2b337fb4fb5b5cc96
import { getAddress } from "viem";
import { privateKeyToAccount } from "viem/accounts";
const ROUTER = process.env.AUTONOMO_ROUTER || "https://autonomo.fun";
// The dollars this wallet pays with. To pay USDC on Base instead, set NETWORK to "eip155:8453".
const NETWORK = process.env.PAY_NETWORK || "eip155:4663";
const DOLLARS = {
"eip155:4663": "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168", // USDG on Robinhood Chain
"eip155:8453": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC on Base
};
const MAX_PRICE = 0.5; // dollars. Never sign for more than this.
const MAX_TRIES = 3; // APIs to pay in turn before giving up. A try that fails is never charged.
const MAX_WAIT = 120; // seconds to wait for an API that says it isn't ready yet
const [service, ...inputs] = process.argv.slice(2);
if (!service || !process.env.AUTONOMO_WALLET_KEY) {
console.error("Usage: AUTONOMO_WALLET_KEY=0x... node route.mjs <service> [name=value ...]");
process.exit(1);
}
const account = privateKeyToAccount(process.env.AUTONOMO_WALLET_KEY);
const decode = (header) => JSON.parse(Buffer.from(header, "base64").toString("utf8"));
const encode = (value) => Buffer.from(JSON.stringify(value), "utf8").toString("base64");
const sleep = (seconds) => new Promise((done) => setTimeout(done, seconds * 1000));
// The request: the service in the path, its inputs in the query. It is sent unchanged every time.
const query = new URLSearchParams(inputs.map((pair) => [pair.slice(0, pair.indexOf("=")), pair.slice(pair.indexOf("=") + 1)]));
const url = `${ROUTER}/route/${encodeURIComponent(service)}?${query}`;
/** Signs a payment for one price list. The same steps as paying an API directly. */
async function sign(required) {
const offer = required.accepts.find(
(o) =>
o.scheme === "exact" &&
o.network === NETWORK &&
o.asset.toLowerCase() === DOLLARS[NETWORK].toLowerCase() &&
o.extra?.name, // a name here means one signature is enough
);
if (!offer) throw new Error(`The API the router picked has no one-signature price on ${NETWORK}`);
const price = Number(offer.amount) / 1e6; // USDG and USDC both count in millionths of a dollar
if (price > MAX_PRICE) throw new Error(`The price is $${price}, over the limit of $${MAX_PRICE}`);
const authorization = {
from: account.address,
to: getAddress(offer.payTo),
value: offer.amount,
validAfter: "0",
validBefore: String(Math.floor(Date.now() / 1000) + offer.maxTimeoutSeconds),
nonce: "0x" + Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString("hex"), // new for every payment
};
const signature = await account.signTypedData({
domain: {
name: offer.extra.name,
version: offer.extra.version,
chainId: Number(offer.network.split(":")[1]),
verifyingContract: getAddress(offer.asset),
},
types: {
TransferWithAuthorization: [
{ name: "from", type: "address" },
{ name: "to", type: "address" },
{ name: "value", type: "uint256" },
{ name: "validAfter", type: "uint256" },
{ name: "validBefore", type: "uint256" },
{ name: "nonce", type: "bytes32" },
],
},
primaryType: "TransferWithAuthorization",
message: {
from: authorization.from,
to: authorization.to,
value: BigInt(authorization.value),
validAfter: BigInt(authorization.validAfter),
validBefore: BigInt(authorization.validBefore),
nonce: authorization.nonce,
},
});
const payment = {
x402Version: required.x402Version,
payload: { authorization, signature },
extensions: required.extensions, // copied back as it came
resource: required.resource, // copied back as it came
accepted: offer, // the option you chose, unchanged
};
return { header: encode(payment), price };
}
/** An answer's body: JSON when it is JSON, otherwise its text. */
async function read(res) {
const text = await res.text();
try {
return JSON.parse(text);
} catch {
return text;
}
}
const show = (body) => (typeof body === "string" ? body : JSON.stringify(body));
// 1. Ask. With no payment the router picks an API and answers 402: that API's price list, as when you call it
// directly, and a route id. Anything else is final: 400 an input is missing, 404 no such service, 503 no API can
// serve it right now, 429 too many requests.
const first = await fetch(url);
if (first.status !== 402) {
console.error(first.status, show(await read(first)));
process.exit(1);
}
let step = {
required: decode(first.headers.get("payment-required")),
routeId: first.headers.get("x-autonomo-route-id"),
api: first.headers.get("x-autonomo-routed-to"),
};
for (let tries = 1; ; tries++) {
// 2. Sign for the API that was picked, and ask again with the payment and the route id.
// A route id works once and for 60 seconds, so pay right after you get it.
const { header, price } = await sign(step.required);
const res = await fetch(url, { headers: { "PAYMENT-SIGNATURE": header, "X-Autonomo-Route-Id": step.routeId } });
const body = await read(res);
// 3. An answer the router checked. Only now has the payment gone through.
if (res.status === 200) {
console.log(res.status, show(body));
const receipt = res.headers.get("payment-response");
const bonded = res.headers.get("x-autonomo-bonded") === "true" ? ", which has a bond locked" : "";
console.log(`Answered by ${res.headers.get("x-autonomo-routed-to")}${bonded}. Paid $${price}.${receipt ? ` Transaction: ${decode(receipt).transaction}` : ""}`);
process.exit(0);
}
// 4. The API failed (502) or isn't ready yet (503), and nothing was charged. The answer says what to pay next:
// another API, or the same one after a wait. It needs a new signature, because each API is paid at its own address.
const next = (res.status === 502 || res.status === 503) && body?.next?.route_id ? body.next : null;
const wait = res.status === 503 ? Number(body?.retry_after_seconds ?? res.headers.get("retry-after") ?? 0) : 0;
if (next && tries < MAX_TRIES && wait <= MAX_WAIT) {
console.error(`${step.api}: ${body.error}`);
if (wait > 0) await sleep(wait);
step = { required: next, routeId: next.route_id, api: next.routed_to };
continue;
}
// 5. Anything else ends it, with nothing charged: the payment was refused (402), the wallet is paused (403), no
// API is left to try (502), or there were too many requests (429).
const refused = res.headers.get("payment-response");
const reason = (refused && decode(refused).errorReason) || body?.error || show(body) || "no reason given";
console.error(res.status === 402 ? `Not paid: ${reason}` : `${res.status} ${reason}`);
process.exit(1);
}# another service, with two inputs
AUTONOMO_WALLET_KEY=0x... node route.mjs token-price token=0xe83c85e1ead16fcfe85401e2b337fb4fb5b5cc96 chain=robinhood
# paying with USDC on Base
PAY_NETWORK=eip155:8453 AUTONOMO_WALLET_KEY=0x... node route.mjs website-check url=https://example.comIt prints the answer, the API that gave it, the price and the transaction. On a failure it prints what the Router said and exits with a code other than 0, so an agent can tell the two apart.
This file is tested on every change to this site, against a stand-in that answers as the Router does: the picked API, a failed one, one that isn't ready, and none at all. The payment it sends is compared, field for field, with what the official x402 client sends.
What you can rely on
- You pay the API's own price. The Router adds nothing, and the money goes where a direct call would send it.
- A bad answer is never charged. The payment only goes through after the answer passed the check. A failed API, a slow one, one that isn't ready and one whose answer is missing a field all cost nothing.
- Every try needs its own signature. A payment is made out to one API's address, so it can't be reused for the next one, and nobody can redirect it.
- Keep your own limit. The API names the price, so check it before signing, as the example does.
- A wallet that can't pay is paused. If the answer was ready and the payment then failed because the money or the signature was gone, that wallet is refused for 24 hours.
- What is kept from a call. The token address or transaction hash of a paid call may be used again later to test APIs with real inputs. Websites, wallets and swap details are never reused.
How an API is picked
Each API has a score from its record: how often its answers passed the check (60%), how fast it answers (30%) and whether it is up (10%). The record comes from real routed calls and from regular test calls Autonomo makes itself. The best score is picked, and APIs within 10% of it share the calls. An API under 95% good answers is left out until its record recovers, and so is one whose answers stop matching the service's fields.
When you want more
The SDK, the CLI and the MCP server call APIs directly today and don't use the Router yet. To pick an API yourself, the free discovery API is on the API reference, and the official APIs are described in one file at /openapi.json.