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: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:- Client makes initial request
- API returns
402 Payment Requiredwith payment details - X402Client automatically signs the payment authorization
- Client retries the request with payment
- API verifies and settles the payment
- 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 settingpreferredToken:
Option 2: using wrapFetchWithPayment (advanced)
For more control, use the lower-levelwrapFetchWithPayment function:
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
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.
- USDC on Base:
0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 - USDT on Base:
0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2
base-mainnet- Base Mainnetethereum-mainnet- Ethereum Mainnetarbitrum-mainnet- Arbitrum Oneoptimism-mainnet- Optimism Mainnetpolygon-mainnet- Polygon PoS
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 nativefetch 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:
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
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:Best practices
Use Environment Variables for Keys
Use Environment Variables for Keys
Never hardcode private keys in your application:
Show Payment Preview to Users
Show Payment Preview to Users
Before initiating payment, show users:
- The exact amount they’ll pay
- The token being used
- The resource server domain
- An option to cancel
Handle Signature Rejection
Handle Signature Rejection
Users can reject wallet signature requests. Always handle this gracefully:
Cache Client Instances
Cache Client Instances
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
402 Error: 'INVALID_SIGNATURE'
402 Error: 'INVALID_SIGNATURE'
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
402 Error: 'INSUFFICIENT_BALANCE'
402 Error: 'INSUFFICIENT_BALANCE'
You don’t have enough of the payment token. Either:
- Add more tokens to your wallet
- Try paying with a different token
Network Mismatch Error
Network Mismatch Error
Make sure your wallet is on the same network as the payment token:
Getting help
- Discord: Join our Discord community
- GitHub: github.com/b3-fun/anyspend-x402
- Examples: See full examples in the GitHub repository
HypeDuel