Skip to main content

Overview

This guide shows you how to use the AnySpend x402 Client SDK to pay for paywalled resources using compatible tokens with EIP-2612 or EIP-3009 support. The SDK handles the entire payment flow automatically, including signature generation and retry logic. You can use this same client to pay for any x402-enabled service, not just those using AnySpend.

Token Compatibility Checker

Browse compatible tokens with EIP-2612 or EIP-3009 support across 19+ networks

Prerequisites

Before you begin, make sure you have:
  • Node.js 18+ or compatible JavaScript runtime
  • A crypto wallet with compatible tokens (USDC, DAI, or other EIP-2612/EIP-3009 tokens)
  • A wallet library like viem or ethers.js
  • Access to an x402-enabled service (like a paywalled API)
Important: Only tokens with EIP-2612 (Permit) or EIP-3009 (TransferWithAuthorization) support are compatible with AnySpend x402 for gasless payments. Use the Token Compatibility Checker to verify your token is supported.

Installation

Install the x402 client SDK:
The SDK uses viem for wallet operations and signature generation.

Basic usage

1. Create a wallet client

First, set up your wallet client using viem:

2. Initialize the x402 client

Create an instance of the x402 client:

3. Make a paid request

Use the client to access paywalled resources:
What happens:
  1. Client makes initial request
  2. API returns 402 Payment Required with payment details
  3. X402Client automatically signs the payment authorization
  4. Client retries the request with payment
  5. API verifies and settles the payment
  6. Client receives the requested data

Advanced usage

Specify payment token

There are two ways to specify which token you want to pay with:

Option 1: using X402Client

Pay with a specific token by setting preferredToken:

Option 2: using wrapFetchWithPayment (advanced)

For more control, use the lower-level wrapFetchWithPayment function:
Benefits of wrapFetchWithPayment:
  • Works with any existing fetch-based code
  • More granular control over payment behavior
  • Can specify max payment value
  • Custom payment requirements selector
  • Supports multi-network signers
If the resource server supports AnySpend middleware, you’ll pay the equivalent amount in your preferred token instead of USDC.

Using HTTP headers for payment preferences

New in X402: You can now specify payment preferences using HTTP headers, which is particularly useful when making direct HTTP requests or when integrating with existing HTTP clients.
When making requests to X402-enabled services, you can specify your preferred payment token and network using these HTTP headers:
Common Token Addresses:
  • USDC on Base: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
  • USDT on Base: 0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2
Network Identifiers:
  • base-mainnet - Base Mainnet
  • ethereum-mainnet - Ethereum Mainnet
  • arbitrum-mainnet - Arbitrum One
  • optimism-mainnet - Optimism Mainnet
  • polygon-mainnet - Polygon PoS
These headers work with any HTTP client and are automatically respected by X402-enabled resource servers using AnySpend middleware. If no headers are provided, the server will default to USDC.

Using headers with X402Client

The X402Client automatically adds these headers based on your configuration:

Using headers with fetch

You can also add these headers manually when using the native fetch API or other HTTP clients:

POST requests with body

Make paid POST requests:

Manual payment flow

For more control, handle the payment flow manually:

Configuration options

X402ClientOptions

Request options

wrapFetchWithPayment Parameters

For advanced use cases, wrapFetchWithPayment provides lower-level control:
Example with all parameters:

Supported tokens

Only tokens with EIP-2612 or EIP-3009 support are compatible with AnySpend x402 for gasless payments.

What are EIP-2612 and EIP-3009?

  • EIP-2612 (Permit): Allows gasless token approvals via off-chain signatures
  • EIP-3009 (TransferWithAuthorization): Enables gasless transfers via off-chain signatures (used by USDC)

Common compatible tokens

  • USDC - EIP-3009 support on all chains
  • B3 - EIP-2612 permit support
  • Many other tokens - Check compatibility using the tool below

Token Compatibility Checker

Browse all compatible tokens with EIP-2612 or EIP-3009 support across 19+ networks
See Network Support for detailed token addresses per chain.

Examples

React component (using X402Client)

React component (using wrapFetchWithPayment)

For applications that need more control or use existing fetch-based code:

Node.js script

Error handling

The SDK throws typed errors for common failure scenarios:

Testing

Test your integration against a test resource server:
Get testnet USDC from Coinbase Faucet.

Best practices

Never hardcode private keys in your application:
Before initiating payment, show users:
  • The exact amount they’ll pay
  • The token being used
  • The resource server domain
  • An option to cancel
Users can reject wallet signature requests. Always handle this gracefully:
Create one X402Client instance and reuse it:

What’s next

Overview

Learn how AnySpend x402 works

Network Support

See supported chains and token addresses

Troubleshooting

The signature verification failed. Check that:
  • Your wallet is connected to the correct network
  • The token address matches the network
  • Your wallet has enough tokens for the payment
You don’t have enough of the payment token. Either:
  • Add more tokens to your wallet
  • Try paying with a different token
Make sure your wallet is on the same network as the payment token:

Getting help