Open account

Qwen Code OPENAI_API_KEY: Set It on Mac, Linux, Windows

The qwen code openai_api_key variable is the environment variable that OpenAI-style tools read to find your API key, and its companion OPENAI_BASE_URL tells them which server to call. Setting both takes seconds, but the same few mistakes break it again and again.

Last updated

A brass key linked by light beams to a glass terminal pane above a row of switches, representing environment variables
per 1M input tokens
$0.15
Output tokens / 1M
$0.50
token context
100,000
trial credit
$0.50
requests per minute
300

What the two variables do

Environment variables are named values that the operating system passes to every program you start from a shell. Tools built on the OpenAI client libraries look for two of them at startup. OPENAI_API_KEY is sent as the bearer token in the Authorization header. OPENAI_BASE_URL replaces the default server address, which is how the same tool can talk to any OpenAI-compatible service. Many coding agents also read OPENAI_MODEL for the model name.

For this API the values are:

  • OPENAI_API_KEY: the key from the API key page
  • OPENAI_BASE_URL: https://api.qwencodeapikey.com/v1
  • OPENAI_MODEL: uncensored

Older libraries and some tools use OPENAI_API_BASE for the address. If a tool ignores the first name, check its documentation for the second.

macOS and Linux

For the current terminal session only:

export OPENAI_API_KEY="YOUR_KEY"
export OPENAI_BASE_URL="https://api.qwencodeapikey.com/v1"
export OPENAI_MODEL="uncensored"

To make them permanent, append the same lines to your shell's startup file. Which file depends on the shell: ~/.zshrc for zsh, which is the default on modern macOS, or ~/.bashrc for bash on most Linux systems. Then reload it:

echo 'export OPENAI_API_KEY="YOUR_KEY"' >> ~/.zshrc
source ~/.zshrc

The word export matters. Without it the variable exists only inside the shell and child programs never see it. This is the most common reason a tool says the key is missing even though echo $OPENAI_API_KEY prints it.

Windows

PowerShell, current session

$env:OPENAI_API_KEY = "YOUR_KEY"
$env:OPENAI_BASE_URL = "https://api.qwencodeapikey.com/v1"

Persistent for your user account

The setx command writes the variable to your user environment. It affects only terminals opened afterwards, never the one you typed it in:

setx OPENAI_API_KEY "YOUR_KEY"
setx OPENAI_BASE_URL "https://api.qwencodeapikey.com/v1"

Close the terminal and open a new one before testing. In Command Prompt, use set for the session and do not wrap the value in quotes, because cmd keeps the quote characters as part of the value. In PowerShell the quotes are needed. Under WSL, Windows variables are not shared by default, so set them inside the Linux shell with the macOS and Linux steps.

Check the variable without leaking it

Printing a full key into a terminal, a screen share or a log is how keys escape. Print the length and the first few characters instead:

echo "${#OPENAI_API_KEY} chars, starts with ${OPENAI_API_KEY:0:4}"

A length that is off by one often means a trailing newline or space got pasted in. Then confirm the key works against the server with a real request:

curl -s https://api.qwencodeapikey.com/v1/models \
  -H "Authorization: Bearer $OPENAI_API_KEY"

A JSON list of models means the key and URL are right. A 401 means the key is wrong or stale, and a 404 usually means the base URL is missing its /v1 suffix.

Using the variables from code

The official OpenAI SDKs pick both variables up on their own, so your code does not need the key written into it. In Python:

from openai import OpenAI

# Reads OPENAI_API_KEY and OPENAI_BASE_URL from the environment,
# for example OPENAI_BASE_URL=https://api.qwencodeapikey.com/v1
client = OpenAI()

r = client.chat.completions.create(
    model="uncensored",
    messages=[{"role": "user", "content": "Say hello in one short sentence."}],
)
print(r.choices[0].message.content)

If you want the code to be explicit and fail loudly when the variable is absent, pass the values yourself:

import OpenAI from "openai";

if (process.env.OPENAI_API_KEY === undefined) {
  throw new Error("OPENAI_API_KEY is not set");
}

const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: "https://api.qwencodeapikey.com/v1",
});

const r = await client.chat.completions.create({
  model: "uncensored",
  messages: [{ role: "user", content: "Say hello." }],
});
console.log(r.choices[0].message.content);

Per-project keys and CI

One shell-wide key is fine for experiments. Once you have several projects, a per-project setup is safer.

  • A per-directory loader. Tools such as direnv read a file (an .envrc by default) when you enter a directory and unload it when you leave, so each project can use its own key and model.
  • A separate key per machine or app. Accounts that have topped up can hold up to 5 keys and give them labels, for example one for a laptop and one for a server. Revoking one leaves the others working.
  • CI secrets. In a build pipeline, store the key in the platform's secret store and expose it as OPENAI_API_KEY for the job. Do not echo it in build logs, and do not pass it on a command line where process lists can show it.

When two keys coexist, the symptom of mixing them up is a request that works on one machine and fails with 401 on another. Print the first four characters on both and compare.

Common mistakes and fixes

  • Set in the wrong terminal. A new tab or window starts without your session-only exports. Add them to the profile file.
  • App launched from the dock or start menu. Graphical launchers do not read shell profile files, so an editor started that way may not see the variable. Start it from a terminal or set the variable in the editor's own settings.
  • A .env file nobody loads. A file named .env does nothing unless the tool or your code reads it. Check the tool's help, or use a loader such as python-dotenv in your scripts.
  • Shadowing. A project .env or a profile line further down can override your value. Print the variable in the exact terminal that launches the tool.
  • Key committed to Git. Add .env to .gitignore before the first commit. If a key has reached a repository, reissue it. Regeneration on the key page is self-service, and the old key stops working at once.

Accounts that have topped up can hold up to 5 keys with labels and revoke any of them. That lets you give each project or machine its own key and cut one off without touching the rest. The production checklist for API keys lists storage habits for deployed apps, and the docs show the request format.

If it still fails

Work through the layers in order: does curl succeed with the variable, does the tool print the variable it sees, and does the tool use the model name uncensored. A 429 response is a limit, not a setup error. A full key allows 8 concurrent requests and 300 requests per minute, and the response headers X-RateLimit-Limit-Requests and X-RateLimit-Limit-Concurrency report what applies to yours. The pricing page covers credit, and a new account can try the API with a $0.50 trial. This is a hosted open-weight model from an independent provider, not the official Qwen service.

Frequently asked questions

Do I need OPENAI_BASE_URL as well as OPENAI_API_KEY?

Yes, if you want the tool to call this API. Without it the tool sends your key to its default OpenAI address, where it will not work.

Why does my tool say the key is missing after I set it?

You probably set it without export, in a different terminal, or it was launched from a desktop icon that never read your shell profile. Set it in the terminal that starts the tool.

Is it safe to store the key in a .env file?

It is common practice if the file stays out of Git and has restricted permissions. Reissue the key if the file is ever exposed.

How do I set the variable permanently on Windows?

Run setx OPENAI_API_KEY with your key in PowerShell or Command Prompt, then open a new terminal. The current window will not see the change.

Get your API key now

Sign up, copy the key, update the base URL. That is all you need.

Start with trial credit

Provide email and password. Your key appears instantly.

Key takeaways

  • OPENAI_API_KEY holds the key, OPENAI_BASE_URL holds the server address.
  • A variable set in one terminal is invisible to other terminals and to apps launched from the desktop.
  • Never print or commit the full key; check only its first few characters.
  • Reissue the key on the API key page if it ever leaks.