API Documentation
Version: 1.2.0
Base URL: https://api.x-router.ai
Welcome to the X-Router API documentation! X-Router is a payment-enabled AI API gateway that provides access to multiple AI models through a unified interface using the x402 protocol for gasless micropayments.
Key Features
- Gasless Payments: Pay with USDC without needing ETH for gas fees
- Dynamic Pricing: Costs calculated based on actual token usage
- Multiple Models: Access to 290+ AI models from various providers
- AI Image Generation: Generate images with 50+ models (FLUX, SDXL, SD 1.5)
- Token Transparency: See exactly how many tokens you consume
- Streaming Support: Real-time Server-Sent Events (SSE) streaming
Getting Started
- Get USDC on Base Mainnet (How to get USDC)
- Install the x402-fetch library
- Make your first request to the API
npm install x402-fetch viemimport { wrapFetchWithPayment } from 'x402-fetch';
import { privateKeyToAccount } from 'viem/accounts';
const account = privateKeyToAccount('0x...' as `0x${string}`);
const fetchWithPayment = wrapFetchWithPayment(fetch, account);
const response = await fetchWithPayment('https://api.x-router.ai/v1/chat/completions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: [
{ role: 'user', content: 'Hello! What is 2+2?' }
],
model: 'anthropic/claude-3.5-sonnet',
max_tokens: 100
})
});
const data = await response.json();
console.log(data.content); // "2+2 equals 4."Generate Images
const response = await fetchWithPayment('https://api.x-router.ai/v1/images/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'a serene mountain landscape at sunset, highly detailed',
model: 'flux-schnell',
width: 1024,
height: 1024
})
});
const data = await response.json();
console.log(data.images[0].url); // CDN URL to generated imageHow x402 Payment Works
X-Router uses the x402 protocol for gasless micropayments. When you make a request, the x402-fetch library automatically handles payment by signing a USDC transfer authorization - no gas fees, no blockchain transactions needed.
Learn more: See the Overview for the complete payment flow.
Navigate the Documentation
- Overview - Learn about X-Router and x402 protocol
- Quick Start - Get up and running in minutes
- Chat Completions - Text generation API
- Image Generation - AI image generation API
- Usage Guides - Example implementations and tutorials
Last updated on