Fix MalformedCredentialsMapError on Google Maps
The name suggests something is wrong with your key. Usually there isn't. Google's description of this error is about the page itself: it was loaded from a URL scheme the Maps JavaScript API doesn't support.
How to tell this is your problem
- The console shows MalformedCredentialsMapError.
- The address bar shows something other than https:// or http://, such as file:///Users/…/index.html.
- Or the map runs inside a mobile or desktop app built with a WebView, Cordova, Capacitor, Ionic or Electron.
- The same key and code work fine when the page is served from your real website.
This is what the console shows:
Why it happens
Google's error reference says your application uses an unsupported URI scheme, and that it should use a URI that conforms to RFC 3986. In plain terms, the Maps JavaScript API needs to know the page's address so it can check it against your key's website restrictions. If the page has no proper web address, that check can't run.
The usual causes, most likely first:
- Opening an HTML file directly. You double-clicked a saved page or a downloaded template, so the browser opened it as file://. This is the most common case during development and testing.
- A hybrid app WebView. Frameworks such as Cordova, Capacitor and Ionic, and some Electron set-ups, serve their pages from a custom scheme like capacitor://localhost, ionic://localhost or app://. Depending on the framework and platform, that can be a scheme the Maps JavaScript API won't accept.
- A page built from a string. A WebView loading HTML from a string, or an iframe with a data: or blob: source, has no normal web origin.
- A browser extension page. Extension pages run under their own scheme, not https://.
Google doesn't publish a list of accepted and rejected schemes, so test your exact set-up. The safe rule is that a page served over https:// from a real domain works.
The fix, step by step
If you opened a local file
- 01Serve the folder through a local web server rather than opening the file. Any static server will do. There are two one-line examples below the steps.
- 02Open http://localhost:8000/ (or whatever address the server prints) instead of the file:// path.
- 03If your key has website restrictions, add the local address you use for testing to the key's list, or use a separate development key that allows it. Otherwise you'll move on to RefererNotAllowedMapError, which means the scheme problem is solved.
# Python 3
python3 -m http.server 8000
# or Node.js
npx serve .
Keep development keys separate from your production key, with tight API restrictions, so a test key left on a laptop can't be abused.
If the map runs inside an app
- 01Check the page's real origin. In the WebView's developer tools console, run location.href and look at the scheme.
- 02If it isn't http:// or https://, configure your framework to serve the app from an http(s) origin, if it supports that. Several hybrid frameworks offer a setting for the scheme or hostname they use. Check its documentation.
- 03Alternatively, host the map page on your website and load that https URL in the WebView. The page then has a normal origin, and your key's website restrictions apply as usual.
- 04For a native app, consider Google's native Maps SDK for Android or Maps SDK for iOS instead of the JavaScript API in a WebView. They use their own key restrictions for apps.
If the map is in a data: or blob: iframe
Move the map into a page served from your own domain and point the iframe's src at that URL. The Maps JavaScript API then sees a normal https origin.
Rotating or regenerating the key won't fix this error, because the key isn't the problem. If the same key works on your live website, leave it alone and fix how the page is loaded. If you've already replaced it, make sure the new key has the same restrictions as the old one.
Confirm it is fixed
Load the page from its new http(s) address with the console open. MalformedCredentialsMapError should be gone. If you now see RefererNotAllowedMapError, the page has a proper origin and the key simply doesn't allow it yet. Add that origin to the key's website restrictions.
For a public page, run a scan too. The scanner loads your page over its real https URL from a clean browser, so it shows you what visitors get, not what your local copy does. If the live site scans clean, the problem was local to your test set-up.
Common questions
Why does my map work online but not when I open the HTML file?
Opening the file directly gives it a file:// address, which the Maps JavaScript API doesn't accept. Serve the folder through a local web server and open it over http://localhost instead.
Does MalformedCredentialsMapError mean my API key is wrong?
Usually not. Google describes this error as an unsupported URI scheme on the application. The fix is to load the page from an http or https address.
Can I use the Maps JavaScript API in a Capacitor or Cordova app?
It depends on the scheme your app serves pages from. If the console shows this error, configure the framework to use an http(s) origin, load a hosted https page in the WebView, or use the native Maps SDKs.
Do I need to add localhost to my key's restrictions?
Only if the key has website restrictions and you test on localhost. A separate development key, restricted to your local addresses and the APIs you need, keeps the production key tidy.
A scan tells you in about twelve seconds, from a clean session.