Skip to main content
This guide walks through adding x402 payment middleware to your server so clients must pay to access your endpoints.
  • A wallet address to receive payments
  • An existing HTTP API built with Express, Next.js, Hono, Gin, FastAPI, or Flask
  • Node.js 18+, Go 1.21+, or Python 3.10+

1. Install dependencies

npm install @x402/express @x402/core @x402/evm

2. Add payment middleware

Wrap your paid routes with x402 middleware and point it at the Abstract facilitator.
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";

const facilitatorClient = new HTTPFacilitatorClient({
  url: "https://facilitator.x402.abs.xyz",
});

app.use(
  paymentMiddleware(
    {
      "GET /weather": {
        accepts: [
          {
            scheme: "exact",
            price: "$0.001",
            network: "eip155:2741",
            payTo,
          },
        ],
        description: "Weather data",
        mimeType: "application/json",
      },
    },
    new x402ResourceServer(facilitatorClient).register(
      "eip155:*",
      new ExactEvmScheme()
    ),
  ),
);

app.get("/weather", (_req, res) => {
  res.send({ weather: "sunny", temperature: 70 });
});

3. Shape the paid resource

For agent-facing APIs, define:
  • a stable route
  • a clear description
  • a deterministic response format
  • a price that is cheap enough for repeated automated use

4. Test the flow

The protected route should:
  1. return 402 Payment Required when called without payment
  2. advertise the payment requirements in the response
  3. return 200 once the client retries with a valid payment

Next

  • Continue to Make x402 Payments to test a paying client
  • Compare with MPP if you expect repeated micro-payments