OpenAI-Compatible API - Switch base_url in 5 Minutes

By TokenVoke Team · Published Mar 5, 2026 · 2 min read
OpenAI compatiblebase_urlquickstart

If a gateway is OpenAI-compatible, migrating is one of the easiest changes you will make: keep your code, swap the base_url and API key, and you are live in about five minutes. This is a step-by-step guide for Python, Node.js, cURL, and common AI tools.

What "OpenAI-compatible" means

It means the gateway implements the same HTTP API as OpenAI - the same endpoints (/v1/chat/completions, etc.), the same request and response shapes, and the same SDKs. You do not rewrite anything; you point the official OpenAI SDK at a different host.

You need two values from your gateway:

  1. Base URL, for example https://api.your-gateway.com/v1
  2. API key, issued in the gateway console

Python (openai SDK)

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_GATEWAY_KEY",
    base_url="https://api.your-gateway.com/v1",
)

resp = client.chat.completions.create(
    model="gpt-5.4-mini",
    messages=[{"role": "user", "content": "Say hi in one sentence."}],
)
print(resp.choices[0].message.content)

To switch models, change model to claude-sonnet-4-6, gemini-3.1-pro, deepseek-v4, and so on.

Node.js (openai SDK)

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.GATEWAY_KEY,
  baseURL: "https://api.your-gateway.com/v1",
});

const resp = await client.chat.completions.create({
  model: "gpt-5.4-mini",
  messages: [{ role: "user", content: "Say hi in one sentence." }],
});
console.log(resp.choices[0].message.content);

cURL

curl https://api.your-gateway.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_GATEWAY_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-mini",
    "messages": [{"role": "user", "content": "Say hi in one sentence."}]
  }'

Environment variables

Most code reads the key and host from env vars, so you often do not touch source at all:

export OPENAI_API_KEY="YOUR_GATEWAY_KEY"
export OPENAI_BASE_URL="https://api.your-gateway.com/v1"

Many SDKs respect OPENAI_BASE_URL automatically.

Popular AI tools

  • Cursor / IDE assistants: set a custom OpenAI base URL and key in settings.
  • Claude Code, Codex, OpenCode and similar CLIs: point the base URL/key to the gateway; pick a supported model id.
  • LangChain / LlamaIndex: pass base_url/openai_api_base and the gateway key to the OpenAI client.

Verify it works

  1. Send one request (the cURL above is fastest).
  2. Confirm you get a normal completion.
  3. Check the gateway console - your request, tokens, and cost should appear.

If you get a 401, re-check the key. If you get a model error, confirm the exact model id is supported by the gateway.

Common pitfalls

  • Forgetting /v1 in the base URL.
  • Mixing keys - use the gateway key, not your old provider key.
  • Wrong model id - gateways use specific ids; copy them from the model list.
  • Hardcoded host - prefer env vars so staging/production can differ.

FAQ

Do I lose any features by switching? For standard chat, tools/function calling, and streaming, no. Confirm support for any niche, provider-specific parameters you rely on.

Can I roll back? Instantly - revert the base URL and key to your previous provider.

How long does migration really take? For a typical app, about five minutes: change two values, send a test request, done.


Get your base URL and key from TokenVoke - see the docs for the full quickstart, or browse the Model Square for supported models and pricing.