How to use the OpenAI SDK with a custom base URL

Point the official OpenAI Python or Node SDK at TokenRex to call GPT, Claude and Gemini models.

Set the base URL

Create the client with your TokenRex key and base URL https://tokenrex.ai/api/public. Every other line of your code stays the same.

You can also set the OPENAI_BASE_URL and OPENAI_API_KEY environment variables instead of editing code.

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="gpt-6-astra",
    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