Googlemaps API Key for Localhost
Solution 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"
- 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
- SAVE and Use your key in your project
Solution 2
You can follow this way. It works at least for me :
in Credential page :
Select option with IP address ( option no. 3 ).
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
Save it.
-
Change your google map link as follow between the scrip tag :
https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzxxxxxxxx"
Wait for about 5 minutes or more to let your API key to propagate.
Now your google map should works.
Solution 3
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.
Solution 4
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.
Solution 5
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.
Related videos on Youtube
adam78
Updated on October 01, 2021Comments
-
adam78 over 2 years
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.
-
viktorianer about 6 yearsPossible duplicate of How to set Google API key restriction - HTTP referrers
-
-
Rafael Herscovici almost 7 yearsbroken link is broken
-
AE Grey almost 7 years@Dementic console.developers.google.com/flows/…
-
Rafael Herscovici almost 7 years@AEGrey - not for me, for future users.
-
tmanolatos almost 6 yearsThis 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?)
-
the_cheff almost 6 years@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.
-
Sajad Mirzaei over 5 years@tmanolatos Yes, but you must build two key, one to test app, and one for the main app,
-
rmutalik over 4 yearsI 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: maps.googleapis.com/maps/api/geocode/…
-
rmutalik over 4 yearsIt returns correct JSON when I use my PROD_KEY api key.
-
izio over 3 years@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...
-
Ben Carp over 3 yearsSeems like the UI changed from step 5 onwards
-
Julius S. almost 3 years@the_cheff Anyone can set any referrer header.
-
ALFmachine over 2 yearshttp explicit protocol worked for me. thank you.