Knowledge base / API keys

Fix InvalidKeyMapError on Google Maps

Google looked up the key string your page sent and found nothing. In practice the key in Cloud Console is usually fine, and something between Cloud Console and the page changed it on the way.

How to tell this is your problem

  • The map area loads grey with the “Oops! Something went wrong.” overlay, on every page load and in every browser.
  • The console shows InvalidKeyMapError, not RefererNotAllowedMapError or ApiNotActivatedMapError.
  • The same key works when you paste it into a bare test page.
  • The map broke straight after a theme, plugin or CMS update, a site migration, or someone tidying up keys in Cloud Console.

This is what the console shows:

Google Maps JavaScript API error: InvalidKeyMapError
https://developers.google.com/maps/documentation/javascript/error-messages#invalid-key-map-error

Why it happens

Google returns this error when the key= value in the loader URL doesn't match any API key it knows about. The key is checked before referrer or API restrictions, so a broken key hides every other problem. These are the usual causes:

  • Whitespace or invisible characters. A trailing space, a line break, or a non-breaking space copied from a chat or email. URL-encoded, a space becomes %20 at the end of the key.
  • HTML escaping. Some CMS fields escape characters, so the key reaches the page altered.
  • A truncated paste. Browser-key strings are 39 characters and start with AIza. Anything shorter has been cut off.
  • The key was deleted. Deleted keys stop working straight away. Google lets you restore a deleted key for a limited time, after which it's gone for good.
  • The wrong key. A server-side key, an OAuth client ID, or a key from another project pasted into the Maps field.
  • A plugin override. A widget-level key field in a page builder overrides the global one, and still holds an old value.

The fix, step by step

  1. 01Open the page and choose View source. Search for maps/api/js or maps.googleapis.com and copy the whole key= value, up to the next & or quote.
  2. 02Check its length and start: 39 characters beginning with AIza. Look for %20, & or a trailing space.
  3. 03In Google Cloud Console open APIs & Services → Credentials and select the project you expect. Compare the key there character by character with the one from your page source.
  4. 04If the key isn't listed, check you're in the right project using the project picker. If it was deleted recently, look for the option to restore it.
  5. 05If it can't be restored, create a new API key and apply the same application and API restrictions as the old one before you use it.
  6. 06Paste the key into your CMS or theme field with nothing around it, save, and clear any page or CDN cache so visitors get the new HTML.
Find every place the key is set

Search your whole site, not just the theme settings. In WordPress, keys commonly live in the theme options, a maps plugin, a page builder's global settings and individual map widgets. Any one of them can inject its own loader. See where each page builder stores the key.

What a healthy key looks like in the page source

Compare what you copied from the page with these patterns. Anything in the right-hand column means the page is changing the key on its way out.

What you see after key=What it means
AIza followed by 35 more letters, digits, - or _Correctly formed. If the error persists, the key was deleted or belongs to a project you can't see.
The same key ending in %20A trailing space was saved in the settings field.
& straight after the keyThe CMS escaped the ampersand that separates URL parameters.
Fewer than 39 charactersThe paste was cut off.
undefined, null or YOUR_API_KEYA template or environment variable was never filled in.

If two different keys appear in the source, two plugins or templates are each loading the API. That causes its own problems even after the key is fixed. See finding a duplicate Maps loader.

If you load the API in your own code

Read the key from configuration, not a hand-edited string, and trim it before use. With the dynamic library import, the key goes in the bootstrap parameters:

JavaScript
(g=>{/* Google's inline bootstrap loader, copied unchanged from the docs */})({
  key: "AIzaSy...your-39-character-key",
  v: "weekly",
});

With an environment variable at build time, make sure the variable is actually set in production. An unset variable usually produces MissingKeyMapError or a literal undefined key, which shows up as this error.

Confirm it is fixed

Reload the page in a private window with the console open. The error should be gone and map tiles should load. If you now see RefererNotAllowedMapError or ApiNotActivatedMapError, the key is being recognised and you've moved on to the next check, which is progress.

Run a scan as well. It loads the page from a clean session and lists every loader request, which catches a second plugin still injecting an old key.

Common questions

Is InvalidKeyMapError the same as RefererNotAllowedMapError?

No. InvalidKeyMapError means Google doesn't recognise the key at all. RefererNotAllowedMapError means the key is valid but isn't allowed on the domain serving the page.

Can I just generate a new key?

Yes, but copy the old key's restrictions to the new one first. An unrestricted key on a public page can be copied and used by anyone, and you pay for their usage.

How long does a new key take to work?

A new key usually works within a few minutes. If it still fails after that, the page is most likely still serving the old key from a cache.

Check whether this is your error

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

Related articles