Practice: REST API design
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Knowing the rules is not the same as applying them. Real work starts from a list of requirements or from someone else's messy API, and the same routine fixes both.
From requirements to endpoints: a college library
Requirements to endpoints takes four passes: underline the nouns, decide how they relate, map each operation to a method, and map each rule to a status code.
Example 1 · Library requirements to an endpoint table
- The library lists books; anyone can search by title or author.- A librarian adds books and changes the number of copies.- Students register as members (roll number must be unique).- A member borrows a book (a loan) and returns it later.- A member may hold at most 3 books; a book with no free copy cannot be lent.- A member can see their own loans.GET /api/v1/books?q=&author=&page=&size= 200 page
POST /api/v1/books 201 | 400
PATCH /api/v1/books/{id} 200 | 400 | 404
POST /api/v1/members 201 | 400 | 409 (roll number taken)
GET /api/v1/members/{id}/loans 200 page | 404
POST /api/v1/loans 201 | 400 | 409 (3 books held, no free copy) | 422 (unknown book or member)
POST /api/v1/loans/{id}/return 200 | 404 | 409 (already returned)What this code does
- Nouns gave three resources: books, members, loans. A loan has its own id and links two parents, so it gets a top-level collection, and "my loans" is a nested list under the member.
- Each rule became a code: a taken roll number and "3 books held" clash with stored data (409); an unknown book id in the body is a broken reference (422).
The library has books, members and loans. A loan links one member to one book, so it is a resource of its own: POST /loans lends a book, and returning it is an action on the loan. The rule "a member may hold at most 3 books" becomes a 409 when a fourth loan is attempted.
Reviewing a bad design: a blog API
A design review asks the same questions of every line: is the URL a plural noun with ids in the path, does the method match, is the success code right, is the error code specific, is the list paged?
Example 2 · A blog API, reviewed line by line
GET /api/v1/getPosts 200GET /api/v1/post/7 200 with {} when missingPOST /api/v1/posts/create 200 { "msg": "ok" }PUT /api/v1/posts/7 body: { "title": "New title" } onlyGET /api/v1/posts/7/delete 200GET /api/v1/posts/7/comments all 40,000 comments at onceGET /api/v1/posts 200 no verb; plural collection GET /api/v1/posts/7 200 | 404 plural; 404 when missing POST /api/v1/posts 201 + Location + created post PATCH /api/v1/posts/7 200 only some fields sent -> PATCH DELETE /api/v1/posts/7 204 a GET must never delete GET /api/v1/posts/7/comments?page=0&size=20 paged, maximum size
What this code does
- Every line had one or two problems, and each fix carries its reason. The PUT was really a partial update, so the method changes, not the body.
- The comments list grows without limit, so it gets page and size like any other growing list.
Write the reason next to each fix. A review that says only "change this" teaches nothing, and the author will make the same mistake in the next API.
Writing the exchanges: bus seat booking
An endpoint table becomes a contract once it has exchanges: a full request and the full response for every success and every error.
Example 3 · Bus booking: success and a lost race
POST /api/v1/bookings HTTP/1.1Host: localhost:8080Content-Type: application/json{ "tripId": 512, "seatNumber": 14, "passengerName": "Neha Joshi", "phone": "9876500014"}POST /api/v1/bookings HTTP/1.1Host: localhost:8080Content-Type: application/json{ "tripId": 512, "seatNumber": 14, "passengerName": "Rahul Verma", "phone": "9876500021"}HTTP/1.1 201 Created
Location: http://localhost:8080/api/v1/bookings/30071
Content-Type: application/json
{
"id": 30071,
"tripId": 512,
"seatNumber": 14,
"passengerName": "Neha Joshi",
"fare": 850.00,
"status": "CONFIRMED",
"bookedAt": "2026-10-16T19:02:11+05:30"
}
HTTP/1.1 409 Conflict
Content-Type: application/problem+json
{
"type": "https://busgo.example/problems/seat-taken",
"title": "Seat already booked",
"status": 409,
"detail": "Seat 14 on trip 512 was booked by someone else. Please choose another seat.",
"instance": "/api/v1/bookings",
"tripId": 512,
"seatNumber": 14
}What this code does
- Neha's request reaches the server first and gets 201 with her booking; the server adds the id, fare, status and time.
- Rahul's request for the same seat is valid but clashes with the booking just made: 409 with a problem body. The extension members tell the app which seat to grey out.
Writing them finds gaps the table hides: which fields the client must send, what the server adds, what an error looks like. The bus booking API below has one tricky case: two people try to book seat 14 at the same moment. Only one booking can win, and the other gets a 409 whose problem body says which seat is gone, so the app can ask the user to pick again.
Review checklist for any REST design
Run this checklist on your own design before anyone else sees it. Most mistakes show up in the first three lines.
Example 4 · Checklist run on a train-ticket design
GET /api/v1/Trains list of 13,000 trains, no pagingGET /api/v1/trains/12627/coaches/S4/seats/32POST /api/v1/tickets 200 OK on successPOST /api/v1/tickets 500 "Duplicate entry for key uq_pnr"GET /api/v2/trains/12627 v2 only adds a "pantry" fieldURLs: /trains lowercase; seats nested too deep -> /coaches/{coachId}/seats/32
Codes: POST success -> 201 + Location; duplicate -> 409 problem, no SQL text
Lists: /trains needs page and size with a maximum, plus filters such as ?from=&to=
Errors: one Problem Details shape; constraint names stay in the server log
Version: adding "pantry" is non-breaking -> stays in v1; no v2 neededWhat this code does
- Each line of the checklist found something: style, nesting, status codes, paging, leaking errors and an unneeded version.
- Coach S4 needs a unique coach id to get its own top-level home; then its seats are one level under it.
The checklist:
- URLs: plural nouns, lowercase, hyphens, ids in the path, one level of nesting, verbs only as POST actions.
- Methods and codes: GET 200/404, POST 201 + Location, PUT/PATCH 200, DELETE 204; 400, 409 and 422 used precisely.
- Lists: filters as optional query parameters, an allow-listed sort with a default, page and size with a maximum.
- Errors and versions: one Problem Details shape with every invalid field, no internals;
/api/v1, additions only.
Finish up
Mark complete & continue
Next: Project: design the ShopLite API on paper