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:

  1. Detects the 402 Payment Required response
  2. Parses payment requirements (token, amount, address)
  3. Checks your agent's balance
  4. Automatically swaps tokens if needed (SOL → USDC, etc.)
  5. Makes the payment on Solana blockchain
  6. 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

1️⃣

Agent Calls API

Your AI agent makes a standard HTTP request to a premium API endpoint.

2️⃣

API Returns 402

API responds with 402 Payment Required, including payment details (token, amount, address).

3️⃣

MoltyDEX Intercepts

HTTPInterceptor automatically detects 402 and parses payment requirements.

4️⃣

Automatic Token Swap

If needed, MoltyDEX swaps tokens automatically via Jupiter aggregator (best prices).

5️⃣

Payment Executed

Payment transaction is built, signed client-side, and sent to Solana blockchain.

6️⃣

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

1

Install SDK

Install @moltydex/agent package using npm or pip. The SDK provides automatic x402 payment handling for both TypeScript/JavaScript and Python agents.

2

Configure HTTP Interceptor

Set up the HTTPInterceptor with your Solana wallet. This automatically intercepts all HTTP responses and handles 402 Payment Required responses seamlessly.

3

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.

4

Make API Calls

Use standard fetch() or requests.get() calls. When an API returns 402, MoltyDEX automatically handles payment, token swapping, and request retry.

5

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

FeatureManual ImplementationOther SolutionsMoltyDEX
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 FeesN/A0.1-0.5%0%
Integration TimeDays/weeksHours5 minutes

Learn More About x402 Payments

Ready to Start Handling x402 Payments?

Get started with automatic x402 payment handling in just 5 minutes. Zero fees, zero friction, zero manual steps.