Skip to Content
X-Router

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

  1. Get USDC on Base Mainnet (How to get USDC)
  2. Install the x402-fetch library
  3. Make your first request to the API
npm install x402-fetch viem
import { 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 image

How 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.

Last updated on