Skip to navigation

Switch an Existing OpenAI SDK App to a Different Base URL

In short: Switching an OpenAI SDK app to another compatible endpoint means changing the base URL and API key, with env vars or one line of code, plus the model ID.

Published 7 min read
On this page

If your app already uses the official OpenAI SDK, moving it to another OpenAI-compatible endpoint usually means changing two settings: the base URL and the API key. Usually you also change the model name. You don’t need a new SDK or a rewrite. This tutorial shows the change in Python, Node and curl, plus LangChain and the Vercel AI SDK. It also covers the things that tend to break, and how to roll the change out safely.

The examples use One AI API Key’s endpoint, https://api.oneaiapikey.com/v1, and gpt-6.1-sol as the model. The same steps work for any compatible provider.

What a base URL is

The SDK builds every request URL by adding a path to the base URL. The default base is https://api.openai.com/v1, so a chat request goes to https://api.openai.com/v1/chat/completions. Change the base to https://api.oneaiapikey.com/v1 and the same call goes to https://api.oneaiapikey.com/v1/chat/completions. The request body, the response shape and your code don’t change.

The /v1 is part of the base URL for OpenAI-format clients. Most “404 Not Found” errors after a switch come from leaving it out, or from adding it twice.

Step 1: How do I find everything I need to change?

Find everything to change by listing every place the old provider is set up before editing. Hardcoded URLs and model names tend to hide in scripts, tests and config files:

find configuration
# Every place the old provider is configured or a model is named
git grep -n -E "api\.openai\.com|OPENAI_BASE_URL|base_url|baseURL|model\s*[=:]" -- . ':!*.lock'

Step 2: How do I switch with environment variables only?

Setting two environment variables switches an app that creates its client with no arguments, with no code changes. Both official SDKs read two environment variables when you create a client with no arguments: OPENAI_API_KEY and OPENAI_BASE_URL. If your code uses OpenAI() or new OpenAI() with no options, setting the variables is the whole switch:

environment
# macOS / Linux (bash, zsh)
export OPENAI_BASE_URL=https://api.oneaiapikey.com/v1
export OPENAI_API_KEY=sk-your-one ai api key-key

# Windows PowerShell
$env:OPENAI_BASE_URL = "https://api.oneaiapikey.com/v1"
$env:OPENAI_API_KEY  = "sk-your-one ai api key-key"
unchanged code
from openai import OpenAI

# With no arguments the SDK reads OPENAI_API_KEY and OPENAI_BASE_URL.
client = OpenAI()

One caution: don’t put these in your shell profile. Many tools on a developer machine use the OpenAI SDK and read the same variables. If you set them globally, all of those tools switch providers. Put them in the project’s .env file or the deployment’s environment instead.

Step 3: How do I verify with curl before touching the app?

Verify with two curl requests, which tell you whether the key, the URL and the model ID are right. Test these before debugging your own code:

verify
# 1. Does the key work, and which model IDs can it call?
curl -s $OPENAI_BASE_URL/models -H "Authorization: Bearer $OPENAI_API_KEY"

# 2. One real completion
curl -s $OPENAI_BASE_URL/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-6.1-sol", "max_tokens": 50, "messages": [{"role": "user", "content": "Say ok"}]}'

The first call lists the model IDs your key can use. The second should return a short completion with a usage block. If both work, any later failure is in your app’s configuration, not in the account.

Step 4: How do I make it explicit in code?

Pass the base URL and key in code, read from your own variable names. For anything beyond a quick test, pass the settings in code, read from your own variable names. It makes the dependency visible, avoids the global-variable problem above, and lets you change the model without a deploy.

Python (pip install openai)
import os
from openai import OpenAI

# Your own variable names: nothing else on the machine reads them by accident.
client = OpenAI(
    base_url=os.environ.get("LLM_BASE_URL", "https://api.oneaiapikey.com/v1"),
    api_key=os.environ["LLM_API_KEY"],
    timeout=120,       # seconds; reasoning models can think for a while
    max_retries=2,
)
MODEL = os.environ.get("LLM_MODEL", "gpt-6.1-sol")

resp = client.chat.completions.create(
    model=MODEL,
    max_tokens=300,
    messages=[{"role": "user", "content": "Name three uses for a base URL setting."}],
)
print(resp.choices[0].message.content)
print(resp.usage)
Node.js (npm install openai)
import OpenAI from "openai";

