x402 Payment Handler
Automatic x402 payments with token swapping for AI agents on Solana. Zero friction, zero fees, zero manual steps.
What is x402 Payment Handler?
The x402 payment handler is a protocol extension that enables pay-per-use APIs. When an API requires payment, it returns a 402 Payment Required response with payment instructions.
MoltyDEX is the first and only x402 payment handler that automatically handles token swapping. When your AI agent encounters a 402 response, MoltyDEX:
- Detects the 402 Payment Required response
- Parses payment requirements (token, amount, address)
- Checks your agent's balance
- Automatically swaps tokens if needed (SOL → USDC, etc.)
- Makes the payment on Solana blockchain
- Retries the original request with payment proof
Result: Your agent gets the data it needs automatically, without any manual intervention.
How x402 Payment Handler Works
Agent Calls API
Your AI agent makes a standard HTTP request to a premium API endpoint.
API Returns 402
API responds with 402 Payment Required, including payment details (token, amount, address).
MoltyDEX Intercepts
HTTPInterceptor automatically detects 402 and parses payment requirements.
Automatic Token Swap
If needed, MoltyDEX swaps tokens automatically via Jupiter aggregator (best prices).
Payment Executed
Payment transaction is built, signed client-side, and sent to Solana blockchain.
Request Retried
Original API request is retried with payment proof. API verifies and returns data.
Why MoltyDEX is the Best x402 Payment Handler
✅ Automatic Token Swapping
Only x402 payment handler with automatic token swapping. Your agent can have SOL, but the API wants USDC? MoltyDEX swaps automatically. No manual steps, no pre-swapping needed.
- Routes through Jupiter aggregator (best prices)
- Supports all SPL tokens
- Handles any token pair
- Zero manual intervention
💰 Zero Platform Fees
0% fees for x402 payment handling. No platform fees, no hidden costs. You only pay Solana network fees (paid separately in SOL).
- No subscription fees
- No per-transaction platform fees
- Transparent pricing
- Pay only for what you use
🔒 Maximum Security
Client-side signing only. Your private keys never leave your system. MoltyDEX builds unsigned transactions - you sign them locally.
- Private keys stay with you
- No key storage on servers
- Open source and auditable
- Production-ready security
⚡ Best Prices
Routes through Jupiter aggregator to find optimal prices across all Solana DEXes. Gets you the best rates automatically.
- Scans Raydium, Orca, Meteora, and more
- Finds optimal swap routes
- Splits orders across DEXes if needed
- Minimizes price impact
How to Integrate x402 Payment Handler
Install SDK
Install @moltydex/agent package using npm or pip. The SDK provides automatic x402 payment handling for both TypeScript/JavaScript and Python agents.
Configure HTTP Interceptor
Set up the HTTPInterceptor with your Solana wallet. This automatically intercepts all HTTP responses and handles 402 Payment Required responses seamlessly.
Enable Automatic Token Swapping
Configure autoSwap: true to enable automatic token conversion. MoltyDEX will automatically swap any token to the required payment token when needed.
Make API Calls
Use standard fetch() or requests.get() calls. When an API returns 402, MoltyDEX automatically handles payment, token swapping, and request retry.
Monitor and Scale
Track payment success rates, monitor balances, and scale your agent operations. MoltyDEX handles all payment complexity automatically.
Quick Start Code
import { HTTPInterceptor } from '@moltydex/agent';
// Setup once - handles all x402 payments automatically
const interceptor = new HTTPInterceptor({
apiUrl: 'https://api.moltydex.com',
walletSecretKey: process.env.WALLET_SECRET_KEY,
autoSwap: true, // Enable automatic token swapping
});
// Now all fetch() calls handle x402 automatically!
const response = await fetch('https://premium-api.com/data');
const data = await response.json(); // Works seamlessly!x402 Payment Handler Use Cases
AI Agent Payments
AI agents need to pay for premium APIs automatically. x402 payment handler enables seamless, automatic payments without manual intervention.
View AI Agent Use Cases →Pay-Per-Use APIs
API providers can monetize their services with x402 payments. Agents pay automatically, including automatic token swapping if needed.
Learn About API Monetization →Automated Data Collection
Agents collect data from multiple paid APIs. x402 payment handler ensures seamless payments even when each API requires different tokens.
Read Case Study →Agent-to-Agent Payments
Enable agents to pay other agents for specialized services. x402 payment handler enables true agent-to-agent economy.
Learn About Agent Economy →x402 Payment Handler Comparison
| Feature | Manual Implementation | Other Solutions | MoltyDEX |
|---|---|---|---|
| Automatic x402 Detection | ❌ Manual | ⚠️ Partial | ✅ Automatic |
| Automatic Token Swapping | ❌ Manual | ❌ None | ✅ Automatic |
| Payment Processing | ⚠️ Complex | ⚠️ Basic | ✅ Complete |
| Request Retry Logic | ⚠️ Manual | ⚠️ Partial | ✅ Automatic |
| Error Handling | ⚠️ Basic | ⚠️ Basic | ✅ Robust |
| Platform Fees | N/A | 0.1-0.5% | 0% |
| Integration Time | Days/weeks | Hours | 5 minutes |
Learn More About x402 Payments
Understanding x402 Protocol
Complete guide to x402 Payment Required protocol for developers
How MoltyDEX Handles x402
Technical deep dive into automatic x402 payment processing
x402 Best Practices
Best practices for implementing x402 payments on Solana
Developer Documentation
Complete API reference and integration guides
For API Providers
Learn how to accept x402 payments for your API
All Blog Posts
Browse all x402 and payment-related content
Ready to Start Handling x402 Payments?
Get started with automatic x402 payment handling in just 5 minutes. Zero fees, zero friction, zero manual steps.