Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to program geocoding. I created API key, but when I add it into my website code I don't get anything, however when I don't use API everything works well. When I'm sending:

https://maps.googleapis.com/maps/api/geocode/json?address=".$adres."&key=KLUCZ_XXX

I get a response:

Browser API keys cannot have referer restrictions when used with this API.

If I use:

https://maps.googleapis.com/maps/api/geocode/json?address=".$adres" 

The result is correct but the number of queries is limited per day.

Key constraint: referrals HTTP (Web) - set to the domain from which conjure inquiry. Interface Google Maps Geocoding API is turned on.

Does the API key has to be activated also somewhere else to work?

It sounds like you may be making the API call server side. I ran into this issue earlier today. Since you've placed a referrer restriction on your API key, it will be limited to executing on the browser with the web service APIs, including the Geocoding API. It didn't work for me even when manually setting the referrer in the request headers. You can find the other web service APIs on this page: https://developers.google.com/maps/web-services/

Important: If you are using any of the web service APIs with an API key that has referer restictions, your requests will fail with the error message: "API keys cannot have referer restrictions when used with this API." You should switch to using a server restriction.

You'll want to create a separate key to use server-side. You can change your restriction from a browser restriction to a server restriction by using IP addresses to restrict access, instead of browser referrers.

See this section in the Maps APIs FAQ on switching key type to a server restricted key: https://developers.google.com/maps/faq#switch-key-type

Alternatively, you can continue using a browser key and geocode client-side (JavaScript), then return the result to the backend via ajax.

But if the key doesn't have any restrictions ie the "none" is selected there. Will this error come when we use the key in server side? – Sanjay Kumar N S Sep 4, 2018 at 10:56 @SanjayKumarNS I also came through such scenario. I had key which has restriction as 'none' then it worked properly. – Arvind Kumar Nov 5, 2019 at 6:54 This is by far the most Google thing Google could have done. They literally force you to make 2 API keys if you want to do any kind of backend validation (eg. if you want to save a place's ID and name in the database, you can't validate it using their API without a second key). Bravo, Google! – mike.void Sep 5, 2022 at 15:40

If server-side geocoding is not an option, you should use the geocoder from the Google Javascript API. You can set HTTP referer restrictions on that API.

Google itself says to avoid the Non-Javascript Geocoder API for dynamic geocoding:

This service is generally designed for geocoding static (known in advance) addresses for placement of application content on a map; this service is not designed to respond in real time to user input. For dynamic geocoding (for example, within a user interface element), consult the documentation for the Maps JavaScript API client geocoder and/or the Google Play services Location APIs.

This issue happens when you set the referrer for the server key to the domain name instead of the IP address, then it will show this message -

API keys with referer restrictions cannot be used with this API

For geocode API create a separate API key and restrict that API using IP address.

It worked for me.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.