SKU // 131

JS APIs Intro

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.

// What an API Is

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.

// Two Kinds You Will Meet

Kind How you reach it Examples
Browser (Web) APIsBuilt into the browser. You call them as JavaScript objects and functionsfetch, localStorage, navigator.geolocation, Worker
Server (Web service) APIsRun on someone else's server. You call them by sending a request to a URL and get data back, usually JSONa 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.

// A Tour of Browser APIs

API Lets you
DOMread and change the page (you used it in JSON HTML)
Fetchsend requests to servers
Web Storagesave small amounts of data in the browser
Geolocationfind where the device is, with permission
Pointer Eventsreact to mouse, finger, and pen in one way
Constraint Validationcheck form fields
Web Workersrun heavy work without freezing the page
Canvas, Audio, Notifications, Clipboard...many more: drawing, sound, alerts, copy and paste

// Is It There? Check Before You Use It

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);
{fetch: true, storage: true, workers: true, geolocation: true, notifications: true, observer: true}

// Using an API: The Pattern

Almost every API follows the same steps:

  1. Find the documentation. It lists the names, the arguments, and what you get back
  2. Check support. Is the feature there? Does it need a secure page (https) or permission?
  3. Call it correctly. The right method, the right options
  4. Wait if it is asynchronous. Many calls return a promise or take callbacks, because they take time
  5. Handle failure. Permission denied, no network, bad input: plan for each

// Calling a Server API

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);
https://api.example.com/v1/scores?student=Ava&limit=3

// Reading the Server's Answer: Status Codes

Every response has a three-digit status code. The first digit tells you the family:

Range Means Examples
2xxsuccess200 OK, 201 Created
3xxredirect301 Moved Permanently
4xxyou asked wrongly401 not logged in, 403 not allowed, 404 not found, 429 too many requests
5xxthe server failed500 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));
["success", "success", "not found", "slow down", "server problem"]

// Keys, Limits, and Safety

How this section is organized
Each lesson that follows takes one browser API: Fetch (in depth), Geolocation, Pointer Events, Web Storage, Validation, and Web Workers. They all follow the pattern above.

// Quick Check

Which is an example of a browser API (built into the browser)?

  1. A weather company's URL that returns JSON
  2. localStorage
  3. A school's private student database
  4. A text file on your computer
Show answer
B. localStorage 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?

Show answer
Anyone can open the page and read its JavaScript, so they could copy the key and use it. Secret keys belong on a server.

What does this log?

const u = new URL("https://api.example.com/items");
u.searchParams.set("page", 2);
console.log(u.search);
Show answer
?page=2. The search property is the query string, including the ?.

// Try It

  1. Click Open Playground and run it. Which APIs are available in your browser?
  2. Add one more check for an API you are curious about, such as "share" in navigator
  3. Build a URL for a made-up API with three query values, one of which has a space
  4. Write the describe function so it also reports "redirect" for status 301
// Video Walkthroughs
Last updated: Oct 11, 2026, 7:41 PM ET