How to Set OPENAI_BASE_URL, and How It Differs From OPENAI_API_BASE
OPENAI_BASE_URL is the environment variable that OpenAI's official Python and Node SDKs read from 1.x onward. It replaces the default https://api.openai.com/v1. The older OPENAI_API_BASE corresponds to openai.api_base from the 0.x era, and current official SDKs no longer rely on it. Once it is set, any program that creates its client the default way will send requests to the address you specify, with no code changes.
This post covers three things: the difference between the two variable names, how to set it on three kinds of systems, and how to confirm it actually took effect. Whether a specific tool reads this variable should be checked against that tool's official documentation, because this kind of behavior changes between versions.
What is the difference between OPENAI_BASE_URL and OPENAI_API_BASE?
| Variable | Origin | Status |
|---|---|---|
| OPENAI_BASE_URL | Official OpenAI SDKs, 1.x onward | Used by current official SDKs |
| OPENAI_API_BASE | The 0.x Python library; some third-party frameworks kept using it | Not read by current official SDKs; support depends on the framework |
A simple rule: if you installed the openai>=1.0 Python package or the official Node package, use OPENAI_BASE_URL. If your project is pinned to an old version such as openai==0.28, or a framework's documentation explicitly mentions OPENAI_API_BASE, follow that documentation. Setting both causes no errors; each one is simply read by different code.
openai_api_base also shows up as a function parameter, for example in the constructors of some frameworks. A parameter name and an environment variable name are two different things, so don't conflate them.
How should the value be written?
The value is the API root path, and it usually needs to include /v1:
https://api.openai.com/v1
The SDK appends paths such as /chat/completions or /responses. There are three common mistakes:
- Leaving out
/v1, so the request goes to a path that doesn't exist and returns 404. - Adding
/chat/completionsyourself, so the SDK appends it again and the path is duplicated. - Trailing slashes are handled differently by different services. If you're unsure, copy the form given in the provider's documentation.
If you use a third-party gateway that is compatible with the OpenAI format, replace the value with the address in its documentation, and use an API key issued by that provider. The key and the address must match. Sending an OpenAI key to a different address will return 401 and also risks leaking the key.
How do I set it on each platform?
macOS / Linux (bash, zsh)
Temporary, for the current terminal only:
export OPENAI_BASE_URL="https://example.com/v1"
export OPENAI_API_KEY="sk-xxxx"
To make it permanent, add the lines to ~/.zshrc or ~/.bashrc, then run source ~/.zshrc.
To apply it to a single command only, put it in front of the command:
OPENAI_BASE_URL="https://example.com/v1" python app.py
Windows PowerShell
Current session:
$env:OPENAI_BASE_URL = "https://example.com/v1"
Persist it at the user level:
setx OPENAI_BASE_URL "https://example.com/v1"
setx does not affect windows that are already open, so you need to start a new terminal. For the graphical interface and details on storing a key, see How to set an environment variable for your API key on Windows.
Docker and .env files
In Docker, use -e OPENAI_BASE_URL=... or the environment section of compose. A .env file is not loaded automatically by Python. Your program has to call a library such as python-dotenv, or compose or your framework has to read it. This is the pitfall beginners hit most often: the file is written, but the process doesn't have the variable.
Which tools read this variable?
Start with the general principle: when an official SDK creates a client and no base_url is passed explicitly, it looks in the environment variables. That leads to the following cases:
| Scenario | Reads it? | Notes |
|---|---|---|
Official Python SDK 1.x, OpenAI() with no arguments | Yes | An explicit base_url= takes precedence |
Official Node SDK, new OpenAI() with no arguments | Yes | Same as above |
base_url hard-coded in the code | No | The parameter takes precedence over the environment variable |
| CLI tools such as Codex | Check the official docs | These tools often have their own config files, which may take precedence over environment variables |
| Editor-type tools such as Cursor and Cline | Usually not | Typically configured in a settings UI; see the links below |
| Claude Code | Does not use this variable | It uses variables on the Anthropic side; check the official docs |
For the three ways to do this in Python and how arguments and environment variables take precedence, see How to change base_url in the openai Python library. For configuring endpoints in CLI tools and editors, see Custom API endpoints for Claude Code, Codex, and OpenCode and Cursor custom API configuration.
How do I confirm it actually took effect?
Don't just check whether the program produces output. When the variable doesn't take effect, the program quietly calls the official address and only fails because your key doesn't match. Check in these four steps:
In the same terminal that runs the program, print the variable and confirm it is non-empty:
echo $OPENAI_BASE_URL(in PowerShell, use$env:OPENAI_BASE_URL).Confirm in Python that the process can see it:
import os from openai import OpenAI print(os.environ.get("OPENAI_BASE_URL")) print(OpenAI().base_url)The second line prints the address the SDK will actually use.
Use curl on its own to verify that the address and key work together, which rules out your program. See Send your first LLM request with curl for the syntax.
Use the error code to narrow things down: 401 usually means the key and address don't match, 404 is most likely a malformed path, and connection timeouts or TLS errors point to the network. For exact meanings, rely on the provider's documentation.
The program still connects to the official address after I set it. What are the usual causes?
- The terminal and the runtime environment are not the same. IDEs, systemd services, cron, and Docker containers each have their own environment, and an
exportin your shell has no effect on them. - You ran
setxbut didn't reopen the terminal. - The code passes
base_urlexplicitly, or a framework setting overrides it. - You are using an old SDK or framework that reads OPENAI_API_BASE.
- The variable name is misspelled. Names are case-sensitive, and
OPENAI_BASE_URLuses underscores.
Also, don't put your key in any file that gets committed to a repository. If it does leak, see Your OpenAI API key leaked: what to do for the steps.
Can I change one address and skip the overseas credit card?
If your goal is to pay in RMB from mainland China and keep using your existing tools, changing the endpoint address is a common approach. AGI Plan is a multi-provider LLM aggregation gateway with monthly subscriptions, billed in RMB, with no overseas credit card required, and it is compatible with both the OpenAI and Anthropic API formats. Tools like Claude Code and Codex only need one endpoint address changed, and a single subscription quota can then be used across several capability groups. Some models are marked as available only outside China; see https://agiplan.dev/models for specifics. The service also states that it is not an officially authorized channel.
AGI Plan is a commercial partner of cdnping, and we are disclosing that relationship here. Whether it suits you is your call; we suggest reading How to choose how to access AI APIs first.
For the onboarding steps, see https://agiplan.dev/startup: sign up with an email verification code, choose a plan and create a key, and change one setting.
What to do next
- Run the four checks above and confirm the variable really exists in the environment where your program runs.
- Use curl to verify that the "address + key" pair works.
- Go back to your program, don't pass
base_urlexplicitly, and let the SDK read the environment variable. - If requests are slow or time out occasionally, use the cdnping online speed test to measure the first-connection and round-trip times of various AI endpoints from your browser, and the API status page to see the last 30 days of probes against the official entry points. You can also read Network latency when using AI APIs from China to work out which segment the problem is in.