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
| Type | Prefix | Secret? | Scope | Expiry |
|---|---|---|---|---|
| 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. | — | Yes | Whatever IAM roles the service account holds | Does 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. | — | 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
- For a service account key, open the Service accounts page and select your project.
- Click the service account's email address, then open the Keys tab.
- Click Add key, then Create new key, choose JSON and click Create. The file downloads once and cannot be downloaded again.
- 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
- 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.
- 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
- 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.
- To revoke short-lived credentials already issued from a key, disable or delete the service account itself.
- 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
- Disable the exposed service account key or delete the exposed API key.
- If short-lived tokens may already have been issued, disable or delete the service account, because deleting a key does not revoke them.
- 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.disableServiceAccountKeyCreationorganization 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.
- OpenCode Console breach may have exposed users' linked AI provider API keys 2026-10-17
- tensorlake 0.5.144 on npm shipped a Shai-Hulud variant with a wipe-on-revoke dead-man's switch 2026-10-08
- GhostAction returns: fake security workflows pushed to 30,000+ repos to steal Actions secrets 2026-10-07
- keyv, flat-cache and cache-manager npm releases carried a CI-secret stealer with a revocation trap 2026-08-04
- 32 @redhat-cloud-services npm packages compromised after a VS Code extension hijacked a GitHub account 2026-06-01
- Nx Console 18.95.0 VS Code extension shipped a credential stealer after a TanStack infection 2026-05-18
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:
| Intent | What BotPassword does |
|---|---|
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. |
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.