Good API Design

A good API is easy to guess: once you know one endpoint, you can guess the others. The path holds plural nouns (/books, /books/9), and the action goes through…

A teammate proposes a new endpoint: POST /deleteBook?id=9. It works, and the book gets deleted. But other developers can't guess its shape, so they have to open the documentation every time they want to use it. Before you read on, count: how many things in that endpoint make it hard to guess? The answer is in the first section below.

A good API is easy to guess: once you know one of its endpoints, you can guess the others without opening the documentation. Think of the floor plan of a well-kept library. The shelves are named after things ("Novels", "Authors"), the books are numbered in order, and borrowing works the same at every shelf. Once you understand one shelf, you understand them all. In an API, the shelf name is a path made of a noun, the book number is the id, and borrowing is a method that works the same for every resource. A carelessly built API is like a library where every shelf has its own rules.

Plural nouns, the method as the verb

The path names the thing (the resource), and the method names the action. The collection is written in the plural, and one item in that collection is picked with its id in the path:

Not quite right Better Why
GET /getBooks GET /books GET already means "fetch"
GET /book/1 GET /books/1 the collection is plural; the book is number 1 in it
POST /books/create POST /books POST to a collection already means "create"
PUT /Books/7/Update_Stock PATCH /books/7 with the body {"stock": 7} lowercase, no verb, a partial change uses PATCH

POST /deleteBook?id=9 has three problems:

The tidy version is DELETE /books/9. The same pattern holds for any resource: every blog post is fetched with GET /api/posts, not GET /api/getPost.

Path names are kept uniform too: all lowercase, words joined with a hyphen when needed (/best-sellers), and no .json ending.

Nesting, but not too deep

A thing that belongs to another thing can be nested: the owner's path (the parent) comes first. One level is enough:

GET /authors/2/books
GET /books/7/reviews

The first line asks for the books by author 2, and the second line asks for the reviews of book 7. GET /authors/2/books/7/reviews is too deep. Book 7 already has its own address (/books/7), so its reviews are just GET /books/7/reviews. A practical rule: at most one parent in front, /parent/id/child (the child is the thing that belongs to the parent).

Filtering, sorting and paging don't need a new path. The query string is enough, as in the URLs & Endpoints and Pagination lessons: GET /books?author_id=2&sort=-price&page=2 (books by author 2, highest price first, page 2). Don't make /getBooksByAuthor/2.

Consistent responses

Answers have the same shape at every endpoint, so the frontend only writes one way to read them:

Situation Status Body
A list 200 {"data": [...], "meta": {...}}
One item 200 {"data": {...}}
Created 201 {"data": {...}, "message": "Book created"}
Not found 404 {"error": "Book not found"}

This is the endpoint for one book, with that answer shape:

app.get("/books/:id", (req, res) => {
  const book = books.find((b) => b.id === Number(req.params.id));
  if (!book) return res.status(404).json({ error: "Book not found" });
  res.json({ data: book });
});

A response like {"s": 1, "d": [{"n": "Budi"}]} does save a few bytes, but its key names aren't descriptive (they don't say what they hold). Whoever reads it has to guess what s, d and n mean. Write {"success": true, "data": [{"name": "Budi"}]}.

Versioning: /api/v1

An API is a promise to every client. Say one day a response has to change shape: title becomes book_title. An old version of the phone app that still reads title breaks right away. That's why the path carries a version number:

GET /api/v1/books
GET /api/v2/books

A change that breaks old clients (a breaking change) goes into /api/v2, while /api/v1 keeps working for old clients. This is called keeping backward compatibility: a new version doesn't break old clients. The details are in the API Versioning lesson.

⚠️ When does an API stop being easy to guess?

TL;DR: A good API is easy to guess: once you know one endpoint, you can guess the others. The path holds plural nouns (/books, /books/9), and the action goes through the method (GET, POST, PUT/PATCH, DELETE). So there's no verb in the URL: POST /deleteBook?id=9 becomes DELETE /books/9. Paths are lowercase, nesting (one path inside another) goes at most one level deep (/books/7/reviews), and filtering or sorting goes through the query string. Responses are consistent with descriptive keys: {"data": ...} for success, {"error": "..."} with the right status for failure, 201 plus message for new data. Put a version in the path (/api/v1/posts). A change that breaks old clients goes into v2, and v1 keeps working (backward compatibility).

⚡ Plural nouns, the method as the verb

Guess first: how many problems does POST /deleteBook?id=9 have?

Three. There's a verb (delete) in the URL. The method is POST, though the action deletes. The book's id sits in the query string, while one item is picked through the path. The tidy version is DELETE /books/9.

Meaning: The path names the thing in the plural (/books for all books, /books/9 for book number 9), and the method names the action.

⚡ One level of nesting

Meaning: A thing that belongs to another thing can be written nested. GET /authors/2/books, the books by author 2, is clear. GET /authors/2/books/7/reviews is too deep: book 7 already has its own address (/books/7), so its reviews are just GET /books/7/reviews. Filtering and sorting go in the query string, not a new path.

⚡ Consistent responses

Write:

app.get("/books/:id", (req, res) => {
  const book = books.find((b) => b.id === Number(req.params.id));
  if (!book) return res.status(404).json({ error: "Book not found" });
  res.json({ data: book });
});

Meaning: Success is always wrapped in { data: ... }, and failure is always {"error": "..."} with the right status, such as 404. So the frontend (the part that shows in the browser) only writes one way to read the answers.

⚡ A version in the path

Meaning: The path carries a version number, such as /api/v1. If the key title in the response becomes book_title, an old version of the phone app that still reads title breaks right away. A change that breaks old clients (a breaking change) goes into /api/v2, while /api/v1 keeps working for old clients. This is called keeping backward compatibility.

What you will learn