Overview
@x402/axios v2 wraps the popular axios HTTP client with automatic x402 payment negotiation. When the API returns402 Payment Required, the client handles the payment flow automatically.
Features
- ✅ Base (EVM) and Solana (SVM) network support
- ✅ Automatic payment negotiation
- ✅ Simple axios-based API
- ✅ Type-safe TypeScript
- ✅ Minimal setup
Prerequisites
- Node.js 18+
- USDC balance on Base or Solana network
- EVM private key (for Base) or SVM private key (for Solana)
Installation
Quick Start
1. Set Up Environment
Create a.env file:
Important: Use your own X (Twitter) account as the receiver to test the integration safely.
2. Implement Base (EVM) Payment
Createbase-send.ts:
3. Implement Solana (SVM) Payment
Createsolana-send.ts:
4. Run the Example
For Base network:Expected Output
How It Works
- Create Signer: Generate account from your private key (EVM or SVM)
- Create x402 Client: Initialize the x402 client
- Register Scheme: Register the appropriate payment scheme (EVM or SVM)
- Wrap Axios: Add x402 payment wrapper
- Make Request: POST to
/payments/x/pay - Auto Payment: Client handles 402 response automatically
- Retry: Request retries with payment proof
- Success: Receive confirmation and receipt
Supported Endpoints
X (Twitter) Payments
Farcaster Payments
GitHub Payments
Configuration Options
Custom Axios Instance
Request Options
Error Handling
Common Errors
Insufficient USDC BalanceTesting Tips
- Start small: Use 0.01 USDC for initial tests
- Test on yourself: Send to your own X account
- Check balance: Verify USDC balance before sending
- Save receipts: Keep receipt URLs for records
Full Example Repository
View the complete example with configuration files: View on GitHub → The repository includes:- Complete TypeScript implementation
- Environment configuration template
- package.json with all dependencies
- Detailed README
Next Steps
- Try CDP SDK - MPC-based key management
- Try fetch - Minimal dependencies
- API Reference - Full API documentation
- x402 Protocol - Understanding the protocol