OpenAI SDK

Full setup for the official Python and Node.js SDKs, including streaming and tool calls.

On this page

Python

bash
pip install openai
python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.apione.ai/v1",
    api_key=os.environ["APIONE_API_KEY"],
    timeout=60.0,
    max_retries=2,
)

resp = client.chat.completions.create(
    model="claude-sonnet-4-6",
    messages=[{"role": "user", "content": "Introduce yourself"}],
)
print(resp.choices[0].message.content)
print(resp.model, resp.usage.total_tokens)

Streaming

python
with client.chat.completions.stream(
    model="claude-sonnet-4-6",
    messages=[{"role": "user", "content": "Write a short poem about caching"}],
) as stream:
    for event in stream:
        if event.type == "content.delta":
            print(event.delta, end="", flush=True)

Async

python
from openai import AsyncOpenAI

client = AsyncOpenAI(
    base_url="https://api.apione.ai/v1",
    api_key=os.environ["APIONE_API_KEY"],
)

async def ask(prompt: str) -> str:
    resp = await client.chat.completions.create(
        model="gemini-3.5-flash",
        messages=[{"role": "user", "content": prompt}],
    )
    return resp.choices[0].message.content

Node.js / TypeScript

bash
npm i openai
javascript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.apione.ai/v1",
  apiKey: process.env.APIONE_API_KEY,
  timeout: 60_000,
  maxRetries: 2,
});

const resp = await client.chat.completions.create({
  model: "claude-sonnet-4-6",
  messages: [{ role: "user", content: "Introduce yourself" }],
});
console.log(resp.choices[0].message.content);

Tool calls

javascript
const tools = [{
  type: "function",
  function: {
    name: "get_weather",
    description: "Current weather for a city",
    parameters: {
      type: "object",
      properties: { city: { type: "string" } },
      required: ["city"],
    },
  },
}];

const first = await client.chat.completions.create({
  model: "gpt-5.6-sol",
  messages: [{ role: "user", content: "What's the weather in Beijing?" }],
  tools,
});

const call = first.choices[0].message.tool_calls?.[0];
// Run your own function, send the result back as role: "tool", then call again
Note

SDK max_retries and gateway failover are two different layers: the gateway swaps providers inside one request, the SDK resends after a failure. Budget your total timeout accordingly.

Contact us

Something unclear, or need a human? Pick the channel that fits your question.