Use XiuRouter with an OpenAI-compatible SDK by setting its Base URL to https://router-api.xiu.ai/v1, providing a dedicated XiuRouter API key, and copying an exact model ID from the current catalog.
Before you start
- Create a dedicated key using the quickstart.
- Copy a model ID from Models and pricing.
- Install the OpenAI SDK used by your project.
Keep the key out of source files, lockfiles, frontend bundles, and Git. These examples read environment variables:
export XIUROUTER_API_KEY="YOUR_XIUROUTER_API_KEY"
export XIUROUTER_MODEL="YOUR_MODEL_ID"Python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["XIUROUTER_API_KEY"],
base_url="https://router-api.xiu.ai/v1",
)
response = client.chat.completions.create(
model=os.environ["XIUROUTER_MODEL"],
messages=[
{"role": "user", "content": "Reply only: Python SDK connected"}
],
)
print(response.choices[0].message.content)JavaScript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.XIUROUTER_API_KEY,
baseURL: "https://router-api.xiu.ai/v1",
});
const response = await client.chat.completions.create({
model: process.env.XIUROUTER_MODEL,
messages: [
{ role: "user", content: "Reply only: JavaScript SDK connected" },
],
});
console.log(response.choices[0].message.content);Common parameters
| Parameter | Purpose | Limits |
|---|---|---|
model |
Select the model | Required; copy the exact ID from the catalog or model list |
messages |
Supply conversation messages | Used by Chat Completions; roles and multimodal content depend on the model |
max_completion_tokens |
Limit output tokens | Preferred for newer models; some upstreams accept only max_tokens |
temperature, top_p |
Control sampling | Reasoning models may ignore or reject some sampling options |
stop |
Set stop sequences | Depends on model support |
tools, tool_choice |
Declare tools | Both the model and upstream must support tool calling |
response_format |
Request JSON or JSON Schema | Requires structured-output support |
stream |
Enable streaming | Use the direct API domain and parse SSE |
stream_options.include_usage |
Include usage near the end of a stream | Only compatible upstreams return it |
reasoning_effort |
Set reasoning effort | Only for reasoning models that accept the field |
The gateway can parse these fields without every model accepting them. Keep the original error and request ID, remove optional parameters, then add them back one at a time.
Verify the result
- Confirm that the program receives complete text, rather than only passing authentication.
- In XiuRouter usage logs, check the model, service group, tokens, cost, and status.
- Record the
x-oneapi-request-idresponse header for troubleshooting.
Troubleshoot
| Symptom | Next step |
|---|---|
| Requests still go to the official provider | Check Python’s base_url or JavaScript’s baseURL |
401 Invalid token |
Confirm that the running process can read XIUROUTER_API_KEY |
| Model unavailable | Fetch GET /v1/models again and check the key’s group and model scope |
| Parameter rejected | Keep only model and minimal messages, then retry |
| Text works but tools or JSON fail | Test tools, structured output, and streaming separately |
Sources and review date
The Chat Completions route, request fields, and OpenAI Python/JavaScript custom Base URL settings were reviewed on 2026-08-19. This translation preserves that review date. Verify parameter support with the actual target model.