Monetize HTTP endpoints with per-request USDT0 payments using x402 on Stable. Server setup, client integration, and spending controls.
This guide walks through monetizing an API endpoint with x402. The server adds a payment handler, the client pays per request, and settlement happens within the HTTP lifecycle.
Concept: For the x402 protocol and why it fits Stable, see x402. For the high-level use case model, see Pay-per-call APIs.
The Semantic facilitator currently operates on mainnet only. The examples in this guide use Stable mainnet. Use small amounts when testing.
A paid HTTP API where the server responds with 402 Payment Required, the client pays per request, and the facilitator settles USDT0 on-chain within the HTTP lifecycle.
The seller adds x402 middleware to define which routes require payment. When a request arrives without payment, the middleware responds with 402 Payment Required and the payment terms. When a valid payment header is present, the middleware forwards it to a facilitator that verifies the signature and settles the payment on-chain. The seller only configures the price and the receiving address; the facilitator handles verification and settlement.
Each route specifies the payment amount in USDT0 base units (6 decimals), the network, and the address to receive funds. For example, "1000" equals $0.001 and "50000" equals $0.05.
The extra fields (name, version, decimals) are used by the buyer’s client for EIP-712 signature construction and must match the on-chain USDT0 contract.
Routes are mapped using the METHOD /path format. Each route specifies the accepted payment scheme, network, price, and the address to receive funds (payTo). The description and mimeType fields help buyers and AI agents discover what the endpoint provides. Routes not listed in the config are not gated and behave like normal Express routes.
// server.tsimport express from "express";import { paymentMiddleware, x402ResourceServer } from "@x402/express";import { ExactEvmScheme } from "@x402/evm/exact/server";import { HTTPFacilitatorClient } from "@x402/core/server";const PAY_TO = process.env.PAY_TO_ADDRESS as `0x${string}`;const FACILITATOR_URL = "https://x402.semanticpay.io/";const STABLE_NETWORK = "eip155:988"; // Stable Mainnet CAIP-2 IDconst USDT0_STABLE = "0x779Ded0c9e1022225f8E0630b35a9b54bE713736";const facilitatorClient = new HTTPFacilitatorClient({ url: FACILITATOR_URL });const resourceServer = new x402ResourceServer(facilitatorClient) .register(STABLE_NETWORK, new ExactEvmScheme());const app = express();app.use( paymentMiddleware( { // Example 1: Configure a paid GET route "GET /weather": { accepts: [ { scheme: "exact", network: STABLE_NETWORK, price: { amount: "1000", // $0.001 asset: USDT0_STABLE, extra: { name: "USDT0", version: "1", decimals: 6 }, }, payTo: PAY_TO, }, ], description: "Weather data", mimeType: "application/json", }, // Example 2: Configure a paid POST route "POST /inference": { accepts: [ { scheme: "exact", network: STABLE_NETWORK, price: { amount: "50000", // $0.05 asset: USDT0_STABLE, extra: { name: "USDT0", version: "1", decimals: 6 }, }, payTo: PAY_TO, }, ], description: "AI inference endpoint", mimeType: "application/json", }, }, resourceServer, ),);app.get("/weather", (req, res) => { res.json({ weather: "sunny", temperature: 70 });});app.post("/inference", (req, res) => { const { prompt } = req.body; res.json({ result: `Inference result for: ${prompt}` });});// Not listed in the config, so no payment required.app.get("/health", (req, res) => { res.json({ status: "ok", payTo: PAY_TO });});const PORT = process.env.PORT || 4021;app.listen(PORT, () => { console.log(`Server listening at http://localhost:${PORT}`); console.log(`GET /health - free`); console.log(`GET /weather - $0.001 per request`); console.log(`POST /inference - $0.05 per request`);});
x402 also provides middleware for Hono (@x402/hono) and Next.js (@x402/next). The pattern is the same: create a facilitator client, register the EVM scheme, and apply middleware.
The buyer accesses paid endpoints without going through manual payment flows. The buyer does not pay gas. The facilitator settles on-chain, and the buyer only pays the exact amount specified in the payment requirements.
WalletAccountEvm satisfies the signer interface that x402 expects, so it can be registered directly as the signer for the x402 client. Once registered, requests sent through the x402-enabled client handle 402 payment flows automatically.
import { x402Client, wrapFetchWithPayment } from "@x402/fetch";import { registerExactEvmScheme } from "@x402/evm/exact/client";const client = new x402Client();registerExactEvmScheme(client, { signer: account });const fetchWithPayment = wrapFetchWithPayment(fetch, client);const response = await fetchWithPayment("http://localhost:4021/weather");const data = await response.json();console.log("Response:", data);
Under the hood, fetchWithPayment intercepts the 402 response, parses the payment requirements (amount, token, network, recipient), signs an ERC-3009 transferWithAuthorization with the WDK wallet, and retries the request with the PAYMENT-SIGNATURE header.
If you prefer Axios, use @x402/axios with wrapAxiosWithPayment for the same automatic payment handling.
Start the server and verify both the paid and free routes.
This test flow runs on Stable mainnet. Each successful paid request settles a real USDT0 payment through the hosted facilitator. Use a dedicated wallet and small amounts only.
Because the Semantic facilitator is mainnet-only, you can’t point your server at a testnet facilitator today. To iterate on server logic, route handlers, and middleware behavior without settling real payments, stub the facilitator client.
// server.test.tsimport { x402ResourceServer } from "@x402/express";import { ExactEvmScheme } from "@x402/evm/exact/server";// Stub facilitator: accepts any signature, returns a fake settlement.const stubFacilitatorClient = { verify: async () => ({ isValid: true, payer: "0xMockPayer" }), settle: async () => ({ success: true, txHash: "0xMOCK000000000000000000000000000000000000000000000000000000000001", networkId: "eip155:988", }),};export const testResourceServer = new x402ResourceServer(stubFacilitatorClient as any) .register("eip155:988", new ExactEvmScheme());
Run unit tests against the stub to validate:
402 responses include the correct PAYMENT-REQUIRED payload.
Requests with a valid PAYMENT-SIGNATURE header reach the handler.
Requests with a missing or malformed header get rejected before the handler runs.
When you’re ready to exercise real settlement, swap back to HTTPFacilitatorClient and run on mainnet with small amounts.
Stubbed settlement only verifies middleware behavior. It doesn’t prove your route handler is idempotent under real network latency or concurrent payments. Always finish with a live mainnet test against small amounts before shipping.
x402 provides hooks to intercept and customize payment processing at key points in the flow. For example, the server can run logic before verification (e.g., checking API keys or subscriber status) to bypass payment for authorized requests, and the client can enforce spending limits before signing.For the full hook reference and examples, see x402 Lifecycle Hooks.