Skip to main content

Monetize your API with x402

x402 lets you monetize HTTP APIs and content with on-chain payments, while keeping your existing server stack. Benefits: βœ… Customers don’t pay network fees.
βœ… Payment settles in < 1 second.
βœ… Universal compatibility β€” if it speaks HTTP, it speaks x402.

Architecture at a glance

x402 sequence diagram
  • Client: Calls your protected resource and submits payments.
  • Server (merchant): Advertises payment requirements, verifies payments, fulfills requests, and settles payments.
  • Facilitator: Verifies and/or settles payments on your behalf via standard endpoints.
  • Blockchain: Where payments are executed and confirmed.

It’s that easy

Add x402 payments to your server with just a few lines:
import { paymentMiddleware, x402ResourceServer } from "@x402/express";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { HTTPFacilitatorClient } from "@x402/core/server";
import { facilitator } from "@payai/facilitator";

const facilitatorClient = new HTTPFacilitatorClient(facilitator);

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

Getting started

Select one of the quickstart examples, or read the reference for more details.

Express

Quickstart for building an x402-enabled server with Express.

Hono

Quickstart for building an x402-enabled server with Hono.

Next.js

Quickstart for building an x402-enabled server with Next.js.

Facilitator setup

The @payai/facilitator package provides a pre-configured facilitator that connects to the PayAI facilitator at https://facilitator.payai.network:
npm install @payai/facilitator
import { facilitator } from "@payai/facilitator";
import { HTTPFacilitatorClient } from "@x402/core/server";

const facilitatorClient = new HTTPFacilitatorClient(facilitator);
This works immediately on the free tier. For production, set PAYAI_API_KEY_ID and PAYAI_API_KEY_SECRET environment variables β€” see Going to Production. For details on how authentication works or to implement it without PayAI packages, see Facilitator Authentication.

x402 reference

For a deeper dive into message shapes, headers, verification and settlement responses, see the x402 Reference.

Need help?

Join our Community

Have questions or want to connect with other developers? Join our Discord server.