Overview
This guide shows you how to use the AnySpend x402 Client SDK to pay for paywalled resources using any cryptocurrency you hold. The SDK handles the entire payment flow automatically, including signature generation and retry logic. The beauty of x402 is that you can use this same client to pay for any x402-enabled service, not just those using AnySpend.Prerequisites
Before you begin, make sure you have:- Node.js 18+ or compatible JavaScript runtime
- A crypto wallet with some tokens (ETH, USDC, DAI, etc.)
- A wallet library like viem or ethers.js
- Access to an x402-enabled service (like a paywalled API)
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
POST Requests with Body
Make paid POST requests:Manual Payment Flow
For more control, handle the payment flow manually:Configuration Options
X402ClientOptions
RequestOptions
wrapFetchWithPayment Parameters
For advanced use cases,wrapFetchWithPayment provides lower-level control:
Supported Tokens
The x402 client can pay with any token that supports EIP-2612 permit or is USDC (which uses EIP-3009 transferWithAuthorization). Common supported tokens:- USDC - Native support on all chains
- DAI - Stablecoin with permit support
- USDT - On chains where it implements permit
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?
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
HypeDuel