Knowledge base / API keys

Rotate a leaked Google Maps API key without downtime

A Maps key used in a web page is public by design. Anyone can read it in the page source. What protects it is its restrictions. If a key has leaked, tighten those first, then rotate in an order that keeps your map working the whole time.

How to tell this is your problem

  • Usage in Cloud Console that doesn't match your traffic, or calls to APIs your site doesn't use.
  • A notice from Google, GitHub or a security scanner about an exposed key.
  • The key appears in a public repository, a shared code snippet, a support forum or a third-party script.
  • A bill or budget alert far above normal.
  • The key has no application restrictions, so it would work from any site.

Why the order matters

A browser key in your page is always visible, so a key being “seen” is not an emergency on its own. It becomes one when the key is unrestricted, or restricted loosely enough that someone else can use it from their own site or server. Three things decide how urgent this is:

  • Application restrictions. A key limited to your domains can't load maps on someone else's site. A key with no restriction works from anywhere.
  • API restrictions. A key limited to the Maps JavaScript API can't be used for expensive web-service calls such as bulk geocoding.
  • Where it leaked. A server-side key in a public repository is far more serious than a browser key in your own page source.

If you delete the old key first and then create a replacement, every map on your site goes grey until the new one is deployed everywhere. Creating, restricting and deploying the new key first removes that gap.

Step 1: contain it now

Before rotating, stop the abuse. Open the leaked key under APIs & Services → Credentials and tighten it:

  1. 01Under Application restrictions, choose Websites and add only your own domains, for example example.com/* and *.example.com/*. See referrer pattern syntax.
  2. 02Under API restrictions, choose to restrict the key and select only the APIs your pages use, usually the Maps JavaScript API plus any of Geocoding or Places that the site needs.
  3. 03Save. Your own site keeps working, and use from other sites and servers is refused.

For many leaks of a browser key, this is the most important step. Rotating still matters if the key was unrestricted for a while, if you can't be sure the restrictions are tight, or if the key was used for server-side calls.

Step 2: rotate without downtime

Cloud Console has a Rotate key action on each API key. Google describes it as creating a new key with all the old key's restrictions, and while you migrate, both the old and the new key are accepted. Rotation is available only in the Cloud Console.

  1. 01Check the old key's usage in Metrics before you start, so you know what normal looks like. Google suggests using the Metrics Explorer filtered by the key's credential_id.
  2. 02Open the key and click Rotate key. Give the new key a clear name, click Create, and copy the new key string.
  3. 03Check the new key's restrictions match what you set in step 1.
  4. 04Deploy the new key to every place the old one is used: theme settings, maps plugins, page builder widgets, server-rendered templates, environment variables and mobile apps.
  5. 05Clear page caches and CDN caches, so visitors receive HTML with the new key.
  6. 06Watch the old key's request graph. It should fall towards zero as caches expire. Any remaining traffic means a page you missed, or someone else using it.
  7. 07When the old key shows no legitimate traffic, delete it from the Previous key section of the new key's page.
Find every place the key lives

Search your codebase, your CMS database and your hosting environment variables for the old key string. On WordPress, keys commonly sit in theme options, a maps plugin, page builder global settings and individual widgets. See where each page builder stores the key.

If the key was in a public repository

  • Rotate it. Removing the key in a new commit doesn't remove it from the repository history, and copies may already exist elsewhere.
  • Move keys out of source code and into environment variables or your hosting platform's settings.
  • Use separate keys for browser and server. A browser key gets website restrictions. A server key for web services such as Geocoding gets IP address restrictions and never appears in front-end code.
  • Use a separate key for development and preview hosts, so you never have to allow a broad wildcard on your production key.

If you deleted the old key too early

Google says you can undelete an API key within 30 days of deleting it. After 30 days it can't be restored. Deletion takes a few minutes to propagate, and so does undeleting. If pages break after deletion, restore the key, finish deploying the new one, and try again.

Confirm it is fixed

Scan each page that has a map. The scan shows the key each page sends, so a page still using the old key stands out. After deletion, any page still sending the old key will show InvalidKeyMapError.

Over the next few days, check that usage matches your traffic and that no calls arrive for APIs you didn't allow. Then set quotas and budget alerts so a future leak is caught early. See capping Google Maps spend.

Common questions

Is it safe to have a Google Maps API key in my page source?

Yes, if it is restricted. The Maps JavaScript API runs in the browser, so the key is always visible. Website and API restrictions are what stop others using it.

Does rotating the key break my map?

Not if you follow the order. Rotate key creates a new key while the old one keeps working, so you can deploy the new one before removing the old one.

Can I get a deleted API key back?

Yes, within 30 days of deleting it. Look for deleted keys in Credentials, or use gcloud services api-keys undelete. After 30 days the key is gone for good.

Will Google refund usage from a leaked key?

Contact Google Cloud billing support with the details. There is no guarantee, so restricting keys in advance is the reliable protection.

Check whether this is your error

A scan tells you in about twelve seconds, from a clean session.

Related articles