How to charge for your API with x402 (and get found by agents)
If you run an API, x402 lets you charge AI agents per request without building signup, billing or API key management. You need a wallet address to receive payments and a few lines of middleware.
1. Add the middleware
Official middleware exists for Express, Hono, Next.js and others, and there are community packages for Python (FastAPI, Flask), Go and Rust. In Hono, for example, it looks roughly like this:
import { Hono } from "hono";
import { paymentMiddleware } from "x402-hono";
const app = new Hono();
app.use(
paymentMiddleware("0xYourReceivingAddress", {
"/weather": { price: "$0.001", network: "base" },
}),
);
app.get("/weather", (c) => c.json({ city: "Amsterdam", tempC: 9 }));
Unpaid requests now get a 402 with your price; paid requests go through. A facilitator verifies and settles the payment for you, so you never touch a blockchain node. Package names differ between SDK versions, so follow the current docs for your framework.
2. Pick a price
Agents compare prices, so look at what similar tools charge in the directory. Common ranges:
- Simple data lookups: $0.001 to $0.01
- Search, scraping, enrichment: $0.005 to $0.05
- AI generation (images, long LLM calls): $0.02 to $0.50
3. Describe your endpoint well
Agents decide based on what you tell them. Include in your payment requirements:
- A clear one-sentence description of what the endpoint returns.
- An input schema (query parameters or JSON body) and an output example.
- The HTTP method.
4. Get discovered
- Enable the Bazaar discovery extension when using the CDP facilitator. x402-tools.com syncs the Bazaar automatically, so your endpoint shows up here within a day.
- Make sure the endpoint answers 402 to an unpaid request. Our health check marks endpoints that do not.
- Want a featured spot or have a tool that is not in the Bazaar? Get in touch.