Gemini API keys are created in Google AI Studio on the API Keys page (aistudio.google.com/apikey). Google's client libraries read GEMINI_API_KEY or GOOGLE_API_KEY, and GOOGLE_API_KEY wins if both are set. You can restrict a key to the Gemini API only.
Google Gemini API key types
| Type | Prefix | Secret? | Scope | Expiry |
|---|---|---|---|---|
| Gemini API key Since May 28, 2026, new keys created in AI Studio are auth keys, bound to a Google Cloud service account, with fast-acting leaked key enforcement. Older standard keys only identify a project; the Gemini API rejects unrestricted standard keys, and from May 7, 2026 it blocks unrestricted keys that have been dormant for an extended period. | — | Yes | Gemini API access for the Google Cloud project it belongs to; can be restricted to the Gemini API only | — |
How to create a Google Gemini API key
- Open Google AI Studio's API Keys page (
aistudio.google.com/apikey). - Click Create API key.
- If your projects do not appear, import your Google Cloud project in the Dashboard's Projects view first.
- Copy the key and store it in an environment variable or secret manager.
Open Google Gemini API's key settings
Limiting what the key can do
In AI Studio, find a key labelled "Unrestricted", choose Add restrictions and select Restrict to Gemini API only (needs the apikeys.keys.update permission). In the Google Cloud console you can also add application restrictions by IP address, site or app. A key shared with other APIs and restricted to those APIs will stop working for Gemini, so use a separate key.
How to rotate a Google Gemini API key
- Create a replacement key.
- Deploy it and verify it works.
- Disable the old key.
How to revoke a Google Gemini API key
- Create and verify a replacement key first, so the switch causes no downtime.
- Disable the old Gemini API key in the Google Cloud console. Google advises against deleting a compromised key right away.
- Delete or revoke old keys once nothing uses them.
If your Google Gemini API key leaked
- Create a replacement Gemini API key in AI Studio or the Google Cloud console.
- Deploy your application with the new key and verify it works.
- Disable the compromised key in the Google Cloud console.
- Review billing logs and API usage in the Google Cloud console for unauthorized activity.
- Set up billing alerts in the Google Cloud console so usage or cost spikes notify you.
- Turn on GitHub secret scanning; GitHub detects Google API keys and Google Gemini API keys, and Google is a secret scanning partner for Google API keys.
Environment variables and CLI
Google Gemini API's SDKs and docs read the key from GEMINI_API_KEY or GOOGLE_API_KEY.
Things that catch people out
- A
200 OKfrommodels.listonly shows that a Gemini API key exists, not that inference calls are enabled for the project. - If both
GEMINI_API_KEYandGOOGLE_API_KEYare set,GOOGLE_API_KEYtakes precedence. - Do not hardcode the key in web or mobile apps; Google says to use a backend proxy.
- Creating keys needs IAM permissions in the project, such as those in the Project Editor role.
Common Google Gemini API authentication errors
Your API key was reported as leaked. Please use another API key.
HTTP 403 PERMISSION_DENIED. Google detected the Gemini API key as leaked and blocked it.
Recent incidents that put Google Gemini API keys at risk
If you were affected by one of these, rotate your Google Gemini API keys using the rotation steps above.
- LiteLLM 1.107.0 on PyPI and Docker carried a credential stealer after a leaked master key 2026-10-21
- OpenCode Console breach may have exposed users' linked AI provider API keys 2026-10-17
- VS Code and Open VSX extensions leaked 550+ secrets, including tokens that could publish updates 2025-10-15
Using your Google Gemini API key with AI agents
A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.
BotPassword works this way. For a Google Gemini API key, these intents fit:
| Intent | What BotPassword does |
|---|---|
run_with | Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output. |
http_request | Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused. |
rotate | Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet. |
store_secret | Saves a value the agent already holds as a new vault item. |
capture_secret | Planned, not built yet: will read a newly created key straight from the page or command output into the vault. |
Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents
Frequently asked questions
Where do I get a Gemini API key?
Go to aistudio.google.com/apikey and click Create API key.
Which environment variable does the Gemini API use?
The client libraries detect GEMINI_API_KEY or GOOGLE_API_KEY; GOOGLE_API_KEY takes precedence.
How do I restrict a Gemini API key?
In AI Studio choose Add restrictions then Restrict to Gemini API only.
Why was my Gemini API key blocked?
Google blocks Gemini API keys reported as leaked, with the message "Your API key was reported as leaked. Please use another API key." It also blocks unrestricted keys that have been dormant for an extended period. Create a new key in AI Studio.
What is a Gemini auth key?
An auth key is a Gemini API key bound to a Google Cloud service account, which allows granular access control and fast leaked key enforcement. New keys created in AI Studio since May 28, 2026 are auth keys.
Can an AI agent use my Google Gemini API key without seeing it?
Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a .env file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with run_with, http_request, store_secret, so the agent gets the result and never the value.