An API is a set of ready-made tools you can use without knowing how they work inside. You have used APIs all along: Math, Date, JSON, and fetch are all APIs. This section is about the ones built into the browser, which let your pages talk to servers, find your location, save data, and run work in the background. This lesson explains what an API is, the two kinds you will meet, and the habits that apply to all of them.
API stands for Application Programming Interface. Think of a restaurant menu: you do not need to know how the kitchen works. You read what is on offer, ask for it in the right way, and a result comes back. An API is the menu for a piece of software: a list of things you can ask for, and how to ask.
| Kind | How you reach it | Examples |
|---|---|---|
| Browser (Web) APIs | Built into the browser. You call them as JavaScript objects and functions | fetch, localStorage, navigator.geolocation, Worker |
| Server (Web service) APIs | Run on someone else's server. You call them by sending a request to a URL and get data back, usually JSON | a weather service, a maps service, a school's student list |
The two work together. A browser API (fetch) is the tool you use to call a server API (a URL that returns JSON). This section teaches the browser ones in depth.
| API | Lets you |
|---|---|
| DOM | read and change the page (you used it in JSON HTML) |
| Fetch | send requests to servers |
| Web Storage | save small amounts of data in the browser |
| Geolocation | find where the device is, with permission |
| Pointer Events | react to mouse, finger, and pen in one way |
| Constraint Validation | check form fields |
| Web Workers | run heavy work without freezing the page |
| Canvas, Audio, Notifications, Clipboard... | many more: drawing, sound, alerts, copy and paste |
Browsers differ in age and settings, so an API you want may be missing. Check first, as you did with navigator in the Navigator lesson:
const checks = {
fetch: "fetch" in window,
storage: "localStorage" in window,
workers: "Worker" in window,
geolocation: "geolocation" in navigator,
notifications: "Notification" in window,
observer: "IntersectionObserver" in window
};
console.log(checks);
Almost every API follows the same steps:
A server API is just a URL with some options in the query string. Build it with the URL object so the encoding is right:
const url = new URL("https://api.example.com/v1/scores");
url.searchParams.set("student", "Ava");
url.searchParams.set("limit", 3);
console.log(url.href);
Every response has a three-digit status code. The first digit tells you the family:
| Range | Means | Examples |
|---|---|---|
| 2xx | success | 200 OK, 201 Created |
| 3xx | redirect | 301 Moved Permanently |
| 4xx | you asked wrongly | 401 not logged in, 403 not allowed, 404 not found, 429 too many requests |
| 5xx | the server failed | 500 error, 503 unavailable |
function describe(status) {
if (status >= 200 && status < 300) return "success";
if (status === 401 || status === 403) return "not allowed";
if (status === 404) return "not found";
if (status === 429) return "slow down";
if (status >= 500) return "server problem";
return "other";
}
console.log([200, 201, 404, 429, 503].map(describe));
429. Cache answers and do not call in a tight loopWhich is an example of a browser API (built into the browser)?
localStoragelocalStorage is built into the browser. The others are services or files, not browser APIs.Why should a secret API key never be written into front-end JavaScript?
What does this log?
const u = new URL("https://api.example.com/items");
u.searchParams.set("page", 2);
console.log(u.search);
?page=2. The search property is the query string, including the ?."share" in navigatordescribe function so it also reports "redirect" for status 301