Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of a javascript API key when it can be seen to anyone viewing the js code

I've seen services like Google that require you add an API key when making a javascript call, like this.

https://www.google.com/jsapi?key=thekeygoeshere

What's the point of having this javascript api key when the code can be seen and the key can be read. Can't someone just copy this key and use it for their own site? Or is there something else that they do in the background to ensure that the key belongs to the site making the call?

like image 668
zmol Avatar asked Feb 05 '11 14:02

zmol


People also ask

What is API key in JavaScript?

An API key is usually a unique string of letters and numbers. In order to start working with most APIs, you must identify yourself (register) and get an API key. You will need to add an API key to each request so that the API can recognize you.

How do I hide my API key?

The only way to hide it is to proxy your request through your own server. Netlify Functions are a free way to add some simple backend code to a frontend app. This is this method I used while learning to program in college, where I needed to share my progress with my peer group without disclosing my API keys.

Why isn't my Google Maps API working?

There are a several reasons why your google maps may not be working, the most common issue being no Google Map API key set or set incorrectly. To use the Google Maps JavaScript API, you must register your app project on the Google Cloud Platform Console and get a Google API key which you can add to your app.


1 Answers

Presumably they check the referer HTTP header.

The majority of users send it. So if is:

  • The site that matches the key, they can operate as normal.
  • A site that doesn't match the key, they can reject the request.
  • Blank, they can operate as normal and allow a tiny fraction of people to use the API on the wrong site.

The majority of visitors to a site using the wrong key will get blocked, so it won't be worth using the wrong key on the site in the first place.

like image 115
Quentin Avatar answered Oct 05 '22 22:10

Quentin