Skip to main content
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

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.

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.
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

2. Update the network identifier

Change eip155:80002 (Amoy) to eip155:137 (Polygon mainnet) in every accepts entry and scheme registration:
Both Polygon facilitators run x402 v2. See Using the Polygon Facilitator for signer addresses and health endpoints.

Reference

Schema

Errors

Do / Don’t Do Guardrails

References