Monetize Your API

Build paid endpoints that other agents can consume via x402

How It Works

x402 is an HTTP-native payment protocol. When a client hits a protected endpoint without paying, the server returns HTTP 402 with payment requirements. The client signs a USDC payment and retries with a payment header. The facilitator verifies and settles the payment, and the server returns the response.

Express.js middlewareUSDC on BasePer-request pricing
1

Get Your Payment Address

Get the wallet address that will receive payments:

npx awal@latest address

Use this address as the payTo value in your server configuration.

2

Set Up the Project

mkdir x402-server && cd x402-server
npm init -y
npm install express x402-express
3

Create index.js

const express = require("express");
const { paymentMiddleware } = require("x402-express");

const app = express();
app.use(express.json());

const PAY_TO = "<your-wallet-address>";

// x402 payment middleware
const payment = paymentMiddleware(PAY_TO, {
  "GET /api/example": {
    price: "$0.01",
    network: "base",
    config: {
      description: "Description of what this endpoint returns",
    },
  },
});

// Protected endpoint
app.get("/api/example", payment, (req, res) => {
  res.json({ data: "This costs $0.01 per request" });
});

app.listen(3000, () => console.log("Server running on port 3000"));
4

Run and Test

Start the server:

node index.js

Test with curl (should get 402 response):

curl -i http://localhost:3000/api/example

Test with a paid request:

npx awal@latest x402 pay http://localhost:3000/api/example

Patterns

Multiple endpoints with different prices

const payment = paymentMiddleware(PAY_TO, {
  "GET /api/cheap": { price: "$0.001", network: "base" },
  "GET /api/expensive": { price: "$1.00", network: "base" },
  "POST /api/query": { price: "$0.25", network: "base" },
});

app.get("/api/cheap", payment, (req, res) => { /* ... */ });
app.get("/api/expensive", payment, (req, res) => { /* ... */ });
app.post("/api/query", payment, (req, res) => { /* ... */ });

POST with body schema

const payment = paymentMiddleware(PAY_TO, {
  "POST /api/analyze": {
    price: "$0.10",
    network: "base",
    config: {
      description: "Analyze text sentiment",
      inputSchema: {
        bodyType: "json",
        bodyFields: {
          text: { type: "string", description: "Text to analyze" },
        },
      },
      outputSchema: {
        type: "object",
        properties: {
          sentiment: { type: "string" },
          score: { type: "number" },
        },
      },
    },
  },
});

Health check (no payment)

Register free endpoints before the payment middleware:

app.get("/health", (req, res) => res.json({ status: "ok" }));

// Payment middleware only applies to routes registered after it
app.get("/api/data", payment, (req, res) => { /* ... */ });

Pricing Guidelines

Simple data lookup
$0.001 – $0.01
API proxy / enrichment
$0.01 – $0.10
Compute-heavy query
$0.10 – $0.50
AI inference
$0.05 – $1.00

Checklist

  • Get wallet address with npx awal@latest address
  • Install express and x402-express
  • Define routes with prices and descriptions
  • Register payment middleware before protected routes
  • Keep health/status endpoints before payment middleware
  • Test with curl (should get 402) and npx awal@latest x402 pay (should get 200)
  • Announce your service so other agents can find and use it