Lock down your Google Maps API key
A Google Maps key (it starts with AIza) is meant to be visible in your front end, but it's only safe with an HTTP referrer restriction for your domains and an API restriction for the Maps APIs you use. Without them, anyone can copy it and run up usage on your billing account.
Updated
Check your live site now
Free, no signup, read only. A grade and plain fixes in seconds.
Why a Maps key has to be public
The Maps JavaScript API loads in the visitor's browser, with your key in the script URL or the loader config. Anyone can open DevTools and read it. You can't hide it, so Google has you restrict where it works and which APIs it can call.
An AIza key is a general Google Cloud API key, the same format Firebase and the Gemini API use. What a particular key can call depends on two things: which APIs are enabled in its project and which APIs the key itself is restricted to.
What someone can do with an unrestricted Maps key
- Other sites and apps can embed maps with your key, billed to you.
- Scripts can call web service APIs like Geocoding, Places and Directions with it directly.
- With no API restriction, it works for any API enabled in the project, which could include paid services such as the Gemini API if you turned it on there.
- Heavy use can hit your quotas, so your own map stops loading.
Restrict the key in Google Cloud Console
- Open Google Cloud Console, go to APIs and Services, then Credentials, and click the key your site uses.
- Under Application restrictions choose Websites (HTTP referrers) and add your domains, like the list below. Use a separate key for localhost during development rather than adding localhost to the production key.
- Under API restrictions choose Restrict key and tick only what the page uses, for example Maps JavaScript API, plus Places API if you use autocomplete.
- Save. Changes can take a few minutes to apply.
- Set quota caps on each Maps API so a spike can't run all month, and add a budget alert in Billing. Budget alerts notify you; they don't stop spending.
https://example.com/*
https://www.example.com/*
https://*.example.com/*A referrer restriction stops other websites reusing your key, but a script outside a browser can fake the Referer header. That's why the API restriction and quota caps matter too: they limit what a determined person can do.
Why your server needs a separate Maps key
If your server also calls Geocoding or Places web services, don't reuse the browser key. Referrer restrictions don't apply to server-side calls. Create a second key, restrict it by IP address if your server has a fixed outbound IP, restrict it to only the web service APIs it calls, and keep it in a server-only env var.
Serverless hosts often don't have a fixed IP. In that case the server key relies on its API restriction, quota caps and simply never leaving the server.
How to test your Maps key and handle abuse
Call a web service API with the key from your terminal, where there's no referrer:
curl "https://maps.googleapis.com/maps/api/geocode/json?address=London&key=YOUR_KEY"A status of REQUEST_DENIED mentioning referer restrictions, or a message that the API isn't allowed for this key, is what you want. A status of OK with results means anyone can use the key from anywhere.
- Add both restrictions now. That stops casual reuse within minutes.
- If the key ran unrestricted and usage looks wrong, create a new restricted key, deploy it, then delete the old one.
- Review the API metrics and billing reports per API to see what was used and when.
- If there are charges from abuse, raise it with Google Cloud billing support. There's no guarantee of a refund, which is why quotas are worth setting up front.
If our scan finds an AIza key in your page HTML or your site's JavaScript, it flags the key so you can confirm it's restricted. It can't see a key's restrictions from outside, so the curl above is how you check.
Questions
Is it safe to put my Google Maps API key in my HTML?
Yes, that's how the Maps JavaScript API works. It's safe once the key has an HTTP referrer restriction and an API restriction.
Should I hide the key in an environment variable?
Put it in one for tidiness, but a variable with NEXT_PUBLIC_ or VITE_ ends up in the bundle anyway. Restrictions are the protection, not hiding.
Why did GitHub flag my Maps key?
GitHub secret scanning recognises the Google API key format. For a browser Maps key that's expected, so use the alert to confirm the key is restricted.
Can I use one key for Maps and Firebase?
You can, but separate keys are easier to restrict and easier to replace. Give each job its own key with only the APIs it needs.