Keeping your API key in an environment variable on Linux, macOS and Windows

Coding with AIChoosing a Tool & Fixing Errors·· 19 views· 3 min read

An environment variable keeps the key out of your configuration files. That way the key is not written in config.toml or opencode.jsonc, and it does not end up in a repository by mistake. In our examples the variable is called INTERWEB_API_KEY, and that is the name Codex, OpenCode and Kilo Code look for.

Linux

For the terminal you have open, type (with your key in quotes):

export INTERWEB_API_KEY="prod_..."

To have the variable in every new terminal, add the same line at the end of ~/.bashrc:

echo 'export INTERWEB_API_KEY="prod_..."' >> ~/.bashrc

Then open a new terminal.

macOS

Since macOS 10.15 (Catalina), Terminal uses zsh. So the file is ~/.zshrc:

echo 'export INTERWEB_API_KEY="prod_..."' >> ~/.zshrc

Close Terminal and open it again. To use the key only in the terminal you have open, without saving it, export is enough, as on Linux.

Windows

In Command Prompt or PowerShell:

setx INTERWEB_API_KEY "prod_..."

setx saves the variable in your Windows account, but only for the windows you open afterwards: the window where you typed the command does not see it. Close the terminal and the tool (VS Code, for example) and open them again.

To use the key only in the PowerShell window you have open, without saving it:

$env:INTERWEB_API_KEY = "prod_..."

This is lost when you close the window.

Check that the variable works

Ask the API for the key details. It spends no credits and shows straight away whether the variable is right.

On Linux and macOS:

curl "https://api.interweb.co.mz/v1/token" -H "Authorization: Bearer $INTERWEB_API_KEY"

In Windows PowerShell:

curl.exe "https://api.interweb.co.mz/v1/token" -H "Authorization: Bearer $env:INTERWEB_API_KEY"

If the answer is 401 unauthorized, the variable is empty or holds the wrong key. On Windows, the usual cause is a window opened before setx.

Which tools read the variable

Tool How it uses the key
Codex CLI env_key = "INTERWEB_API_KEY" in ~/.codex/config.toml. It reads the variable at start.
OpenCode "apiKey": "{env:INTERWEB_API_KEY}" in opencode.jsonc. If the variable does not exist, the value is empty and the API answers 401.
Kilo Code (command line) {env:INTERWEB_API_KEY} in the global kilo.jsonc, in ~/.config/kilo/. A project file does not read variables.
Claude Code ANTHROPIC_AUTH_TOKEN="$INTERWEB_API_KEY" in the terminal. In ~/.claude/settings.json the key is written out, because Claude Code does not read variables in that file.
Aider It uses its own variables, OPENAI_API_BASE and OPENAI_API_KEY.
Cursor, Cline and Kilo Code in VS Code You paste the key in the tool's settings.
Changed your key? Run the same command with the new key and open the terminals again. If the old one was exposed, revoke it and see what to do, in the right order.

Want to code with All in One AI models? Visit the Coding with AI page.

See Coding with AI

SEE ALSO

Before you set up a coding tool

An API key has leaked

Using Claude Code with All in One AI

OpenCode with All in One AI

Coding with AI in All in One AI

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $5.36/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.