Keeping the key out of opencode.jsonc: {env:...} and setx on Windows

Coding with AIOpenCode·· 4 views· 3 min read

The key should never be written in the OpenCode file. The file only holds the name of a variable, and the key stays in your system. That way you can copy, show or share the file without giving the key to anyone.

How {env:...} works

"apiKey": "{env:MOZOUT_API_KEY}"

When it reads the file, OpenCode replaces {env:MOZOUT_API_KEY} with the value of the MOZOUT_API_KEY variable. If the variable does not exist, it puts an empty value there. The request goes without a key and the API answers 401.

1. Save the key in the variable

On Windows, open Command Prompt or PowerShell and type (with your key in quotes):

setx MOZOUT_API_KEY "prod_..."

setx saves the variable for the windows you open afterwards. Close the terminal and the tool, then open them again.

On Linux and macOS, type in the terminal:

export MOZOUT_API_KEY="prod_..."

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

2. Close and open again

On Windows, this is the step people forget most. setx does not change the windows that are already open: an OpenCode started before it does not see the variable. Close OpenCode and the terminal completely, open a new terminal and only then start OpenCode.

3. Check that the variable exists

In a new terminal. In Windows PowerShell:

echo $env:MOZOUT_API_KEY

In Windows Command Prompt:

echo %MOZOUT_API_KEY%

On Linux and macOS:

echo $MOZOUT_API_KEY

If the key shows, OpenCode sees it too. If nothing shows (or, in Command Prompt, the name itself between percent signs), the variable does not exist in that window.

Another way: {file:...}

OpenCode can also read a value from a file, with {file:path}. The path can be relative to the folder of the configuration file, absolute, or start with ~/. If the file does not exist, the value is empty.

"apiKey": "{file:~/.api-key}"

Keep that file outside the project folder, so it does not end up in Git.

If the key was ever written in the file and the file went to Git, a chat or an e-mail, revoke it and create another. See what to do, in the right order.
Kilo Code is different: {env:...} only works in its global file. See why the key only works in the global kilo.jsonc.

Want to use our models in OpenCode with your key kept safe? Visit the Coding with AI page.

See Coding with AI

SEE ALSO

OpenCode with All in One AI: creating opencode.json from scratch, the key and the models

Sharing an OpenCode setup in your repository without sharing the key

An API key or token has leaked: what to do, in the right order

Why the key only works in the global kilo.jsonc, and not in a project's file

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 $6.60/mo (3-year plan, with coupon)

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