# Google Gemini API API keys: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/google-gemini
Last checked: 2026-11-08

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.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Gemini API key | — | 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

1. Open Google AI Studio's API Keys page (`aistudio.google.com/apikey`).
2. Click **Create API key**.
3. If your projects do not appear, import your Google Cloud project in the Dashboard's Projects view first.
4. Copy the key and store it in an environment variable or secret manager.

## 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

1. Create a replacement key.
2. Deploy it and verify it works.
3. Disable the old key.

## How to revoke a Google Gemini API key

1. Create and verify a replacement key first, so the switch causes no downtime.
2. Disable the old Gemini API key in the Google Cloud console. Google advises against deleting a compromised key right away.
3. Delete or revoke old keys once nothing uses them.

## If the key leaked

1. Create a replacement Gemini API key in AI Studio or the Google Cloud console.
2. Deploy your application with the new key and verify it works.
3. Disable the compromised key in the Google Cloud console.
4. Review billing logs and API usage in the Google Cloud console for unauthorized activity.
5. Set up billing alerts in the Google Cloud console so usage or cost spikes notify you.
6. 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

- `GEMINI_API_KEY`
- `GOOGLE_API_KEY`

## Gotchas

- A `200 OK` from `models.list` only shows that a Gemini API key exists, not that inference calls are enabled for the project.
- If both `GEMINI_API_KEY` and `GOOGLE_API_KEY` are set, `GOOGLE_API_KEY` takes 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.

## Using this key with AI agents (BotPassword)

- `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.

## FAQ

### 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.

## Sources

- https://ai.google.dev/gemini-api/docs/api-key
- https://aistudio.google.com/docs/api-key
- https://aistudio.google.com/docs/troubleshooting
- https://docs.github.com/en/code-security/secret-scanning/introduction/supported-secret-scanning-patterns
