Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.polygon.technology/llms.txt

Use this file to discover all available pages before exploring further.

This tutorial shows how to add x402 v2 payment middleware to your API or service. By the end, buyers and AI agents will automatically pay in USDC when accessing your protected endpoints.
Polygon facilitators on Amoy and mainnet run x402 v2. Use the @x402/* packages below. For Go or Python server examples, see the official x402 seller quickstart.
These snippets are for demonstration only. Store private keys and facilitator URLs in a secure vault. Never hardcode secrets.

Prerequisites

RequirementExample / Notes
Wallet to receive USDCAny EVM-compatible wallet (Metamask, Rabby, Safe, etc.)
Node.jsNode.js 18+ with npm or Bun
Existing API / serverExpress, Next.js, or Hono
Polygon networkAmoy testnet or mainnet
This guide starts with Amoy testnet configuration. See Running on Polygon mainnet when you are ready for production.

Install dependencies

Wherever bun is used, replace it with npm or your preferred package manager.
bun install @x402/express @x402/core @x402/evm express

Add payment middleware

Integrate payment middleware with:
  • A facilitator client pointing at the Polygon Amoy facilitator
  • An x402ResourceServer with the EVM scheme registered
  • Route config using an accepts array with scheme, price, network, and payTo
Full example in the x402 repo.
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 = "0xYourEvmAddress";

const facilitatorClient = new HTTPFacilitatorClient({
  url: process.env.FACILITATOR_URL || "https://x402-amoy.polygon.technology",
});

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

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

app.listen(4021, () => {
  console.log("Server running at http://localhost:4021");
});
Buyers calling protected routes receive a 402 challenge, pay via the facilitator, and then receive the response.

Running on Polygon mainnet

Once you have tested on Amoy, switch to mainnet:

1. Update the facilitator URL

const facilitatorClient = new HTTPFacilitatorClient({
  url: "https://x402.polygon.technology",
});

2. Update the network identifier

Change eip155:80002 (Amoy) to eip155:137 (Polygon mainnet) in every accepts entry and scheme registration:
// Testnet → Mainnet
network: "eip155:137",
// ...
server.register("eip155:137", new ExactEvmScheme());
Both Polygon facilitators run x402 v2. See Using the Polygon Facilitator for signer addresses and health endpoints.

Reference

Schema

nametyperequiredexampledescription
payTostringyes0xYourAddressAddress that receives USDC
networkstringyeseip155:80002CAIP-2 network ID (Amoy or eip155:137 for mainnet)
schemestringyes"exact"Payment scheme
pricestringyes"$0.001"Cost per request in USDC
FACILITATOR_URLstringoptional"https://x402-amoy.polygon.technology"Polygon facilitator endpoint
descriptionstringoptional"Weather data"Used for discoverability in x402 Bazaar
mimeTypestringoptional"application/json"Response content type

Errors

case / codemeaningfix
402_LOOPClient cannot fulfill paymentCheck facilitator URL, network ID, and wallet
INVALID_NETWORKWrong network identifierUse eip155:80002 (Amoy) or eip155:137 (mainnet)
BAD_CONFIGMissing route configDefine accepts, price, network, and payTo per route

Do / Don’t Do Guardrails

DoDon’t
Use https://x402-amoy.polygon.technology for Amoy testingUse legacy x402-express against Polygon facilitators
Use https://x402.polygon.technology for mainnetExpose private keys in middleware
Test on Amoy before mainnetUse V1 network strings like "polygon-amoy" or "polygon"
Include description and mimeType for AI discoverabilitySkip payTo in route config

References