132

How do I get Google Maps API key to work on localhost?

I've created an API key and under referrers I add the following:

Accept requests from these HTTP referrers (websites) (Optional)

Use asterisks for wildcards. If you leave this blank, requests will be 
accepted from any referrer. Be sure to add referrers before using this key 
in production. 

localhost

This doesn't work and if I exclude the API key it doesn't work, either.

Null
  • 1,950
  • 9
  • 30
  • 33
adam78
  • 9,668
  • 24
  • 96
  • 207
  • Possible duplicate of [How to set Google API key restriction - HTTP referrers](https://stackoverflow.com/questions/41993276/how-to-set-google-api-key-restriction-http-referrers) – viktorianer Jan 26 '18 at 14:40

10 Answers10

212
  1. Go to this address: https://console.developers.google.com/apis
  2. Create new project and Create Credentials (API key)
  3. Click on "Library"
  4. Click on any API that you want
  5. Click on "Enable"
  6. Click on "Credentials" > "Edit Key"
  7. Under "Application restrictions", select "HTTP referrers (web sites)"
  8. Under "Website restrictions", Click on "ADD AN ITEM"
  9. Type your website address (or "localhost", "127.0.0.1", "localhost:port" etc for local tests) in the text field and press ENTER to add it to the list
  10. SAVE and Use your key in your project
Sajad Mirzaei
  • 2,635
  • 1
  • 11
  • 13
  • 23
    This is what I did as well, but I wonder: does this mean that ANYONE working locally (localhost / 127.0.0.1) might use my API KEY (and possibly infer costs for me after the new pricing on 2018-06-11 takes place?) – tmanolatos Jun 04 '18 at 11:30
  • 58
    @tmanolatos Yes, that is what it means. It is not good practice to give your production key localhost rights, since anyone can use your key while running locally. Instead you should maintain two keys. One for production which can be seen by anyone and hence should only be allowed on your domain. One for development which should be treated as credentials (do not share it) and allows access from localhost. – the_cheff Jun 06 '18 at 09:54
  • 3
    @tmanolatos Yes, but you must build two key, one to test app, and one for the main app, – Sajad Mirzaei Sep 24 '18 at 06:13
  • 1
    I am getting this response after following the above steps: { "error_message" : "API keys with referer restrictions cannot be used with this API.", "results" : [], "status" : "REQUEST_DENIED" } Here is the URL, where DEV_KEY is substituted with my api key: https://maps.googleapis.com/maps/api/geocode/json?key=DEV_KEY&address=1600+Amphitheatre+Parkway,+Mountain+View,+CA – rmutalik Jan 14 '20 at 19:49
  • It returns correct JSON when I use my PROD_KEY api key. – rmutalik Jan 14 '20 at 19:51
  • @tmanolatos well, if you put in your etc/host the matching 127.0.0.1 - domain,(or you use a local reverse proxy), you can use the key for that domain developing locally... – izio Jul 24 '20 at 13:18
  • Seems like the UI changed from step 5 onwards – Ben Carp Jul 27 '20 at 02:54
  • 1
    @the_cheff Anyone can set any referrer header. – Julius S. Jun 21 '21 at 10:05
15

You can follow this way. It works at least for me :

in Credential page :

  1. Select option with IP address ( option no. 3 ).

  2. Put your IP address from your provider. If you don't it, search your IP address by using this link : https://www.google.com/search?q=my+ip

  3. Save it.

  4. Change your google map link as follow between the scrip tag :

    https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzxxxxxxxx"

  5. Wait for about 5 minutes or more to let your API key to propagate.

Now your google map should works.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
Sulung Nugroho
  • 1,605
  • 19
  • 14
15

If you are working on localhost, create a separate APIkey for your development and then remove restrictions on that key so that your localhost can use it. Remember to not use this key on production, so you don't expose your key to hunters online.

I had the same issue and all attempts to get the restrictions working on my localhost environment was not successful until I created a separate apikey specifically for development and then removed its restrictions. However I don't use that key on production environment, and once am done with developments, I will delete the API key immediately.

I Know this post is late, but for people that will likely face this issue in the future, this is the best route to go.

Jeff C
  • 319
  • 3
  • 9
7

Guess I'm a bit late to the party, and although I agree that creating a seperate key for development (localhost) and product it is possible to do both in only 1 key.

When you use Application restrictions -> http referers -> Website restricitions you can enter wildcard urls.

However using a wildcard like .localhost/ or .localhost:{port}. (when already having .yourwebsite.com/* ) don't seem to work.

Just putting a single * does work but this basicly gives you an unlimited key which is not what you want either.

When you include the full path withhout using the wildcard * it also works, so in my case putting:

http://localhost{port}/
http://localhost:{port}/something-else/here

Makes the Google maps work both local as on www.yourwebsite.com using the same API key.

Anyway when having 2 seperate keys is also an option I would advise to do that.

Patrick
  • 71
  • 1
  • 4
3

It didn't work for me when I used

http://localhost{port}/
http://localhost:{port}/something-else/here

However, removing the http did the trick for me. I just added localhost:8000 without prefixing it with the http.

Dharman
  • 30,962
  • 25
  • 85
  • 135
M.Z.
  • 424
  • 5
  • 12
2

You can follow this tutorial on how to use Google Maps for testing on localhost.

Check these related SO threads:

Hope this helps!

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
1

You have to check the specific error within the javascript console (e.g. Ctrl + Shift + K in Firefox for Windows).

According to Steven Gliebe (2016), there are four common cases for this problem. If I may summarize it, as this:

  1. MissingKeyMapError >> Get Google Maps API Key (but also consider alternative no.2)
  2. RefererNotAllowedMapError >> Register your localhost:port in your google developer dashboard.
  3. ApiNotActivatedMapError >> Enabling the Google Maps API in Google API Library page
  4. InvalidKeyMapError >> Add your key to your scripts/ codes properly

After doing some code modification, please clear your browser cache as necessary.

In case there are other errors, you can check Google Maps API Error Codes Documentation page.

Santosa Sandy
  • 217
  • 6
  • 20
1

Typing 'my IP' in google search I got my public IP address and pasted it in IP address (the third option). It works for me.

Alaa ma
  • 21
  • 1
0

Where it says "Accept requests from these HTTP referrers (websites) (Optional)" you don't need to have any referrer listed. So click the X beside localhost on this page but continue to use your key.

It should then work after a few minutes.

Changes made can sometimes take a few minutes to take effect so wait a few minutes before testing again.

GMC
  • 107
  • 3
-1

Go to this address: https://console.developers.google.com/apis

Create new project and Create Credentials (API key) Click on "Library" Click on any API that you want Click on "Enable" Click on "Credentials" > "Edit Key" Under "Application restrictions", select "HTTP referrers (web sites)" Under "Website restrictions", Click on "ADD AN ITEM"

Add * and hit an enter SAVE and Use your key in your project

Anj
  • 99
  • 5