SDK & CLI usage

Routic provides a drop-in replacement for the OpenAI API. You can use the official OpenAI SDKs for Python and Node.js, or interact via CLI tools like curl.

Python (OpenAI SDK)

Installation

pip install openai

Basic usage

from openai import OpenAI

client = OpenAI(
    base_url="https://api.routic.ai/v1",
    api_key="sk-xxxxxxxx",  # your Routic API Key
)

response = client.chat.completions.create(
    model="deepseek-v3",
    messages=[{"role": "user", "content": "Hello, world!"}],
)

print(response.choices[0].message.content)

Streaming

stream = client.chat.completions.create(
    model="deepseek-v3",
    messages=[{"role": "user", "content": "Tell me a joke"}],
    stream=True,
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Tool calls (function calling)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "City name"},
                },
                "required": ["city"],
            },
        },
    }
]

response = client.chat.completions.create(
    model="deepseek-v3",
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
    tools=tools,
)

JSON mode

response = client.chat.completions.create(
    model="deepseek-v3",
    messages=[{"role": "user", "content": "List 3 countries with their capitals"}],
    response_format={"type": "json_object"},
)

Node.js (OpenAI SDK)

Installation

npm install openai
# or
yarn add openai
# or
pnpm add openai

Basic usage

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.routic.ai/v1",
  apiKey: "sk-xxxxxxxx", // your Routic API Key
});

const response = await client.chat.completions.create({
  model: "deepseek-v3",
  messages: [{ role: "user", content: "Hello, world!" }],
});

console.log(response.choices[0].message.content);

Streaming

const stream = await client.chat.completions.create({
  model: "deepseek-v3",
  messages: [{ role: "user", content: "Tell me a joke" }],
  stream: true,
});

for await (const chunk of stream) {
  if (chunk.choices[0]?.delta?.content) {
    process.stdout.write(chunk.choices[0].delta.content);
  }
}

Tool calls (function calling)

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

const response = await client.chat.completions.create({
  model: "deepseek-v3",
  messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
  tools,
});

CLI (curl)

Basic request

curl -sS "https://api.routic.ai/v1/chat/completions" \
  -H "Authorization: Bearer $ROUTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v3",
    "messages": [{"role": "user", "content": "Hello, world!"}]
  }'

Streaming with curl

curl -sS "https://api.routic.ai/v1/chat/completions" \
  -H "Authorization: Bearer $ROUTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v3",
    "messages": [{"role": "user", "content": "Tell me a joke"}],
    "stream": true
  }'

List available models

curl -sS "https://api.routic.ai/v1/models" \
  -H "Authorization: Bearer $ROUTIC_API_KEY"

Environment variables

Set your API key as an environment variable to avoid hardcoding:

# Linux/macOS
export ROUTIC_API_KEY="sk-xxxxxxxx"

# Windows (PowerShell)
$env:ROUTIC_API_KEY = "sk-xxxxxxxx"

Then reference it in your code:

import os
client = OpenAI(
    base_url="https://api.routic.ai/v1",
    api_key=os.environ.get("ROUTIC_API_KEY"),
)

Error handling

All SDKs follow the same error pattern:

from openai import OpenAI, APIError, AuthenticationError, RateLimitError

client = OpenAI(
    base_url="https://api.routic.ai/v1",
    api_key="sk-xxxxxxxx",
)

try:
    response = client.chat.completions.create(
        model="deepseek-v3",
        messages=[{"role": "user", "content": "Hello"}],
    )
except AuthenticationError as e:
    print(f"Invalid API key: {e}")
except RateLimitError as e:
    print(f"Rate limited, retry later: {e}")
except APIError as e:
    print(f"API error: {e}")

For HTTP-level error codes, see HTTP semantics & error payloads.


Smart routing

Use routing aliases to let Routic pick the best model for a capability:

response = client.chat.completions.create(
    model="auto/reasoning",  # Routic picks the best reasoning model
    messages=[{"role": "user", "content": "Solve this math problem..."}],
)

Available aliases:

  • auto/chat — best general-purpose chat model
  • auto/reasoning — best reasoning model

Thinking mode

Enable extended thinking for reasoning models:

response = client.chat.completions.create(
    model="deepseek-r1",
    messages=[{"role": "user", "content": "Exprove the square root of 2 is irrational"}],
    extra_body={"thinking": {"type": "enabled", "budget_tokens": 1024}},
)

See also