Gemini CLI API key: how to set it and which .env file it reads

Get a key from Google AI Studio, set GEMINI_API_KEY in your shell, start gemini and choose Use Gemini API key. Gemini CLI also loads a .env file on its own, and not only its own .gemini/.env: a plain .env in your project counts too. In a folder you've trusted, every variable in it, your app's other secrets included, then sits in Gemini CLI's environment.

We make fidelius, a Mac app built on iCloud Keychain, with no vault or sync of its own, that keeps API keys out of shell and project files. It comes up at the end; everything before that works without it.

Setting the key

Keys come from Google AI Studio. For one terminal window, this zsh line asks for the key without showing it, so it doesn't end up in your shell history the way export GEMINI_API_KEY="..." would:

$ read -s "GEMINI_API_KEY?Gemini API key: " && export GEMINI_API_KEY$ gemini

In gemini, run /auth and choose Use Gemini API key. Vertex AI uses other variables, such as GOOGLE_CLOUD_PROJECT, and when you switch to it with application default credentials, the docs say to unset both GOOGLE_API_KEY and GEMINI_API_KEY.

To keep the key set, people add the export to ~/.zshrc. The authentication page warns about that: any process launched from that shell can read it. More on shell files in how to set environment variables on a Mac.

Which .env file Gemini CLI reads

Gemini CLI loads environment variables from a .env file on its own. The configuration reference gives the search order:

  1. A .env in the current folder.
  2. If there's none, the parent folders, upward, stopping at the first .env, at the project root (the folder with .git) or at your home folder.
  3. If there's still none, ~/.env.

The authentication page describes the same idea with .gemini/.env files, in your project or in ~/.gemini/.env. Either way, only the first file found is used. Files aren't merged, so a project .env can hide the key you put in your home folder.

Folder trust limits this. Gemini CLI asks whether you trust a folder, and in one you haven't trusted it takes only its own sign-in variables from a project .env, such as GEMINI_API_KEY and GOOGLE_CLOUD_PROJECT.

Your project's .env goes along

The file Gemini CLI finds is usually your app's .env, with your database password, Stripe key and everything else in it. Once you trust the folder, Gemini CLI loads those variables too, apart from a short exclusion list (advanced.excludedEnvVars, by default just DEBUG and DEBUG_MODE). From its environment they pass to the commands it runs, so one env during a session prints them.

Two settings in ~/.gemini/settings.json change this. advanced.ignoreLocalEnv stops Gemini CLI reading generic .env files in the project, and security.environmentVariableRedaction.enabled redacts environment variables that look like secrets. Both are off by default and take effect after a restart:

{
  "advanced": { "ignoreLocalEnv": true },
  "security": { "environmentVariableRedaction": { "enabled": true } }
}

Merge these into the file if it already has advanced or security sections. With project files ignored, put the key in ~/.gemini/.env or in your shell instead.

Where Gemini CLI keeps sign-ins

With Sign in with Google, Gemini CLI caches your credentials locally for later sessions, in your ~/.gemini folder. Treat that folder like a password: keep it out of dotfiles repos and shared backups. An API key you exported or put in a .env file stays wherever you put it.

Where fidelius fits

fidelius keeps your project API keys in the Keychain, grouped by project, so a project needs no .env file for Gemini CLI to find, and Gemini CLI starts without them. With one click it adds instructions telling Gemini CLI to run commands that need a key through accio, as in accio myapp npm test, so the key reaches that command and Gemini CLI works with the key name, as long as it follows those instructions.