OpenLibx402 Examples¶
This directory contains example applications demonstrating the X402 payment protocol in both Python and TypeScript.
๐ Structure¶
๐ Python Examples¶
FastAPI Server¶
HTTP API server that requires payments for premium endpoints.
Location: python/fastapi-server/
Run:
Endpoints:
- GET / - API info (free)
- GET /free-data - Free endpoint
- GET /premium-data - Requires 0.10 USDC
- GET /expensive-data - Requires 1.00 USDC
- GET /tiered-data/{tier} - Requires 0.05 USDC
LangChain Agent¶
AI agent that autonomously pays for API access.
Location: python/langchain-agent/
Run:
Features: - Autonomous payment capability - Multiple API access - Custom tool integration
LangGraph Workflow¶
Workflows with payment nodes for accessing paid APIs.
Location: python/langgraph-workflow/
Run:
Features: - Payment nodes - Conditional routing - Multi-step workflows
๐ฆ TypeScript Examples¶
Express.js Server¶
HTTP API server that requires payments for premium endpoints.
Location: typescript/express-server/
Run:
Or from root:
Endpoints:
- GET / - API info (free)
- GET /free-data - Free endpoint
- GET /premium-data - Requires 0.10 USDC
- GET /expensive-data - Requires 1.00 USDC
- GET /tiered-data/:tier - Requires 0.05 USDC
LangChain.js Agent¶
AI agent that autonomously pays for API access.
Location: typescript/langchain-agent/
Run:
Or from root:
Features: - Autonomous payment capability - Multiple API access - Custom tool configuration
LangGraph.js Workflow¶
Workflows with payment nodes for accessing paid APIs.
Location: typescript/langgraph-workflow/
Run:
Or from root:
Features: - Payment nodes - Conditional routing - Multi-step workflows
๐ Quick Start¶
1. Start a Server¶
Python FastAPI:
TypeScript Express:
Server will be available at:
- Python: http://localhost:8000
- TypeScript: http://localhost:3000
2. Test with Client¶
Python Client:
TypeScript Client:
3. Run Agent Examples¶
Make sure the server is running first!
Python LangChain:
TypeScript LangChain.js:
๐ Prerequisites¶
All Examples¶
- Solana wallet with:
- SOL for transaction fees
- USDC tokens for payments
- Running server (FastAPI or Express)
Agent Examples (Additional)¶
- OpenAI API key (for LangChain/LangChain.js agents)
- Set as
OPENAI_API_KEYenvironment variable
๐ฐ Wallet Setup¶
All examples will create a wallet.json file on first run.
Fund your wallet:
โ ๏ธ Security Warning:
- Never commit wallet.json to version control
- In production, use secure key management
- The wallet files are in .gitignore
๐งช Testing Flow¶
- Start Server: Run FastAPI or Express server
- Test Free Endpoint:
curl http://localhost:3000/free-data - Test Paid Endpoint:
curl http://localhost:3000/premium-data - Should return 402 Payment Required
- Run Agent: Agent automatically handles payment
- Verify: Check agent receives data
๐ Learn More¶
Each example directory contains its own detailed README:
- FastAPI Server
- Python LangChain Agent
- Python LangGraph Workflow
- Express.js Server
- TypeScript LangChain Agent
- TypeScript LangGraph Workflow
๐ง Troubleshooting¶
Server Won't Start¶
- Check port isn't already in use
- Verify dependencies are installed
- Check wallet address is configured
Agent Can't Pay¶
- Ensure server is running
- Fund wallet with SOL and USDC
- Check OpenAI API key is set
- Verify server URL is correct
Payment Verification Fails¶
- Check RPC URL is accessible
- Ensure transaction was broadcast
- Verify wallet has sufficient funds
๐ฏ Next Steps¶
- Run the server examples
- Test with simple curl requests
- Try the agent examples
- Build your own application!
For more information, see the main documentation.