How to call Gemini with the OpenAI SDK

Call Gemini models through the OpenAI-compatible chat endpoint with one key.

Change the model ID

Set the model to a Gemini ID such as gemini-3.8-flash. Requests use the OpenAI chat format; the Google-native format is not supported.

Example

from openai import OpenAI

client = OpenAI(api_key="YOUR_TOKENREX_KEY", base_url="https://tokenrex.ai/api/public")

resp = client.chat.completions.create(
    model="gemini-3.8-flash",
    messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)
Get API keyCost calculatorDocs

Questions

Do I need a separate key for each provider?
No. One TokenRex API key works for every enabled model; change only the model ID.

Related