OpenAI SDK compatibility
OxyRouter implements the OpenAI chat completions API. The official SDKs work by changing the base URL and API key.
Python
Section titled “Python”import osfrom openai import OpenAI
client = OpenAI( api_key=os.environ["OXYROUTER_API_KEY"], base_url="https://api.oxyrouter.in/v1",)
completion = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello"}],)
print(completion.choices[0].message.content)Node.js
Section titled “Node.js”import OpenAI from 'openai'
const client = new OpenAI({ apiKey: process.env.OXYROUTER_API_KEY, baseURL: 'https://api.oxyrouter.in/v1',})
const completion = await client.chat.completions.create({ model: 'gpt-4o-mini', messages: [{ role: 'user', content: 'Hello' }],})
console.log(completion.choices[0].message.content)Streaming
Section titled “Streaming”stream = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Count to five."}], stream=True,)
for chunk in stream: delta = chunk.choices[0].delta.content if delta: print(delta, end="", flush=True)const stream = await client.chat.completions.create({ model: 'gpt-4o-mini', messages: [{ role: 'user', content: 'Count to five.' }], stream: true,})
for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content ?? '')}Tool calling
Section titled “Tool calling”Tools are declared exactly as in the OpenAI API. Send the assistant’s
tool_calls back followed by one tool message per result.
tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get current weather for a city", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, }, }]
response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Weather in Pune?"}], tools=tools,)
tool_call = response.choices[0].message.tool_calls[0]Error handling
Section titled “Error handling”Errors use the same envelope as OpenAI. The SDK raises an error with the HTTP status, code, and message.
from openai import APIStatusError
try: completion = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello"}], )except APIStatusError as error: print(error.status_code) print(error.body)Common cases:
| Status | Meaning |
|---|---|
401 |
Invalid or expired API key |
402 |
Wallet balance or spending cap prevents the request |
403 |
Missing scope or model not allowed |
404 |
Unknown or unavailable model |
429 |
Rate or concurrency limit |
502 / 503 |
Upstream or platform failure |
See Errors and limits for codes and retry guidance.
Configuration tips
Section titled “Configuration tips”- Set the SDK’s base URL to
https://api.oxyrouter.in/v1and keep the default paths. - Pick model IDs from
GET /v1/models. Do not reuse another provider’s internal model names. - Keep
max_tokensormax_completion_tokensset, but never both. A cap also makes spending predictable. - Disable SDK-level request retries for streaming, or set a single retry with a timeout; a stream cannot be replayed mid-flight.
- For non-streaming requests, add an
Idempotency-Keyheader when your SDK supports custom headers, so retries do not double-charge. - Read
X-Request-IDfrom responses and log it for support.
Unsupported OpenAI endpoints
Section titled “Unsupported OpenAI endpoints”This reference covers chat completions and model discovery. Responses,
embeddings, audio, images, and other OpenAI endpoints are not part of the
documented OxyRouter API surface. Calling an undocumented path may return 404.