Fix InvalidAppCheckTokenMapError on Google Maps
This error only appears on projects that use Firebase App Check with the Maps JavaScript API. Your page sent Google an App Check token, and Google judged it invalid or expired. The key is fine. The problem is the attestation layer on top of it.
How to tell this is your problem
- The console shows InvalidAppCheckTokenMapError.
- Your site uses Firebase, and someone has set up App Check for Maps, possibly a while ago or on a different team.
- The map works on your production domain but fails on staging or localhost, or the other way round.
- The map broke soon after App Check enforcement was turned on.
This is what the console shows:
How App Check works with Maps
App Check is a Firebase service that helps block traffic from sources other than your genuine app. In the browser, an attestation provider (Google recommends reCAPTCHA Enterprise or reCAPTCHA v3) checks the visitor's session and issues a short-lived App Check token. The Maps JavaScript API attaches that token to its requests, and Google verifies it.
Two points from Google's documentation matter here. Enforcement isn't on by default: until you enforce, App Check only reports metrics. Once enforced, traffic without a valid App Check token is rejected. Google's error reference adds that this error only applies when App Check verification is in use, and that the token is invalid or expired.
Why it happens
- The token isn't wired into Maps. The page initialises App Check but never sets Settings.getInstance().fetchAppCheckToken, so Maps has no way to get a valid token. This is common when one part of the site is built with App Check in mind and a map is added elsewhere.
- Attestation fails on this domain. reCAPTCHA site keys have a list of allowed domains. On a staging domain, a preview deployment or a new custom domain missing from that list, attestation fails and no valid token is produced.
- Local development without a debug token. On localhost the real providers usually can't attest. Google's guide says to use the App Check debug provider and register its debug token in the Firebase console.
- Tokens aren't refreshed. Tokens have a time to live set in the Firebase console. If auto-refresh is off, a page left open for long enough starts sending expired tokens.
- Blocked attestation requests. A Content Security Policy, ad blocker or privacy extension can block reCAPTCHA, so the visitor never gets a token. See CSP for Google Maps.
- Enforcement was switched on too early. Google advises watching the App Check metrics until most traffic is verified before you enforce.
Quick fix: stop enforcing while you investigate
If the map is down for real visitors and you need it back now, open App Check in the Firebase console for the project and stop enforcing it for Maps. Without enforcement, App Check only collects metrics, and maps load again. Then work through the steps below and re-enable enforcement once the metrics show your traffic is verified.
If nobody on your team meant to use App Check, leaving enforcement off is a reasonable final answer. Keep your key's website and API restrictions in place either way.
The fix, step by step
- 01Check that the page loads a current Maps JavaScript API version. Google requires the latest weekly or quarterly release for App Check.
- 02Confirm App Check is initialised before the map is created, with your reCAPTCHA site key and isTokenAutoRefreshEnabled: true.
- 03Confirm the page sets Settings.getInstance().fetchAppCheckToken to a function that returns getToken(appCheck, false), as in the sample below.
- 04In the reCAPTCHA settings for your site key, check that every domain serving the map is listed, including www and non-www versions and any staging hosts.
- 05For local development, turn on the App Check debug provider, copy the debug token it prints to the console, and add it in the Firebase console. Never ship the debug provider to production.
- 06Check your Content Security Policy allows the reCAPTCHA and Firebase requests as well as the Maps domains.
- 07Reload with the console open and check the App Check metrics in the Firebase console to see whether requests are now verified.
This is Google's pattern for connecting App Check to the Maps JavaScript API, trimmed slightly:
import { initializeApp } from "firebase/app";
import { getToken, initializeAppCheck, ReCaptchaEnterpriseProvider } from "firebase/app-check";
async function init() {
const { Settings } = await google.maps.importLibrary("core");
const { Map } = await google.maps.importLibrary("maps");
const app = initializeApp({ /* your Firebase config */ });
const appCheck = initializeAppCheck(app, {
provider: new ReCaptchaEnterpriseProvider("YOUR_RECAPTCHA_SITE_KEY"),
isTokenAutoRefreshEnabled: true,
});
// Tell Maps how to get a token for each request.
Settings.getInstance().fetchAppCheckToken = () =>
getToken(appCheck, /* forceRefresh = */ false);
new Map(document.getElementById("map"), {
center: { lat: 51.5072, lng: -0.1276 },
zoom: 10,
});
}
init();
Most WordPress maps plugins and site builder map blocks don't support App Check. If App Check is enforced for Maps on a project and a plugin's map uses a key from that project, expect it to fail. Use a key from a separate project without App Check for those maps, or turn enforcement off.
Confirm it is fixed
Reload the page in a private window with the console open, on every domain that serves the map. The error should be gone and tiles should load. In the Firebase console, the App Check metrics should show Maps requests as verified.
Then run a scan from outside your network. It loads the page in a fresh headless browser and reports any Maps error codes in the console, which shows you how App Check treats a first-time visitor.
Common questions
What is an App Check token?
A short-lived token issued by Firebase App Check after an attestation provider, such as reCAPTCHA, checks the visitor's session. Google uses it to confirm requests come from your genuine app.
I never set up App Check. Why am I seeing this?
Google says this error only applies when App Check verification is in use, so someone has configured it on the Cloud or Firebase project behind your key. Check the Firebase console for that project, or ask whoever manages it.
Does App Check replace API key restrictions?
No. Keep website and API restrictions on the key. App Check is an extra layer that rejects requests without a valid token.
Why does the map work in production but not on localhost?
Real attestation providers usually can't attest a localhost session. Use the App Check debug provider and register its debug token in the Firebase console for local testing.
A scan tells you in about twelve seconds, from a clean session.