Overview
This guide shows you how to add AnySpend x402 payment support to your Express API, enabling you to accept payments in any token while receiving the token of your choice. With just a few lines of code, your API can monetize endpoints and handle automatic payment verification and settlement. Key Benefits:- Buyers can pay with any token they hold (ETH, DAI, USDC, etc.)
- Sellers can receive any token they prefer (USDC, B3, DAI, custom tokens, etc.)
- AnySpend automatically handles all token conversions
- No blockchain infrastructure or wallet management required
Prerequisites
Before you begin, make sure you have:- Node.js 18+ installed
- An Express.js API (or willingness to create one)
- A wallet address to receive USDC payments
- Basic knowledge of Express middleware
Installation
Install the AnySpend x402 package:Quick Start
1. Basic Integration
The simplest way to add payments to your Express API:- Clients request your API endpoint
- Middleware returns 402 Payment Requiredwith payment details
- Client pays with their preferred token (ETH, DAI, etc.)
- AnySpend facilitator converts to USDC and settles to your wallet
- Your route handler executes and returns data
2. Receive Custom Tokens (e.g., B3)
You can configure your API to receive any token, not just USDC:- Buyers can still pay with any token (ETH, USDC, DAI, etc.)
- AnySpend converts their payment to B3 tokens
- You receive exactly 100 B3 tokens in your wallet
- Complete flexibility for both buyers and sellers!
Configuration
Facilitator URL
The AnySpend facilitator is hosted at:@b3dotfun/anyspend-x402 package includes a pre-configured facilitator instance:
Endpoint Configuration
You have three methods to configure payment requirements:Method 1: Simple Dollar Amounts (Recommended)
Let AnySpend handle token conversion automatically:- Clients can pay with any supported token
- AnySpend converts to USDC automatically
- You always receive USDC
Method 2: Specific Token Requirements
Require specific tokens on specific networks. You can receive any token, not just USDC! Example 1: Receive USDC (Stablecoin)Key Points:
- Buyers can still pay with any token they hold
- AnySpend automatically converts their token to the one you want to receive
- You receive exactly the token you specified in your configuration
- Both buyer and seller flexibility are maintained
Finding Token Details
To configure a custom token, you need to know its EIP-712 parameters. Here’s how to find them: Method 1: Check Token Contract Use a blockchain explorer like Basescan:| Token | Name | Version | Decimals | Base Address | 
|---|---|---|---|---|
| USDC | USD Coin | 2 | 6 | 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 | 
| DAI | Dai Stablecoin | 1 | 18 | 0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb | 
| B3 | B3 | 1 | 18 | 0xB3B32F9f8827D4634fE7d973Fa1034Ec9fdDB3B3 | 
Method 3: Flexible (Let Client Choose)
Allow clients to specify their preferred payment token:Advanced Configuration
Dynamic Pricing
Calculate prices based on request parameters:Custom Facilitator Configuration
If you need custom facilitator settings:Multiple Recipients
Route payments to different wallets based on endpoint:Protected Routes
Protect Specific Routes
Only protect endpoints that require payment:Route-Specific Middleware
Apply different payment configs to different route groups:Testing
Local Development
Test your API locally with testnet USDC:Test Client Request
Test with curl:Monitoring Payments
Access Payment Metadata
The middleware attaches payment information to the request:Webhook Notifications
Get notified when payments are settled:Examples
AI Agent API
Data API with Rate Limiting
Best Practices
Use Environment Variables
Use Environment Variables
Store sensitive data in environment variables:
Implement Logging
Implement Logging
Log all payment events for debugging and analytics:
Handle Errors Gracefully
Handle Errors Gracefully
Provide clear error messages when payments fail:
Test Thoroughly
Test Thoroughly
Always test with testnet before deploying to production:
What’s Next?
Overview
Understand how AnySpend x402 works
Network Support
See supported chains, tokens, and addresses
Troubleshooting
Payment verification fails
Payment verification fails
Check that:
- Your facilitator URL is correct: https://mainnet.anyspend.com/x402
- Your recipient address is valid
- The network configuration matches the client’s network
Clients can't pay with preferred token
Clients can't pay with preferred token
Make sure your endpoint config allows flexible payments:
USDC not arriving
USDC not arriving
Verify:
- Your recipient address is correct
- You’re checking the correct network
- Payment was settled (check req.x402Payment.txHash)
Getting Help
- Discord: Join our Discord community
- GitHub: github.com/b3-fun/anyspend-x402
- Documentation: x402.org/docs
- Examples: See full examples in the repository
 HypeDuel
HypeDuel