DEV Community

Cover image for How to switch your OpenAI SDK to Flatkey by changing one base_url
Hunter G
Hunter G

Posted on

How to switch your OpenAI SDK to Flatkey by changing one base_url

If you've built anything on the OpenAI SDK, you already know the lock-in feeling: your code speaks one provider's dialect, and the day you want to try Claude, Grok, Gemini, DeepSeek, or Kimi, you're staring down a rewrite — new SDK, new auth, new request shapes.

Here's the thing: you don't have to rewrite anything. If a gateway is OpenAI-compatible, switching to it is a one-line change — you point the same OpenAI SDK at a different base_url, and every model becomes a string you can swap.

This post walks through doing exactly that with Flatkey (an OpenAI-compatible gateway), in Python, Node, and curl. Same code you already have. One base URL. Every official model behind one key.

The whole change, in one line

You keep the OpenAI SDK. You change two things: the base_url and the API key.

base_url = https://router.flatkey.ai/v1
Enter fullscreen mode Exit fullscreen mode

That's it. Everything downstream — chat.completions.create, streaming, tools/function calling, the response shape — stays the same, because the endpoint is OpenAI-compatible.

Python (openai SDK)

Before — vanilla OpenAI:

from openai import OpenAI

client = OpenAI(api_key="sk-openai-...")

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

After — the exact same code, one base_url:

from openai import OpenAI

client = OpenAI(
    api_key="fk-...",                          # your Flatkey key
    base_url="https://router.flatkey.ai/v1",   # the only real change
)

resp = client.chat.completions.create(
    model="gpt-5-mini",                        # or swap the model, see below
    messages=[{"role": "user", "content": "Say hi in one word."}],
)
print(resp.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Nothing else moves. Streaming, tools=[...], response_format, max_tokens — all the same calls you already wrote.

Node (openai SDK)

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.FLATKEY_API_KEY,
  baseURL: "https://router.flatkey.ai/v1",   // the only change
});

const resp = await client.chat.completions.create({
  model: "claude-opus-4-5",
  messages: [{ role: "user", content: "Say hi in one word." }],
});
console.log(resp.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

curl

curl https://router.flatkey.ai/v1/chat/completions \
  -H "Authorization: Bearer $FLATKEY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v3",
    "messages": [{"role": "user", "content": "Say hi in one word."}]
  }'
Enter fullscreen mode Exit fullscreen mode

The payoff: switching models is now just a string

Because you're behind one gateway, changing models isn't a rewrite — it's editing the model field:

for model in ["gpt-5-mini", "claude-opus-4-5", "deepseek-v3", "kimi-k2.6"]:
    resp = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": "One word: your name."}],
    )
    print(model, "", resp.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Same code, four providers. This is the actual point of a gateway: the day a new model tops the charts, you try it by changing a string — not by onboarding a new SDK.

Coding agents: point Claude Code / Codex at it too

The same trick works for coding agents that speak the OpenAI protocol. Set the base URL and key via environment (exact env var names depend on the agent), and you can drive Claude, GPT, or others from inside your existing agent workflow without maintaining a separate integration per provider.

One caveat worth knowing

"OpenAI-compatible" covers the endpoints you use 95% of the time — chat completions, streaming, tool calls, embeddings, images. Where providers expose something genuinely provider-specific (an exotic param, a non-OpenAI endpoint), that's the 5% you'd handle directly. For everyday chat + agent work, the one-line swap is all you need.

A note on why I bothered: verifiable pricing

The reason I use a gateway at all is that I don't want to bet my stack on one model — new ones ship every few months. But most gateways hide how they mark up model costs; you can't see the multiplier you're paying.

Flatkey publishes the exact formula (model_ratio × group multiplier × a fixed base) and a machine-readable pricing endpoint, so you can verify every price yourself before you commit:

curl https://router.flatkey.ai/api/pricing
Enter fullscreen mode Exit fullscreen mode

If a gateway can't show you its pricing math, that's worth being suspicious about. Being able to curl it is the bar.

TL;DR

  • Keep your OpenAI SDK. Set base_url=https://router.flatkey.ai/v1 and your key. Done.
  • Switching models = editing the model string, not rewriting.
  • Verify the pricing before you trust it: router.flatkey.ai/api/pricing.

Don't marry a single model. One base URL, and you can switch on a dime.

Top comments (0)