# Google Cloud (service account keys and API keys) API keys: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/google-cloud
Last checked: 2026-10-25

Google Cloud has two common long-lived credentials. A service account key is a JSON file created under IAM & Admin → Service accounts → your account → Keys → Add key, and it can be downloaded only once. An API key is created on the APIs & Services → Credentials page and should be restricted to specific APIs and applications.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Service account key (JSON) | — | Yes | Whatever IAM roles the service account holds | Does not expire by default; an organization policy can set a maximum validity |
| API key | — | Yes | The APIs and applications it is restricted to; unrestricted keys are insecure | — |

## How to create a Google Cloud (service account keys and API keys) key

1. For a service account key, open the **Service accounts** page and select your project.
2. Click the service account's email address, then open the **Keys** tab.
3. Click **Add key**, then **Create new key**, choose **JSON** and click **Create**. The file downloads once and cannot be downloaded again.
4. For an API key, open the **Credentials** page, click **Create credentials**, then **API key**, and add at least one API restriction.

## Limiting what the key can do

A service account key can do whatever the service account's IAM roles allow, so give the account only the roles it needs. API keys can be limited by API restriction (which APIs the key may call) and by application restriction (websites, IP addresses, Android apps or iOS apps; one type at a time). Google recommends setting both.

## How to rotate a Google Cloud (service account keys and API keys) key

1. Service account keys, create a new key, update your applications to use it, disable the old key to confirm nothing depends on it, then delete the old key.
2. API keys, create a new key with the same restrictions (the console has a **Rotate key** button), update your applications, then delete the previous key.

## How to revoke a Google Cloud (service account keys and API keys) key

1. Service account key, open the service account's **Keys** tab and delete the key (deletion is permanent). Disabling first lets you check nothing depends on it.
2. To revoke short-lived credentials already issued from a key, disable or delete the service account itself.
3. API key, delete it on the **Credentials** page; deleted API keys can be restored within 30 days.

## If the key leaked

1. Disable the exposed service account key or delete the exposed API key.
2. If short-lived tokens may already have been issued, disable or delete the service account, because deleting a key does not revoke them.
3. Create a replacement, ideally avoiding keys by using an attached service account or Workload Identity Federation.

## Environment variables

- `GOOGLE_APPLICATION_CREDENTIALS`

## Gotchas

- A service account key file can be downloaded only once, at creation.
- Deleting a service account key does not revoke short-lived credentials already issued from it.
- Key creation may be blocked by the `iam.disableServiceAccountKeyCreation` organization policy constraint.
- Unrestricted API keys are insecure; the console requires at least one API restriction.

## Using this key with AI agents (BotPassword)

- `list_credentials`: Lists the credentials this agent is granted and what it may do with each. Never values.
- `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.
- `cli_login`: Lets git, Docker, the AWS CLI and kubectl log in through bpw's credential helper, so the token is fetched when the tool needs it and not written into its config. The helper hands the value to the tool, so the grant is always disclosing. Other tools use run_with; OAuth device logins are planned.
- `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.

## FAQ

### Where do I find my Google Cloud service account key?

Open IAM & Admin → Service accounts, click the account, open the Keys tab and choose Add key → Create new key → JSON. Google cannot show an existing key again, so create a new one if the file is lost.

### How do I rotate a Google Cloud API key?

Create a new key with the same restrictions (the console has a Rotate key button), update your applications, then delete the old key.

### What is GOOGLE_APPLICATION_CREDENTIALS?

It is the environment variable Application Default Credentials checks first. It points to a JSON credential file such as a service account key.

### Why can't I create a service account key?

Your organization probably enforces the `iam.disableServiceAccountKeyCreation` constraint, which organizations created on or after May 3, 2024 have by default.

### Can an AI agent use my Google Cloud (service account keys and API keys) 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 `list_credentials`, `run_with`, `store_secret`, so the agent gets the result and never the value.

## Sources

- https://docs.cloud.google.com/iam/docs/keys-create-delete
- https://docs.cloud.google.com/docs/authentication/api-keys
- https://docs.cloud.google.com/docs/authentication/application-default-credentials
