Guide
Run x402 on your own server
Most people don't need this: a hosted gateway puts any API behind x402 with no code. Use this guide if you want x402 inside your own server, on any network your facilitator supports.
Set it up
01Add x402 to your server
Install the middleware for your framework. This example uses Express; Next.js, Hono, Fastify, Go and Python work the same way.
bash
npm install @x402/express @x402/core @x402/evmts
import express from "express";
import { paymentMiddleware, x402ResourceServer } from "@x402/express";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { HTTPFacilitatorClient } from "@x402/core/server";
const app = express();
const payTo = "0xYourWalletAddress"; // where payments land
// Testnet facilitator (Base Sepolia). Switch to a mainnet facilitator to go live.
const facilitator = new HTTPFacilitatorClient({ url: "https://x402.org/facilitator" });
app.use(
paymentMiddleware(
{
"GET /weather": {
accepts: [{ scheme: "exact", price: "$0.001", network: "eip155:84532", payTo }],
description: "Current weather for a city",
mimeType: "application/json",
},
},
new x402ResourceServer(facilitator).register("eip155:84532", new ExactEvmScheme()),
),
);
app.get("/weather", (req, res) => {
res.json({ city: "Lisbon", weather: "sunny", tempC: 24 });
});
app.listen(4021);This runs on Base Sepolia, a testnet, so you can try it without real money. To go live, switch to a mainnet network (Base is eip155:8453) and a mainnet facilitator, as described in the x402 seller quickstart.
02Check that it answers 402
bash
curl -i http://localhost:4021/weather
# HTTP/1.1 402 Payment Required
# PAYMENT-REQUIRED: <price, network and payTo, base64-encoded>Get listed on Autonomo
03Choose how
- Automatically. Register your endpoint with a facilitator that supports Bazaar discovery. Autonomo imports the Coinbase CDP and PayAI catalogs every 3 days, groups endpoints by host and shows price, network and usage.
- Through the launch form. Request a launch with your website and a paid endpoint URL. Every request is reviewed by hand, and APIs with a live
402endpoint go fastest.