Skip to main content
This tutorial walks through setting up an x402 v2 buyer client on Polygon. By the end, your client will automatically detect 402 Payment Required responses, pay in USDC, and retrieve the unlocked resource.
Polygon facilitators on Amoy and mainnet run x402 v2. Use the @x402/* packages shown below. If you have an existing V1 integration, see Migration: V1 to V2.
This tutorial uses test credentials and local endpoints for clarity. In production, never expose private keys or facilitator URLs publicly.

Prerequisites

Before you begin, ensure you have:
  • A crypto wallet with USDC on Polygon Amoy (or mainnet for production)
  • Node.js 18+ with npm or Bun
  • A service that requires payment via x402, or a local seller from the Quickstart for Sellers
For Go or Python client examples, see the official x402 buyer quickstart.

Install dependencies

Install the x402 v2 client packages and viem for signing:
Wherever bun is used, replace it with npm or your preferred package manager.

Create a wallet signer

Create a signer from your private key. In x402 v2, the client selects the correct network from the 402 response; you do not bind a chain-specific wallet client.
Fund this wallet with test USDC on Amoy before calling paid endpoints. See the Polygon faucet for test POL.

Make paid requests automatically

@x402/fetch extends the native fetch API to handle 402 responses and payment headers for you.
The client:
  1. Sends the request
  2. Receives 402 Payment Required with payment requirements
  3. Pays in USDC via the seller’s facilitator (Polygon Amoy: https://x402-amoy.polygon.technology)
  4. Retries with a PAYMENT-SIGNATURE header
  5. Returns the unlocked response and a PAYMENT-RESPONSE receipt header

Payment schemes

Most Polygon endpoints use the exact scheme (fixed price per request). Servers may also advertise upto (usage-based) or batch-settlement (high-volume batched micropayments). Register the matching scheme when the server requires it. See x402 payment schemes.

Error handling

Reference

Configuration, error codes, and guardrails.

Schema

The facilitator URL is configured on the seller side. Buyers do not set it directly; the 402 response includes payment requirements for the seller’s facilitator.

Errors

Do / Don’t Do Guardrails

References