API Error: API keys are not supported by this API when .env file exists
What happened?
oAuth2 error appears when running Gemini CLI with API Key auth in a folder which contains .env for a Google Cloud project:
[API Error: API keys are not supported by this API. Expected OAuth2 access token or other authentication credentials that assert a principal. See
https://cloud.google.com/docs/authentication (Status: UNAUTHENTICATED)]
The .env file contains:
GOOGLE_GENAI_USE_VERTEXAI=TRUE
GOOGLE_CLOUD_PROJECT=[PROJECT_ID]
GOOGLE_CLOUD_LOCATION=us-central1
A workaround is to switch to Google Login instead of an API_KEY auth.
What did you expect to happen?
API_KEY auth should work without errors.
Client information
$ gemini /about
CLI Version 0.1.9
Model gemini-2.5-pro
Sandbox no sandbox
OS linux
Auth Method gemini-api-key
GCP Project [PROJECT_ID]
Login information
API key
Anything else we need to know?
No response
cc @scidomino We debugged a similar issue last week. This is one step further, where the .env variables are intentionally present, but should NOT be used for the CLI.
Do you have GEMINI_API_KEY or GOOGLE_API_KEY set in your environment variables? We recently changed the logic around this.
I ran into this error with these in my local .env file
export GOOGLE_GENAI_USE_VERTEXAI=TRUE
export GOOGLE_CLOUD_PROJECT="......."
export GOOGLE_CLOUD_LOCATION="global"
but I know I also had some other crufty env variables as well.
We've seen this error happen when you have either a GEMINI_API_KEY or GOOGLE_API_KEY and also GOOGLE_GENAI_USE_VERTEXAI=TRUE. Can someone confirm that they are seeing this issue and it is not the case that GEMINI_API_KEY or GOOGLE_API_KEY are set? It's not enough to show me the .env file because there are other ways to set environment variables. run printenv (or your os's equivalent) to see what's in your environment variables.
same issue. I have configured the API Key in the ~/.gemini/.env and it works fine, but if there is .env in the directory when gemini starts, the API Key will be required. It seems that gemini cli requires the API Key as long as it detects that there is a .env file in the current directory, instead of using the API Key in ~/.gemini/.env first.
Preferring the .env file in the working directory over the one in the home directory is intentional. The working dir configs are considered more specific than the ones in the home dir.
Preferring the .env file in the working directory over the one in the home directory is intentional. The working dir configs are considered more specific than the ones in the home dir.
Thanks for reply, but I think the logic should be based on variables, not files. If the env in the working directory does not have an API KEY, the one in the home directory should be used instead of requiring login. This is a problem because env files exist in many projects, such as vite project. Keeping this behavior will cause some troubles
Preferring the .env file in the working directory over the one in the home directory is intentional. The working dir configs are considered more specific than the ones in the home dir.
Thanks for reply, but I think the logic should be based on variables, not files. If the env in the working directory does not have an API KEY, the one in the home directory should be used instead of requiring login. This is a problem because env files exist in many projects, such as vite project. Keeping this behavior will cause some troubles
Merging .env files would leave users with no way to unset a variable. That's not scalable. We intentionally load only the single most specific .env file for each execution. If you intentionally create an .env file in your working directory, you are going to have to remember to add the API key to it as well. There's no way around that.