Self-host an x402 tool server
Agent402 is open source and the hosted service runs the same code as the
repository. This guide boots a copy on your own machine, turns on payment to
your own wallet, and adds a tool. Most tools are deterministic code; the
model-backed ones (the /v1 gateway, the report products, the media and
answer tools) need an upstream key of your own and stay off until you set one.
1. Boot it free
You need Node.js and git. Clone the repository, then:
cd Agent402
npm install
FREE_MODE=true npm start # every tool runs free, no wallet needed
The server listens on port 3000 (PORT changes it). Check it:
curl -s http://127.0.0.1:3000/health
curl -s -X POST http://127.0.0.1:3000/api/hash \
-H "Content-Type: application/json" \
-d '{"text":"hello world","algo":"sha256"}'
FREE_MODE is the explicit opt-in to run without payment: a bare
npm start exits unless a receiving wallet is set, so a public server is
never free by accident.
The repository's Dockerfile builds the same server with the browser, media
and OCR tools' system packages installed:
docker build -t agent402 .
docker run -p 3000:3000 -e FREE_MODE=true agent402
2. Turn on payment
Unset FREE_MODE and give the server a wallet to be paid at:
WALLET_ADDRESS: your USDC receiving address.NETWORK: the primary chain (baseby default;base-sepoliafor a testnet run).- A facilitator:
FACILITATOR_URL, or the keys of the default one. What a facilitator does. BASE_URL: the public origin of your server. Each 402 names it as the resource being paid for.
Every paid route now answers 402 with your wallet as the recipient. An
agent pays it the way it pays any x402 server; the settlement rule is the one
the hosted service runs: the route runs first, and only a successful answer is
settled. More chains are one setting, PAYMENT_NETWORKS, with a receiving
address for each chain family.
Every setting, with its default, is in the configuration reference; the longer walkthrough (volumes, proxies, redeploys) is the self-hosting page.
3. Add a tool
A tool is one object in a kit array under src/tools/:
{
route: "POST /api/reverse",
name: "Reverse text",
slug: "reverse",
category: "text",
price: "$0.001",
description: "Reverse a string.",
discovery: {
inputSchema: { properties: { text: { type: "string" } }, required: ["text"] },
example: { text: "abc" },
},
handler: (input) => {
if (typeof input.text !== "string") { const e = new Error('"text" required'); e.statusCode = 400; throw e; }
return { reversed: [...input.text].reverse().join("") };
},
}
handler(input) returns JSON, or throws an Error with .statusCode
for a refusal. The server routes it, prices it, publishes its schema in
/openapi.json and /api/pricing, and lists it on the MCP connector. The
test suite calls every tool with its own published example, so the example is
part of the contract. A new tool is paid from a wallet; a pure-CPU tool joins
the free proof-of-work tier when its slug is added to the free-tier list.
4. Point an agent at it
The buyer SDK and the MCP server both take your address:
AGENT402_URL=http://127.0.0.1:3000 npx -y agent402-mcp@0.19.2