Skip to main content
This guide shows you how to point the official OpenAI SDK at the to11 gateway. Set the base_url to the gateway, authenticate to to11 with the x-to11-authorization header, and keep passing your provider key as the SDK’s api_key — the rest of your code stays the same.

Python

Install the SDK if you haven’t already:
pip install openai
Set the base_url to your gateway:
from openai import OpenAI

client = OpenAI(
    base_url="https://gw.to11.ai/v1",
    api_key="<your OpenAI API key>",  # provider credential, forwarded upstream
    default_headers={
        "x-to11-authorization": "Bearer <your to11 API key>",  # to11 auth — Settings → API keys
        "x-to11-project-id": "<your project id>",
    },
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello from to11!"}],
    max_tokens=256,
)
print(response.choices[0].message.content)

TypeScript / Node.js

npm install openai
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://gw.to11.ai/v1",
  apiKey: "<your OpenAI API key>", // provider credential, forwarded upstream
  defaultHeaders: {
    "x-to11-authorization": "Bearer <your to11 API key>", // to11 auth — Settings → API keys
    "x-to11-project-id": "<your project id>",
  },
});

const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Hello from to11!" }],
  max_tokens: 256,
});
console.log(response.choices[0].message.content);

Streaming

Add stream: true to receive the response incrementally. The gateway streams chunks back as they arrive from the provider, and the call is traced the same as a non-streaming one.
stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Count to 10"}],
    stream=True,
)
for chunk in stream:
    content = chunk.choices[0].delta.content
    if content:
        print(content, end="", flush=True)

Cross-format routing

You can route OpenAI-format requests to Anthropic models. The gateway translates the request and returns the response in OpenAI format — your code doesn’t change:
# Same OpenAI SDK, but targeting an Anthropic model
response = client.chat.completions.create(
    model="claude-sonnet-4-6",
    messages=[{"role": "user", "content": "Hello from to11!"}],
    max_tokens=256,
)
System messages are automatically extracted and forwarded as Anthropic’s top-level system field. Tool definitions are translated between formats.

Tool calling

Tool calling works identically to direct OpenAI usage:
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
    tools=[{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                },
                "required": ["location"],
            },
        },
    }],
)
If this request is routed to an Anthropic model, the gateway translates tool definitions to Anthropic’s input_schema format automatically.

Environment variable configuration

If you prefer not to hard-code the base URL, use the OPENAI_BASE_URL environment variable:
export OPENAI_BASE_URL=https://gw.to11.ai/v1
# No base_url needed — the SDK reads OPENAI_BASE_URL. Still pass the to11 auth header.
client = OpenAI(
    api_key="<your OpenAI API key>",
    default_headers={"x-to11-authorization": "Bearer <your to11 API key>"},
)