Developer documentation
Code examples for text, image, and speech
Use an official x402 client with the public text, image, and speech request formats.
Page tools
Every client uses the same official service address and payment lifecycle. The payment wrapper must use official x402 packages and locally enforce the payment network, asset, recipient, and spending limits in atomic United States Dollar Coin (USDC) units. Request bodies differ by capability; use the generated guide for the selected endpoint.
TypeScript
Construct paymentFetch with the official @x402/core, @x402/evm, and @x402/fetch client packages. Then pass it to the repository wrapper:
import { OnchainRouterClient } from '@onchain-router/client';
const router = new OnchainRouterClient({
baseUrl: process.env.ONCHAIN_ROUTER_URL!,
paymentFetch,
});
const response = await router.chat({
model: 'gemini-3.6-flash',
messages: [{ role: 'user', content: 'Explain why the sky appears blue.' }],
max_tokens: 1024,
stream: false,
});
const body = await response.json();
console.log(body.choices[0].message.content);
console.log(response.headers.get('x-receipt-id'));The local payment policy must reject any network other than Base mainnet, an incorrect USDC contract, a changed recipient, an unsupported payment scheme, or a maximum above the caller's spending limit.
Python
The maintained repository example accepts an httpx.Client that is already wrapped by the official x402 software development kit for Python:
result = paid_chat(payment_client)
print(result["choices"][0]["message"]["content"])Do not implement Ethereum Improvement Proposal 712 (EIP-712), Permit2, or payment settlement code in your application. Use the official libraries.
Direct Hypertext Transfer Protocol request
An ordinary unpaid request is useful for inspecting the challenge:
curl -i "$ONCHAIN_ROUTER_URL/v1/chat/completions" \
-H "content-type: application/json" \
-H "x-idempotency-key: $(uuidgen)" \
--data '{"model":"gemini-3.6-flash","messages":[{"role":"user","content":"Say hello."}],"max_tokens":512,"stream":false}'Expect Hypertext Transfer Protocol (HTTP) status 402. Do not construct the paid retry by hand. Pass the response to an official x402 buyer client.
Other capabilities
Use the same payment-aware client for every paid route:
- Messages accepts the Anthropic-compatible message shape.
- Image Generations accepts an image model, prompt, size, aspect ratio, and response format.
- Text to Speech accepts text, model, voice, MP3 response format, and optional speed.
- Speech to Text accepts one MP3 either as canonical Base64 JSON or as multipart form data.
Do not assume that a text-specific wrapper method supports media. Send the exact body shown by OpenAPI through the same official payment-aware HTTP client.
