API Documentation
The x402 Gateway exposes 17 production APIs behind x402 micropayments. Every endpoint returns 402 Payment Required until a valid stablecoin payment is attached to the request.
https://x402-gateway-production.up.railway.app
Quick Start
Install the x402 client packages and viem:
npm install @x402/fetch viem
Make your first paid call with TypeScript:
import { wrapFetchWithPayment } from "@x402/fetch";
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";
// Create a wallet with USDC on Base
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const wallet = createWalletClient({
account,
chain: base,
transport: http(),
});
// Wrap native fetch — handles 402 negotiation automatically
const paidFetch = wrapFetchWithPayment(fetch, wallet);
// Call any endpoint — payment is transparent
const BASE = "https://x402-gateway-production.up.railway.app";
// GET endpoint
const price = await paidFetch(`${BASE}/api/crypto/price?ids=bitcoin`);
console.log(await price.json());
// { service: "crypto-price", data: { bitcoin: { usd: 97234 } } }
// POST endpoint
const balances = await paidFetch(`${BASE}/api/wallet/balances`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ chain: "base", address: "0x..." }),
});
console.log(await balances.json());
How does payment work?
paidFetchcalls the endpoint normally- Gateway returns
402with payment requirements in headers - The wrapper reads the requirements, signs a USDC/USDm permit or transfer
- It retries the request with the payment in
X-PAYMENTheader - Gateway verifies the payment, calls the upstream API, and returns data
Payment Flow
If you're building a custom client (not using @x402/fetch), here's the raw HTTP flow:
Step 1: Initial request
GET /api/crypto/price?ids=bitcoin HTTP/1.1
Host: x402-gateway-production.up.railway.app
Step 2: 402 response
The gateway returns payment requirements as base64 JSON in the PAYMENT-REQUIRED header:
HTTP/1.1 402 Payment Required
PAYMENT-REQUIRED: eyJ4NDAyVmVyc2lvbiI6MiwiYWNjZXB0cyI6Wy4uLl19
Content-Type: application/json
{
"error": "X-PAYMENT header is required",
"accepts": [
{
"scheme": "exact",
"network": "eip155:8453",
"maxAmountRequired": "1000",
"resource": "https://x402-gateway-.../api/crypto/price?ids=bitcoin",
"payTo": "0x7dd5Be069f2d2eAd75eC7C3423B116fF043c2629",
"extra": { ... }
}
],
"x402Version": 2
}
Step 3: Retry with payment
Sign a payment using the details from accepts and send it in the X-PAYMENT header:
GET /api/crypto/price?ids=bitcoin HTTP/1.1
Host: x402-gateway-production.up.railway.app
X-PAYMENT: <base64-encoded signed payment payload>
Step 4: Response with settlement
HTTP/1.1 200 OK
PAYMENT-RESPONSE: <base64 settlement receipt>
Content-Type: application/json
{
"service": "crypto-price",
"data": { "bitcoin": { "usd": 97234.12, "usd_24h_change": 2.31 } }
}
Supported Networks
| Network | CAIP-2 | Token | Decimals | Speed | Verification |
|---|---|---|---|---|---|
| Base | eip155:8453 | USDC | 6 | ~2s | Coinbase CDP facilitator |
| MegaETH | eip155:4326 | USDm | 18 | ~10ms | Direct on-chain |
| Solana | solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp | USDC | 6 | ~400ms | Coinbase CDP facilitator |
$0.001 = 1000 USDC (6 decimals) = 1000000000000000 USDm (18 decimals)
Discovery
Agents can discover available services and payment options programmatically.
/.well-known/x402.json
Free
Full discovery document — services, networks, payment routes, categories.
/api/services
Free
List all services with IDs, descriptions, pricing, and endpoints.
/api/services/:id
Free
Service details including payment options. Example: /api/services/crypto-price
Crypto & Blockchain
/api/crypto/price
$0.001
Real-time cryptocurrency prices in any fiat currency.
Parameters
| Name | Type | Description |
|---|---|---|
| ids required | string | Comma-separated CoinGecko IDs, e.g. bitcoin,ethereum |
| currencies optional | string | Comma-separated fiat codes. Default: usd |
| include_24h optional | string | true or false. Default: true |
| include_mcap optional | string | true or false. Default: false |
Example Response
{
"service": "crypto-price",
"data": {
"bitcoin": { "usd": 97234.12, "usd_24h_change": 2.31 },
"ethereum": { "usd": 3456.78, "usd_24h_change": -0.45 }
}
}
/api/crypto/markets
$0.002
Market rankings with price, volume, and market cap.
Parameters
| Name | Type | Description |
|---|---|---|
| currency optional | string | Fiat currency. Default: usd |
| category optional | string | Filter by CoinGecko category |
| order optional | string | Sort order. Default: market_cap_desc |
| limit optional | number | Results per page, max 250. Default: 100 |
| page optional | number | Page number. Default: 1 |
Example Response
{
"service": "crypto-markets",
"count": 100,
"data": [
{ "id": "bitcoin", "symbol": "btc", "name": "Bitcoin",
"current_price": 97234, "market_cap": 1920000000000, ... }
]
}
/api/crypto/history
$0.003
Historical price, market cap, and volume data.
Parameters
| Name | Type | Description |
|---|---|---|
| id required | string | CoinGecko coin ID, e.g. bitcoin |
| currency optional | string | Default: usd |
| days optional | string | Days of history or "max". Default: 30 |
| interval optional | string | Data interval, e.g. daily |
Example Response
{
"service": "crypto-history",
"data": {
"prices": [[1706745600000, 43012.5], [1706832000000, 43150.2], ...],
"market_caps": [...],
"total_volumes": [...]
}
}
/api/crypto/trending
$0.001
Currently trending coins on CoinGecko. No parameters required.
Example Response
{
"service": "crypto-trending",
"data": {
"coins": [
{ "item": { "id": "pepe", "name": "Pepe", "symbol": "PEPE", "market_cap_rank": 24, ... } }
]
}
}
/api/crypto/search
$0.001
Search for coins by name or symbol to get CoinGecko IDs.
Parameters
| Name | Type | Description |
|---|---|---|
| q required | string | Search query, e.g. btc |
Example Response
{
"service": "crypto-search",
"data": {
"coins": [
{ "id": "bitcoin", "name": "Bitcoin", "symbol": "BTC", "market_cap_rank": 1 }
]
}
}
/api/wallet/balances
$0.005
Token balances for any wallet across 20+ chains. Powered by Allium.
Request Body
| Field | Type | Description |
|---|---|---|
| chain required | string | Chain name: ethereum, base, solana, etc. |
| address required | string | Wallet address |
Example
POST /api/wallet/balances
Content-Type: application/json
{ "chain": "base", "address": "0x7dd5Be069f2d2eAd75eC7C3423B116fF043c2629" }
{
"service": "wallet-balances",
"data": {
"balances": [
{ "token_symbol": "USDC", "balance": "164.23", "usd_value": 164.23, ... },
{ "token_symbol": "ETH", "balance": "0.05", "usd_value": 162.50, ... }
]
}
}
/api/wallet/transactions
$0.005
Transaction history with asset transfers and labels.
Request Body
| Field | Type | Description |
|---|---|---|
| chain required | string | Chain name |
| address required | string | Wallet address |
/api/wallet/pnl
$0.01
Portfolio profit/loss with realized and unrealized P&L per token.
Request Body
| Field | Type | Description |
|---|---|---|
| chain required | string | Chain name |
| address required | string | Wallet address |
| min_liquidity optional | number | Minimum liquidity filter |
| min_volume_24h optional | number | Minimum 24h volume filter |
/api/token/prices
$0.005
Real-time DEX-derived token prices with OHLC data. Up to 200 tokens per request.
Request Body
| Field | Type | Description |
|---|---|---|
| tokens required | array | Array of { token_address, chain } objects. Max 200. |
Example
POST /api/token/prices
Content-Type: application/json
{
"tokens": [
{ "token_address": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", "chain": "base" }
]
}
/api/token/metadata
$0.002
Token/asset metadata — name, symbol, decimals, chain info.
Parameters (at least one required)
| Name | Type | Description |
|---|---|---|
| chain | string | Chain name |
| address | string | Token contract address |
| slug | string | Token slug |
| id | string | Token ID |
Compute
/api/image/fast
$0.015
Quick image generation using FLUX Schnell (~2s). Good for drafts and iteration.
Request Body
| Field | Type | Description |
|---|---|---|
| prompt required | string | Image description |
| width optional | number | 256–2048 pixels. Default: 1024 |
| height optional | number | 256–2048 pixels. Default: 1024 |
| seed optional | number | For reproducible results |
Example
POST /api/image/fast
Content-Type: application/json
{ "prompt": "A cyberpunk cityscape at sunset, neon lights reflecting off rain-slicked streets" }
/api/image/quality
$0.05
High-quality image generation using FLUX.2 Pro (~5s). Production-ready output.
Same parameters as /api/image/fast.
/api/image/text
$0.12
Image generation with accurate text rendering using Ideogram v3. Best for logos, signs, and typography.
Same parameters as /api/image/fast.
/api/code/run
$0.005
Run code in an isolated sandbox. Supports Python, JavaScript, Bash, and R.
Request Body
| Field | Type | Description |
|---|---|---|
| code required | string | Code to execute (max 100KB) |
| language optional | string | python, javascript, bash, r. Default: python |
| timeout optional | number | 1–300 seconds |
| files optional | object | Additional files for the sandbox, keyed by filename |
Example
POST /api/code/run
Content-Type: application/json
{
"code": "import math\nprint(f'Pi to 10 decimals: {math.pi:.10f}')",
"language": "python"
}
{
"service": "code-run",
"success": true,
"data": {
"exit_code": 0,
"stdout": "Pi to 10 decimals: 3.1415926536\n",
"stderr": ""
}
}
/api/transcribe
$0.10
Audio transcription with speaker identification using Deepgram Nova-3.
Request Body
| Field | Type | Description |
|---|---|---|
| audio_url required* | string | URL to audio file |
| audio_base64 required* | string | Base64-encoded audio (max 50MB) |
| audio_mimetype optional | string | Required with base64, e.g. audio/mp3 |
| language optional | string | Language code. Default: en |
| diarize optional | boolean | Speaker identification. Default: true |
| model optional | string | nova-3 or whisper-large |
* Provide either audio_url or audio_base64, not both.
Example Response
{
"service": "transcribe",
"data": {
"text": "Hello, welcome to the meeting. Today we'll discuss...",
"segments": [
{ "text": "Hello", "start": 0.0, "end": 0.5, "speaker": 0, "confidence": 0.98 }
],
"speakers": [{ "id": 0, "segments_count": 12 }, { "id": 1, "segments_count": 8 }],
"duration_seconds": 145.2,
"language": "en",
"model": "nova-3"
}
}
Storage
/api/ipfs/pin
$0.01
Pin JSON, files, or URLs to IPFS for decentralized storage. Powered by Pinata.
Request Options
Option 1: File upload
Send as multipart/form-data with a file field. Max 100MB.
Option 2: JSON data
Send { "json": { ... }, "name": "optional-name" } as JSON body.
Option 3: Pin from URL
Send { "url": "https://...", "name": "optional-name" } as JSON body.
Example
POST /api/ipfs/pin
Content-Type: application/json
{ "json": { "name": "Test", "value": 42 }, "name": "my-data" }
{
"service": "ipfs-pin",
"data": {
"cid": "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtensgqas7y",
"name": "my-data",
"size": 28,
"gateway_url": "https://gateway.pinata.cloud/ipfs/bafkrei..."
}
}
/api/ipfs/get
$0.001
Fetch files from IPFS by CID via Pinata gateway.
Parameters
| Name | Type | Description |
|---|---|---|
| cid required | string | IPFS Content Identifier |
Response
For JSON content: returns { service, cid, data }. For binary content: streams the raw file with appropriate Content-Type.
Error Handling
All errors return JSON with an error field.
| Status | Meaning |
|---|---|
| 400 | Missing or invalid parameters |
| 402 | Payment required — check PAYMENT-REQUIRED header |
| 404 | Resource not found |
| 408 | Execution timeout (code execution) |
| 500 | Internal server error |
| 502 | Upstream provider error or not configured |
{
"error": "Missing required parameter: ids",
"details": "Provide comma-separated CoinGecko coin IDs"
}