Chat completions (Python)
Send an ordered array of messages to chat.completions.create. The response contains choices; usage, when returned, reports token consumption.
This guide uses the official OpenAI client with TheRouter; it is not a TheRouter-owned SDK. The therouter and @therouter/sdk packages previously named here are not currently published in the public package registries.
Install and run
bash
pip install openaiSet THEROUTER_API_KEY on your server, then run:
python
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["THEROUTER_API_KEY"], base_url="https://api.therouter.ai/v1")
completion = client.chat.completions.create(
model="openai/gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
max_tokens=32,
)
print(completion.choices[0].message.content)