// new OpenAI() with no options also reads OPENAI_API_KEY and OPENAI_BASE_URL.
const client = new OpenAI({
  baseURL: process.env.LLM_BASE_URL ?? "https://api.oneaiapikey.com/v1",
  apiKey: process.env.LLM_API_KEY,
  timeout: 120_000, // milliseconds
  maxRetries: 2,
});
const MODEL = process.env.LLM_MODEL ?? "gpt-6.1-sol";

const resp = await client.chat.completions.create({
  model: MODEL,
  max_tokens: 300,
  messages: [{ role: "user", content: "Name three uses for a base URL setting." }],
});
console.log(resp.choices[0].message.content, resp.usage);

AsyncOpenAI in Python takes the same base_url and api_key arguments.

How do I switch LangChain and the Vercel AI SDK?

LangChain and the Vercel AI SDK both accept a base URL, because most frameworks pass it through to the SDK underneath:

LangChain (pip install langchain-openai)
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-6.1-sol",
    base_url="https://api.oneaiapikey.com/v1",
    api_key=os.environ["LLM_API_KEY"],
    max_tokens=300,
)
print(llm.invoke("Say ok").content)
Vercel AI SDK (npm install ai @ai-sdk/openai)
import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

const gateway = createOpenAI({
  baseURL: "https://api.oneaiapikey.com/v1",
  apiKey: process.env.LLM_API_KEY,
});

// .chat() uses /chat/completions, the most widely supported endpoint.
const { text } = await generateText({
  model: gateway.chat("gpt-6.1-sol"),
  prompt: "Say ok",
});

Step 5: Do I need to change the model IDs?

Yes, because model names are not portable between providers. An ID that exists at OpenAI may not exist on another endpoint, and the request will fail with a model error. These are the IDs One AI API Key serves through the OpenAI format today, with current rates per 1M tokens:

Model IDNameInputOutput
stepaudio-3-chat-preview:freeStepAudio 3 Chat Preview$0.00$0.00
step-5:freeStep 5$0.00$0.00
gpt-6-luna:freeGPT-6 Luna$0.00$0.00
nvidia-deepseek-v4-1-flash:freeDeepSeek V4.1 Flash$0.00$0.00
minimaxai-minimax-m3-1-flash:freeMiniMax M3.1 Flash$0.00$0.00
deepseek-v4-flash-0731-thinking:freeDeepSeek V4 Flash 0731 Thinking$0.00$0.00
deepseek-v4-flash-0731:freeDeepSeek V4 Flash 0731$0.00$0.00
deepseek-v4-flash-0731-1:freeDeepSeek V4 Flash 0731$0.00$0.00
beam-1-0:freeBeam 1.0$0.00$0.00
qwen-qwen3.8-27b:freeQwen3.8 27B$0.00$0.00
qwen3.8-27b-uncensored:freeQwen3.8-27B Uncensored$0.00$0.00
dealignai-glm-5.3-flash-uncensored-fp8:freeGLM 5.3 Flash Uncensored$0.00$0.00
nvidia-nemotron-3-ultra-550b-a55b:freeNemotron 3 Ultra 550B$0.00$0.00
z-ai-glm-5.3:freeGLM 5.3$0.00$0.00
deepseek-deepseek-v4.1-flash:freeDeepSeek V4.1 Flash$0.00$0.00
z-ai-glm-5.3-flash:freeGLM 5.3 Flash$0.00$0.00
ernie-5.5-flash-preview:freeERNIE 5.5 Flash Preview$0.00$0.00
glm-4.6v-flash:freeGLM 4.6V Flash$0.00$0.00
glm-4.7-flash:freeGLM 4.7 Flash$0.00$0.00
hy4-flash:freeHy4 Flash$0.00$0.00
dealignai-deepseek-v4.1-flash-uncensored-fp8:freeDeepSeek V4.1 Flash Uncensored$0.00$0.00
dealignai-glm-5.3-uncensored-fp8:freeGLM 5.3 Uncensored$0.00$0.00
moonshotai-kimi-k3.1-pro:freeKimi K3.1 Pro$0.00$0.00
deepseek-v4-flash:freeDeepSeek V4 Flash$0.00$0.00
ernie-5.5-flash-preview-1:freeERNIE 5.5 Flash Preview$0.00$0.00
ernie-5.5-flash-preview-2:freeERNIE 5.5 Flash Preview$0.00$0.00
gemma-4-31b-it:freeGemma 4 31B$0.00$0.00
gemma4-31b:freeGemma 4 31B Alt$0.00$0.00
laguna-xs-2.1:freeLaguna XS 2.1$0.00$0.00
lfm-3-1-7b:freeLFM 3.1 7B$0.00$0.00
longcat-2.5:freeLongCat 2.5$0.00$0.00
laguna-xs-2.1-1:freeLaguna XS 2.1$0.00$0.00
laguna-xs-2.1-uncensored:freeLaguna XS 2.1 Uncensored$0.00$0.00
llama-3.3-70b-instruct:freeLlama 3.3 70B Instruct$0.00$0.00
nemotron-3-nano-omni-30b-a3b-reasoning:freeNemotron 3 Nano Omni 30B Reasoning$0.00$0.00
nemotron-3.5-super-vl:freeNemotron 3.5 Super VL$0.00$0.00
nemotron-3-5-lightning:freeNemotron 3.5 Lightning$0.00$0.00
nvidia-nemotron-3-ultra-550b-a55b-1:freeNemotron 3 Ultra 550B$0.00$0.00
solar-mini4:freeSolar Mini 4$0.00$0.00
nemotron-3-ultra-550b-a55b-uncensored:freeNemotron 3 Ultra 550B Uncensored$0.00$0.00
jais-3-mova-36b-a4b:freeJais 3 Mova 36B$0.00$0.00
nvidia-mistral-large-4-1t-a52b:freeMistral Large 4$0.00$0.00
k2-horizon-375b-a23b-uncensored:freeK2 Horizon 375B Uncensored$0.00$0.00
k2-horizon-375b-a23b:freeK2 Horizon 375B$0.00$0.00
glm-4.6v-flash-1:freeGLM 4.6V Flash$0.00$0.00
glm-4.7-flash-1:freeGLM 4.7 Flash$0.00$0.00
grok-4.7-fast:freeGrok 4.7 Fast$0.00$0.00
roleplay-chat:freeRoleplay Chat$0.00$0.00

