API Requests and JSON

5.A Menu for Programs

A

In this chapter

We'll learn what an API request is — a program ordering from another program's menu — and how data travels in JSON, the web's standard packing list, including request bodies and response bodies.

12–14 min

The Problem in Real Life

Anna filters the Network tab to show only requests whose path starts with /api/. While she picks a seat, five of them appear. She clicks one: POST /api/holds. Instead of a web page, the response is a short block of text full of curly brackets and quotes:

{ "holdId": "h_9182", "seatId": "C14", "heldUntil": "2026-11-14T10:10:00Z" }

"This isn't a page," she says. "It's... data?"

J

Pages are for people. APIs are for programs. Most of what your browser does today is the second kind.

John

Pages for People vs. Data for Programs

Programs need to talk

The seat map, the mobile app and partner websites all need BlueTicket's data — not its web pages.

A shared format

Different languages and devices need one simple, agreed way to write data down.

Data goes both ways

Some requests send data in (a new hold); responses send data back (the hold's details).

API Requests and JSON

The restaurant menu analogy: a restaurant's menu tells you exactly what you can order, what to say, and what you'll get back. You don't walk into the kitchen and cook; you order from the menu. An API (Application Programming Interface) is a menu for programs: a list of things one program can ask another to do, exactly how to ask, and what it will get back (we met the word in Act 04).

API request — an order from the menu: an API request is an HTTP request made by a program, asking for data or an action instead of a web page. When Anna clicks a seat, JavaScript on the page sends POST /api/holds to BlueTicket's server: "please hold seat C14 for me." The server answers with data, and the JavaScript updates the seat's colour — without reloading the page. The mobile app sends the very same API requests.

JSON — the web's standard packing list: to send data between programs written in different languages, everyone needs one simple format. JSON (JavaScript Object Notation) is that format: plain text, easy for people to read and for every language to understand. It looks like the JavaScript objects from Act 07.

  • Objects are written in curly brackets, with keys in double quotes and values after a colon: { "seatId": "C14" }.
  • Values can be a string "C14", a number 4999, a boolean true or false, null (nothing), an array in square brackets ["C14", "C15"], or another object.
  • Strict rules: keys must use double quotes, strings must use double quotes, and there's no trailing comma after the last item. One wrong comma and the whole message can't be read — the same "exactness" as code syntax (Act 07).
Table — JSON value types
TypeExampleUsed for
String"C14"Text, IDs, dates
Number4999Prices in cents, counts
BooleantrueYes/no facts
Nullnull"No value"
Array["C14", "C15"]Lists
Object{ "seatId": "C14" }A thing with named details
Table — Web page request vs. API request
FeatureWeb page requestAPI request
Who asksThe browser, for a personA program (page script, app, partner)
What comes backHTML to displayData, usually JSON
Typical path/events/festival/api/events/42/seats
After the responseA new page is shownThe program updates part of the screen

Holding a seat through the API

Fan clicks seat C14

JavaScript on the page

API request

POST /api/holds

body: { "seatId": "C14" } · Content-Type: application/json

Server

validates, holds the seat (Act 08), saves it

API response

201 Created + JSON body

{ "holdId": "h_9182", "heldUntil": "..." }

Page updates without reloading

seat turns yellow, countdown starts

An API request from JavaScript
const response = await fetch("/api/holds", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ seatId: "C14" }), // request body: object -> JSON text
});
const hold = await response.json(); // response body: JSON text -> object
console.log(response.status); // 201
console.log(hold.heldUntil); // "2026-11-14T10:10:00Z"

JSON.stringify turns an object into JSON text to send; response.json() turns the JSON text that comes back into an object.

Request body and response body — what's in the box each way: an API request can carry a request body: the data being sent in, usually as JSON. Creating a hold sends { "seatId": "C14" } in the request body, with a header Content-Type: application/json that says "the box contains JSON." The response body is the data coming back: the hold's ID and when it expires. GET requests usually have no request body — everything they need is in the URL.

Why APIs changed the web: older websites rebuilt the whole page on the server for every click. Modern websites load the page once, then use small API requests for everything else. It's faster, and it means one API can serve many clients at once: BlueTicket's website, its iPhone and Android apps, the organisers' dashboard, and even partner sites that sell BlueTicket tickets — all ordering from the same menu.

Anna reads the hold response again, and now it reads like a sentence: "Hold h_9182 is for seat C14 and lasts until 10:10." The JavaScript on the page reads that and starts the ten-minute countdown — her Act 08 seat-hold design, working for real.

Key Takeaway

An API is a menu for programs: what one program can ask another to do, how to ask, and what comes back. An API request is an HTTP request for data or an action rather than a page. JSON is the standard packing list for that data — objects in curly brackets, keys and strings in double quotes. Data sent in travels in the request body; data returned travels in the response body.

Why This Matters

Almost every modern app is built on APIs — web front ends, mobile apps, payment providers, maps, email services. Reading API requests and JSON in the Network tab is one of the most common things a developer does. In Act 23, BlueTicket connects to payment and SMS companies through exactly these kinds of API requests.

Anna has seen a dozen /api/ paths now — /api/holds, /api/events/42, /api/events/42/seats — and they seem to follow a pattern. They do. It's called REST, and it's how most APIs on the web are designed.