# Google API Key Exposed? Check Its Restrictions, Then Rotate It

URL: https://tuvio.dev/blog/google-api-key-exposed
Published: 2026-11-08
By the Tuvio team

Google API key exposed? First find out what the key can do. Open the key in the Google Cloud console and look at its restrictions. A key locked to your website and to the Maps JavaScript API was always going to be public, and the fix is to confirm the restrictions are tight. A key with no restrictions, or one that can call a billed API such as Gemini, Vision or Places from anywhere, is a credential someone else can spend, and you rotate it now, restrict the replacement, and check the bill.

That decision is what makes Google keys different from an [OpenAI key](https://tuvio.dev/blog/openai-api-key-leaked) or a Stripe secret. Those are always secret. A Google API key (the `AIza...` string) might be fine where it is. This post walks through telling the two cases apart, and what to do in each.

## Google API key exposed: what to do first

1. Open the key and read its restrictions.
2. If it's unrestricted, or restricted to something that isn't where you found it, rotate it.
3. Restrict the new key: one application restriction, and only the APIs it needs.
4. Check usage and billing for traffic you didn't cause, and set a budget alert.
5. Close the leak: the commit, the bundle, the APK, the chat.

Each step is below.

## Step 1: Read the key's restrictions

In the Google Cloud console, open **APIs & Services** and go to the **Credentials** page. Each API key shows two things that decide everything else.

**Application restrictions** say *where* the key may be used from. Google's [API keys docs](https://docs.cloud.google.com/docs/authentication/api-keys) list four types, and "you can apply only one application restriction type at a time":

- Websites, for web applications (the browser's referrer must match)
- IP addresses, for applications called by specific servers
- Android apps (package name plus signing certificate fingerprint)
- iOS apps (bundle identifier)

**API restrictions** say *what* the key may call. Per the same docs, "API restrictions specify which APIs can be called using the API key."

Now match that against where the key was exposed:

| Where you found the key | Restrictions it has | Verdict |
|---|---|---|
| Your website's JavaScript | Websites = your domains, APIs = Maps JavaScript, Firebase | Public by design. Confirm the list, done. |
| Your website's JavaScript | None, or APIs include Places, Gemini, Vision, Translate | Anyone can call billed APIs as you. Rotate. |
| A GitHub repository, in `.env` or source | Any | The restriction may still hold, but you can't know who copied it or where they'll try it. Rotate. |
| An Android APK or iOS build | Android/iOS app restriction, APIs = what the app uses | Mostly fine for Maps and Firebase. Billed APIs still need a server. |
| A chat, screenshot, log or support ticket | Any | Rotate. |

Google's Maps Platform [security guide](https://developers.google.com/maps/api-security-best-practices) is explicit about the stakes: "You are financially responsible for charges caused by abuse of unrestricted API keys."

## Step 2: Rotate the key

Rotation on the Credentials page creates a replacement without breaking your app on the spot. Per the docs, "when you rotate an API key, you create a new key with the same restrictions as the old key." Click **Rotate key** on the key's page, copy the new key into your server environment or your app config, deploy, and then from the new key's details page use the **Previous key** section to **Delete the previous key**.

Two details:

- The old key keeps working until you delete it. Don't leave the deletion for later; the point of rotating is that the exposed string stops working.
- Deleted by mistake? The docs say you "can undelete (restore) that key within 30 days of deleting the key." After that it's gone.

If the exposed key is the only one your production app uses and you can't redeploy right now, rotate anyway and deploy as soon as you can. A few minutes of a broken Maps embed costs less than a weekend of someone else's Gemini traffic.

## Step 3: Restrict the new key properly

Google's [best practices](https://docs.cloud.google.com/docs/authentication/api-keys-best-practices) are short: "By adding restrictions, you can limit the ways an API key can be used, reducing the impact of a compromised API key." Maps Platform says the same more firmly: "Best practice is to always restrict your API keys with one type of application restrictions and one or more API restrictions."

In practice, for an AI-built web app:

- **The browser key.** Application restriction: Websites, with your production domain and your preview domains (`https://yourapp.com/*`, `https://*.vercel.app/*` if you need previews). API restrictions: Maps JavaScript API, and the Firebase APIs your app uses. Nothing billed per request that a stranger could call from a script.
- **The server key.** Application restriction: IP addresses, if your host gives you stable ones; otherwise none, and the key lives only in server environment variables. API restrictions: exactly the APIs the server calls. This is where Gemini, Places, Geocoding, Vision and Translate belong.
- **Separate keys per use.** Google's docs say to "retain only the API keys you are actively using to keep your attack surface as small as possible", and one key per purpose means you can rotate one without touching the rest.

A referrer restriction isn't a secret. Referrers can be faked by a script that isn't a browser, so a website-restricted key that can call a per-request billed API is still spendable. The application restriction protects the key against casual copying; the API restriction is what protects your bill.

## Step 4: Check usage and the bill

In the console, **APIs & Services** shows traffic per API, and **Billing** shows cost by service. Look at the period since the key could have been exposed, which for a repository means since the commit that added it, not since you noticed.

Then set a **budget** with alerts in Billing so a spike emails you instead of waiting for the invoice. A budget doesn't stop spending by itself, but it's the difference between finding out in a day and finding out in a month.

If there's usage you didn't cause, write down the dates and amounts and open a billing support case from the console. Describe what happened and what you've done since (rotated, restricted). There's no promise of a refund, but a clear account helps.

## Did Google find it before you did?

GitHub runs a [secret scanning partner program](https://docs.github.com/en/code-security/secret-scanning/introduction/about-secret-scanning-for-partners): when a known secret format shows up in a public repository, the provider is told. Google is a partner for the `google_api_key` type, with a validity check, per GitHub's [supported patterns](https://docs.github.com/en/code-security/secret-scanning/introduction/supported-secret-scanning-patterns). Google may also notify a project's owners when a key is found in public. If you get an email like that, treat it as real but act on it in the console, not through the email's links.

Two limits. A notification doesn't mean the key was disabled; check. And the program covers public GitHub repositories, not your deployed bundle, an APK, a private repo or a pasted chat message. Those leaks are yours to find.

## How Google API keys get exposed, and the fix for each

### In a GitHub repository

The usual path: a `.env` file committed early on, or `GOOGLE_MAPS_API_KEY=AIza...` pasted straight into a config file by an assistant. Rotate, then follow [what to do after pushing a .env file](https://tuvio.dev/blog/accidentally-pushed-env-file-to-github) for the cleanup and for push protection. A key deleted in a later commit is still in the history.

### In the frontend bundle

If the key is for Maps JavaScript or Firebase and it's restricted, this is the intended place for it. If it's for a billed API, the browser is the wrong place for it regardless of restrictions: move the call to a server route that holds the key. [How to hide an API key in the frontend](https://tuvio.dev/blog/hide-api-key-in-frontend) covers the pattern, and the same `NEXT_PUBLIC_` and `VITE_` prefix rules apply: anything under those prefixes ships to the browser.

### In an Android or iOS build

Keys in a mobile app can be read out of the package. Google's answer is the Android app and iOS app restriction types, which tie the key to your app's identity, plus API restrictions. That's enough for Maps SDKs and Firebase. For anything billed per request, the app should call your server, and your server holds the key.

### A Firebase key

Firebase's [API keys page](https://firebase.google.com/docs/projects/api-keys) says keys restricted to Firebase services "do not need to be treated as secrets", because access to your data is controlled by Security Rules and App Check, not by the key. So a Firebase key in your bundle is normal. What isn't normal: the same key allowed to call Gemini. The same page warns never to add the Gemini Developer API to a publicly accessible key's allowlist. And a public Firebase key with [open Firestore rules](https://tuvio.dev/blog/firestore-rules-allow-read-write) is a different problem entirely, and the rules are the fix.

### A Gemini key

Gemini API keys from Google AI Studio are also `AIza...` strings, and they are always secret: every request is billed or counted against your quota. GitHub lists `google_gemini_api_key` as a user-alert pattern only, so no partner is told when one leaks. Treat it like an OpenAI key: server only, rotated the moment it appears anywhere public.

### Pasted somewhere

A Stack Overflow question, a Discord message, a screenshot of your console, a CI log. Rotate; there's no cleanup that reaches everyone who saw it.

## Exposed Google API key checklist

- The key's restrictions read, and compared with where it was found.
- Unrestricted or mismatched: rotated, and the previous key deleted.
- New key has one application restriction and only the APIs it needs.
- Billed APIs (Gemini, Places, Vision, Translate, Geocoding) called from a server, never from the browser or a mobile app.
- Usage and billing reviewed since the exposure; a budget alert set.
- Support contacted, with dates and amounts, if there was usage you didn't cause.
- Leak path closed: not in the repo or its history, not in a public env var unless it's meant to be public, push protection on.

## Check your repository for an exposed Google API key

A free [Tuvio scan](https://tuvio.dev/scan) of your GitHub repository needs no account. It flags Google API keys in your code and in git history (the full history where it can, else the last 12 months, else the latest commit, and the report says which), so a key removed in a later commit is still flagged. It also flags `.env` files committed to the repository and an LLM client running in the browser with its key read from a public variable.

It reads the repository as pushed to GitHub, and it can't see a key's restrictions in your Google Cloud project, so Step 1 is still yours: a flagged Maps key may be fine, and the scan tells you where it is, not whether it should be there. [Coverage](https://tuvio.dev/coverage) lists what is checked, and the [example report](https://tuvio.dev/example) shows what a finding looks like.