Read the model from configuration, as in the examples above, rather than hardcoding it in each call. The next time you switch, it’s a config change.

What tends to break after the switch, and how do you fix it?

The common breakages after a switch are a wrong /v1 in the base URL, a leftover old key, an unknown model ID, an unsupported parameter, a short timeout and a firewall that blocks the new host.

Symptom Likely cause Fix
404 Not Found Base URL without /v1, or with /v1/v1 The base URL ends in /v1 with no trailing path
401 Unauthorized The old OpenAI key is still loaded, often from a shell profile or a CI secret Print the key’s first 6 characters at startup and compare
Model not found An old model ID Use an ID from GET /v1/models
400 on a parameter A parameter the target model doesn’t support Remove it. Reasoning models often want max_completion_tokens instead of max_tokens
Timeouts on long answers Client timeout shorter than generation time Raise the timeout, or stream the response
Connection refused in production Outbound firewall or proxy allows only the old host Add the new API host to the allowlist

Some endpoints are specific to one provider. Files, fine-tuning, batch jobs, Assistants, and audio or image generation are not part of what One AI API Key serves. The API reference lists the endpoints we do support: chat completions, responses, messages, models and embeddings. If your app uses OpenAI-only endpoints, keep a separate client for them on OpenAI and move only the chat traffic.

OpenAI account settings such as organization and project IDs (OPENAI_ORG_ID, OPENAI_PROJECT_ID) belong to your OpenAI account. Unset them for the new client so they aren’t sent to a provider that doesn’t know them.

Step 6: How do I roll it out gradually?

Roll it out one environment at a time, because the switch is just configuration:

  1. Local and staging first. Run your test suite and a small set of real prompts. Compare quality, latency and the usage numbers with what you had before.
  2. Production behind a flag. Keep the old values in the deployment config and switch with an environment variable, so rolling back is a redeploy, not a code change.
  3. Watch the first day. Track error rates by status code and compare spend in the usage log with your estimate.

If you also use coding agents, the same base URL idea applies to them. The Claude Code setup guide and the agent kits cover tool-specific settings.

Checklist

  • The base URL includes /v1 and nothing after it.
  • The key is loaded from a project-level secret, not a shell profile.
  • curl to /models and /chat/completions both work with that key.
  • Every model ID in code and config exists on the new endpoint.
  • OpenAI-only endpoints stay on a separate client.
  • The rollout can be reversed by changing configuration.
More in Tutorials

Try it

One API key for GPT, Grok, DeepSeek, Kimi, GLM, MiniMax, Qwen, Step, Beam, Nemotron, ERNIE, Hunyuan, Gemma, Laguna, LFM, LongCat, Llama, Solar, Jais, Mistral, K2 and Roleplay.

One AI API Key speaks the OpenAI and Anthropic formats, so you change the base URL and the key, not your code. Credit packs start at $10, and every model has a published per-token rate.

New accounts$10 credit on sign-up
Claim $10 credit →