Skip to main content
This guide walks through adding x402 payment middleware to your server so that clients must pay to access your endpoints. The facilitator supports any ERC-3009-capable token on Abstract — the examples below use USDC.
  • A crypto 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+ installed.

1. Install dependencies

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

2. Add payment middleware

Add the x402 payment middleware to your server. The middleware intercepts incoming requests, returns a 402 Payment Required response if no payment is attached, and verifies/settles payments through Abstract’s facilitator. Replace 0xYourWalletAddress with the wallet address where you want to receive payments.
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 });
});

app.listen(4021, () => {
  console.log("Server listening at http://localhost:4021");
});

3. Test your integration

With your server running, send a request to the protected endpoint:
curl -i http://localhost:4021/weather
You should receive a 402 Payment Required response with a PAYMENT-REQUIRED header containing the payment requirements. This confirms the middleware is working — clients using an x402 client SDK will handle payment automatically.

Using testnet

The examples above use Abstract mainnet (eip155:2741). To use Abstract testnet during development, change the network ID to eip155:11124:
{
  scheme: "exact",
  price: "$0.001",
  network: "eip155:11124",
  payTo: "0xYourWalletAddress",
}
The facilitator URL stays the same — https://facilitator.x402.abs.xyz supports both networks.

Next steps