Try it live! Experience X402 on Solana at x402-demo.anyspend.com or explore the source code on GitHub.
GitHub Repository
View source code and examples
npm Package
@b3dotfun/anyspend-x402
Live Demo
Full working Solana demo app
Overview
This guide shows you how to use AnySpend X402 on Solana for both server (accepting payments) and client (making payments) applications. AnySpend X402 on Solana enables gasless SPL token payments using transaction signatures, making it perfect for paywalled APIs, AI agents, and premium services. Key Features:- Gasless transactions - Facilitator sponsors all transaction fees
- SPL token support - Pay with USDC and other SPL tokens on Solana
- Browser wallet integration - Works with Phantom, Solflare, Ledger, and more
- AI agent friendly - Simple fetch wrapper for autonomous payments
Prerequisites
- Node.js 18+ installed
- Solana wallet with some USDC (for testing)
- Basic knowledge of Solana or Express.js (depending on your use case)
Server Setup
Accept Solana USDC payments with just a few lines of code.Installation
Basic Server Example
- Client requests the endpoint
- Middleware returns
402 Payment Requiredwith Solana payment details - Client signs a Solana transaction with USDC transfer
- Facilitator verifies and submits the transaction (gasless)
- Your route handler executes after payment confirmation
Client Setup
There are two ways to make payments on Solana depending on your use case.Option 1: Using Fetch (AI Agents & Backend)
Perfect for Node.js scripts, AI agents, and server-side applications.Installation
Example Script
- Use base58-encoded private key (from Solana keypair)
- Automatically handles 402 responses and payment flow
- Returns transaction hash in
x-payment-responseheader
Option 2: Using Browser Wallets (React/Next.js)
For web applications using Phantom, Solflare, Ledger, or other Solana wallets.Installation
React Component Example
App Wrapper Setup
Wrap your app with wallet providers:- Works with Phantom, Solflare, Ledger, Trust Wallet, and more
- Prompts user to approve transaction signature
- Fully gasless - users only sign, facilitator pays fees
- Bridges Solana Wallet Adapter v1 with X402 v2 signatures
Configuration
Supported Networks
solana- Solana mainnet-betasolana-devnet- Solana devnet (for testing)
Token Configuration
The most common SPL token on Solana is USDC:Facilitator URL
Production:https://mainnet.anyspend.com/x402
The facilitator handles:
- Transaction verification
- On-chain submission
- Gas fee sponsorship
- Payment settlement
Testing
Get Test USDC
For Solana devnet testing:- Get devnet SOL from Solana Faucet
- Use devnet USDC mint:
4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU - Set network to
solana-devnetin your config
Test Your Integration
Common Token Addresses
| Token | Mint Address | Decimals | Network |
|---|---|---|---|
| USDC | EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v | 6 | mainnet-beta |
| USDC | 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU | 6 | devnet |
Error Handling
What’s Next?
X402 Overview
Learn how AnySpend X402 works
Network Support
See all supported chains and tokens
Signature Methods
Deep dive into Solana transaction signatures
Buyers Guide
EVM client setup guide
Troubleshooting
Transaction signature verification failed
Transaction signature verification failed
Check that:
- Your wallet has enough USDC for the payment
- You’re on the correct network (mainnet vs devnet)
- The facilitator URL is correct
Wallet adapter not working
Wallet adapter not working
Make sure:
- You’ve wrapped your app with
WalletProvider - The wallet extension is installed (Phantom, Solflare, etc.)
- You’re using the correct RPC endpoint
Payment succeeded but data not returned
Payment succeeded but data not returned
Verify:
- The server endpoint is configured correctly
- The payment amount matches server expectations
- Check server logs for errors
Getting Help
- Live Demo: Try it yourself at x402-demo.anyspend.com
- Demo Source Code: github.com/b3-fun/anyspend-x402-demo
- Discord: Join our Discord community
- GitHub: github.com/b3-fun/anyspend-x402
- Examples: See full examples in the repository
HypeDuel