Skip to content

OpenAI SDK compatibility

OxyRouter implements the OpenAI chat completions API. The official SDKs work by changing the base URL and API key.

import os
from 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)
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)
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)

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]

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.

  • Set the SDK’s base URL to https://api.oxyrouter.in/v1 and keep the default paths.
  • Pick model IDs from GET /v1/models. Do not reuse another provider’s internal model names.
  • Keep max_tokens or max_completion_tokens set, 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-Key header when your SDK supports custom headers, so retries do not double-charge.
  • Read X-Request-ID from responses and log it for support.

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.