What Is an API?

An API (Application Programming Interface) is the list of requests a client (the program that asks) may make to a server (the program that answers), plus the…

The weather widget on your phone shows 31°C, and rain this afternoon. But your phone has no thermometer outside the window. The weather app asks a server for the data: a program on a computer that belongs to the weather service. The server answers, and the app shows the numbers. The driver dot that moves on the map in a ride-hailing app and the "Sign in with Google" button work the same way: one program asks, another program answers.

The asking has rules. The two programs have agreed on them: what may be asked for, and what the answer looks like. Those rules are called an API (Application Programming Interface). Think of eating at a restaurant. You don't walk into the kitchen. You read the menu, then tell the waiter your order. The waiter takes the order to the kitchen and comes back with the food. In that restaurant, you who order are the client, the kitchen that cooks is the server, and the menu together with the waiter is the API. You can't order food that isn't on the menu. An API is the same: a client can only ask for what is on its list.

Client, server and the API

Throughout this track we use one example that keeps growing: the Bookshop API at https://api.bookshop.test. The .test ending is reserved for examples, so this address doesn't exist on the internet. The API has two kinds of data: books (books) and authors (authors).

This is the Bookshop API's menu at the start (GET means "get this"):

Request The answer
GET /books the list of all books
GET /books/1 one book, number 1
GET /authors the list of all authors

The Bookshop website and its phone app don't each have their own database. Both ask the same API, so the data they show is always the same.

Request and response

The client and the server communicate with HTTP, the protocol (the rules for communicating) that the web uses to send requests and answers. One message that asks is called a request, and its answer is called a response.

This is the request from the app, which asks for book number 1:

GET /books/1 HTTP/1.1
Host: api.bookshop.test
Accept: application/json

This is the server's response:

HTTP/1.1 200 OK
Content-Type: application/json

{"id": 1, "title": "The Rainbow Troops", "author_id": 1, "price": 89000, "stock": 12}

Now the app asks for book number 99, which doesn't exist. This is the response:

HTTP/1.1 404 Not Found
Content-Type: application/json

{"error": "Book not found"}

The server doesn't stop working (crash) and doesn't stay silent. It still answers, with the status 404 Not Found and a body that explains the problem. The client reads the status code first, then decides what to show: the book's data, or a "book not found" message.

A request that isn't on the menu still gets an answer too. At the start, the Bookshop API can only be read. So the request DELETE /books/1 (delete book number 1) is answered with 405 Method Not Allowed: that method is not allowed. The answer also carries the header Allow: GET, the list of methods that are allowed at that address.

REST: addresses for things, methods for actions

REST is a style of designing an API on top of HTTP, and it is the most widely used one. Its two main rules:

  1. Every thing (called a resource), such as a book, has its own address. That address is written as a noun: /books, /books/1, /authors.
  2. The action is chosen with the method: GET reads, POST creates, PUT or PATCH changes, DELETE deletes.

So a URL doesn't need a verb in it. Compare:

Not quite right REST style
POST /api/deleteStudent DELETE /api/students/5
GET /getBooks GET /books

POST /api/deleteStudent has two mistakes. First, there is a verb (delete) in the URL. Second, the method is POST, but the action deletes. The method should say "delete", and the URL only needs to point at which student.

REST is not the only style. Other options you will meet:

Style What it looks like When to pick it
REST DELETE /api/students/5 The default for general web APIs, and the style this track uses, because it is easy to try out and its answers are easy to cache.
GraphQL one address, POST /graphql, and the client writes the fields it needs One screen needs data from many tables at the same time, for example in a phone app. Covered in the GraphQL Basics lesson.
RPC (gRPC, tRPC, JSON-RPC) calling a function by its name, for example POST /api/deleteStudent Your own servers calling each other, or a TypeScript frontend and backend in one project (tRPC).
WebSocket, SSE (Server-Sent Events), webhook the server sends news first Real-time data such as chat, or news from another server (webhooks, covered in the Webhooks lesson).

So POST /api/deleteStudent on its own is normal in an RPC-style API. The mistake is mixing it into an API that uses the REST style.

Your first endpoint

The exercise editor uses a small Express-style app, and the following lessons use the same app. Express is the most popular library (ready-made code) for building a server with Node.js, a program that runs JavaScript outside the browser. In this lesson's exercise, the code below is already in the editor:

app.get("/api/status", (req, res) => {
  res.json({ status: "ok", message: "API is running!" });
});

When you click Run, the tester (the program that checks the exercise) sends GET /api/status to your code and checks that the answer's status is 200, with a JSON body that isn't empty.

⚠️ What people get wrong about APIs at first

TL;DR: An API (Application Programming Interface) is the list of requests a client (the program that asks) may make to a server (the program that answers), plus the shape of the answers. The client sends a request over HTTP, the web's rules for communicating. A request has a method (the action, such as GET), a path (the address of the data), headers (extra notes) and a body (the content). The server sends back a response: a status code (three digits that tell the result), headers and a body. Data that doesn't exist still gets an answer, 404 Not Found. A method that isn't allowed at an address (such as DELETE /books/1 on an API that can only be read) gets 405. REST is a style of designing an API: every resource (a thing, such as a book) has a noun address (/books/1), and the action is chosen with the method (GET, POST, PUT/PATCH, DELETE). So there are no verbs in the URL. In the exercise, app.get(path, (req, res) => res.json(...)) registers an endpoint (one address in the API) on the small server app. That endpoint answers with JSON (a text format for data) and status 200.

⚡ The client asks, the server answers

Meaning: The client is the program that asks, such as a website or a phone app. The server is the program that answers, and it is the one holding the database (the place where data is stored). The API is the list of requests a client may make to the server, plus the shape of the answers. An API works like a restaurant menu: what isn't on the menu can't be ordered.

⚡ Missing data still gets an answer

Guess first: the client sends the request GET /books/99 (get book number 99), but book 99 doesn't exist. Does the server stay silent?

No. The server still answers, with the status 404 Not Found and a body (the content of the answer) that explains the problem: {"error": "Book not found"}.

Meaning: Every response (the server's answer) carries a status code: three digits that tell the result. 200 means success, and 404 means that what was asked for doesn't exist. The client reads the status code first, then decides what to show.

⚡ Nouns in the URL, actions in the method

Guess first: an API deletes a student's data with POST /api/deleteStudent. POST is the method (the verb of the request), and it means "create". What is wrong?

Two things. There is a verb (delete) in the URL, and the method is POST while the action deletes. In REST, the most common style of designing an API, the right form is DELETE /api/students/5: the method DELETE says the action, and the URL points at student number 5.

Meaning: In REST, every resource (a thing, such as a book) has a noun address (/books/1). The action is chosen with the method: GET reads, POST creates, PUT or PATCH changes, DELETE deletes.

⚡ Your first endpoint

Write:

app.get("/api/status", (req, res) => {
  res.json({ status: "ok", message: "API is running!" });
});

Meaning: app is a small server that the exercise already provides. app.get registers the endpoint GET /api/status: one address that can be called in the API. res is what you answer with: res.json sends the object inside its brackets as JSON (a text format for data), with status 200 unless you set another one.

What you will learn