OpenLibx402 Quick Reference Guide¶
Installation¶
FastAPI Server Examples¶
Simple Decorator Approach¶
Dependency Injection Approach¶
Global Configuration¶
Client Examples¶
Explicit Client (Manual Control)¶
Implicit Client (Auto-Payment)¶
LangChain Examples¶
Using X402PaymentTool¶
Using X402RequestsWrapper (Middleware)¶
Convenience Function¶
LangGraph Examples¶
Basic Payment Node¶
Combined Fetch-and-Pay Node¶
Error Handling¶
Catching Specific Errors¶
Handling All X402 Errors¶
Configuration¶
Environment Variables¶
Testing¶
Mock Payment Processor¶
Test Server¶
Wallet Setup¶
Creating a New Wallet¶
Loading Wallet¶
Getting Devnet SOL (for fees)¶
Common Patterns¶
Retry Configuration¶
Custom Payment Amount¶
Multiple APIs¶
Devnet Quick Start¶
Production Checklist¶
- [ ] Use mainnet RPC URL
- [ ] Use real USDC mint address
- [ ] Never log private keys
- [ ] Use HTTPS for all APIs
- [ ] Implement rate limiting
- [ ] Set up monitoring
- [ ] Configure reasonable payment timeouts
- [ ] Implement proper error handling
- [ ] Use environment variables for config
- [ ] Test with small amounts first
- [ ] Set maximum payment limits
- [ ] Implement wallet balance monitoring
Resources¶
- X402 Whitepaper: https://www.x402.org/x402-whitepaper.pdf
- Solana Docs: https://docs.solana.com
- SPL Token: https://spl.solana.com/token
- FastAPI: https://fastapi.tiangolo.com
- LangChain: https://python.langchain.com
- LangGraph: https://langchain-ai.github.io/langgraph
Common Issues¶
"Insufficient Funds" Error¶
- Check wallet balance:
solana balance <ADDRESS> --url devnet - Ensure you have both SOL (for fees) and USDC (for payment)
"Transaction Failed" Error¶
- Check network connectivity
- Verify RPC endpoint is accessible
- Ensure devnet is not experiencing issues
"Payment Verification Failed"¶
- Transaction may not be confirmed yet
- Check transaction on Solana Explorer
- Verify payment address and amount are correct
Import Errors¶
- Ensure all packages are installed
- Check Python version (requires 3.8+)
- Verify virtual environment is activated
Happy Building! 🚀