Cloud and hosting

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

Last checked 2026-10-25 against Google Cloud (service account keys and API keys)'s documentation

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.

Google Cloud (service account keys and API keys) key types

TypePrefixSecret?ScopeExpiry
Service account key (JSON)
A service account can have up to 10 keys. The JSON file contains fields such as type, project_id, private_key_id, private_key and client_email. Organizations created on or after May 3, 2024 enforce iam.disableServiceAccountKeyCreation by default.
—YesWhatever IAM roles the service account holdsDoes not expire by default; an organization policy can set a maximum validity
API key
Google's example key begins AIza, but the docs do not state a prefix rule. Deleted keys can be restored within 30 days.
—YesThe 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.

Open Google Cloud (service account keys and API keys)'s key settings

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.

Wait at least 60 seconds after creating a service account key before using it; if you get an error, retry with exponential backoff.

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 your Google Cloud (service account keys and API keys) 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 and CLI

Google Cloud (service account keys and API keys)'s SDKs and docs read the key from GOOGLE_APPLICATION_CREDENTIALS.

Google Cloud CLI (gcloud): gcloud auth application-default login creates local Application Default Credentials; gcloud services api-keys create makes API keys

Things that catch people out

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

Recent incidents that put Google Cloud (service account keys and API keys) keys at risk

If you were affected by one of these, rotate your Google Cloud (service account keys and API keys) keys using the rotation steps above.

Using your Google Cloud (service account keys and API keys) 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 Cloud (service account keys and API keys) key, these intents fit:

IntentWhat BotPassword does
list_credentialsLists the credentials this agent is granted and what it may do with each. Never values.
run_withRuns 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_loginLets 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.
rotatePartly 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_secretSaves a value the agent already holds as a new vault item.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

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.

Stop pasting Google Cloud (service account keys and API keys) keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.