TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to REST API design

๐Ÿฌ REST API design: cheat sheet

๐ŸŽจ The analogyA well-organised supermarket: clear labels on every aisle, the same few actions anywhere, and a plain receipt.
๐ŸŽ“ The regular explanationDesigning HTTP APIs around resources, with nouns in URLs, standard methods used for their real semantics, honest status codes, stateless requests, paged lists, standard error bodies and conditional requests for caching and safe updates.
Resources and URLsStep 5 of 5
๐Ÿฌ SupermarketDairy aisle41key /products/41: 41042key /products/42: 42143key /products/43: 432342Card under spot 42r1key /products/42/reviews/1: r10r2key /products/42/reviews/2: r2123๐Ÿง‘Shoppernouns, not verbs
  1. The dairy aisle has three spots, each with its own number label. The card under spot 42 holds two tasting notes.
  2. The shopper looks down the whole dairy aisle and gets back a list of everything on it.
  3. Knowing the label is enough. The shopper walks straight to spot 42 and finds the milk.
  4. The tasting notes live on the card under spot 42, one step in. That's as deep as it needs to go.
  5. There's no sign that says "get milk". Labels name things, and what you do there is a separate choice.
Methods and their semanticsStep 6 of 6
๐Ÿง‘Shopper๐Ÿ›Ž๏ธOrder desk๐Ÿ›๏ธ Bag 7bread, jam, milk
  1. The shopper hands in a list. The desk packs a new bag, puts it on the order shelf and says which spot, bag 7.
  2. No receipt seemed to arrive, so the shopper hands in the same list again. Now there are two bags. Adding twice adds twice.
  3. The shopper asks to remove the extra bag 8. It's gone, and the receipt says "done, nothing to hand back".
  4. Asking to remove bag 8 again changes nothing. The receipt now says "nothing there", but the bag is still gone, just as wanted.
  5. The shopper replaces bag 7 with a whole new list, bread and jam. Doing it twice still leaves one bag with bread and jam.
  6. The shopper asks to add milk to bag 7. Only that part changes. Asking twice would add milk twice, so it depends on how you ask.
Status codes chosen wellStep 5 of 5
๐Ÿง‘Shopper๐Ÿ›Ž๏ธOrder desk๐Ÿ“ฆ Stock room7 shipped
  1. A clear list goes in. The bag is packed, and the receipt says "created" with the spot where it waits, bag 7.
  2. The next list is a scribble the desk can't read at all. Receipt "can't read your list", with a complaint slip.
  3. This list is neat, but it asks for minus three apples. Receipt "I can read it, but it makes no sense".
  4. The shopper asks for bag 99. The desk checks the stock room, and there's nothing at that spot.
  5. The shopper tries to cancel bag 7, but it already left in the van. Receipt "that clashes with how things are now".
Paging through a listStep 4 of 4
๐Ÿง‘Shopperno repeats๐Ÿ›Ž๏ธDesk 1๐Ÿ›Ž๏ธDesk 2๐Ÿ“‹ Stock listkiwi added๐Ÿง‘โ€๐Ÿ”งStock clerk
  1. The shopper asks desk 1 for fruit, cheapest first, three at a time. Back comes page one and a slip, "carry on after the pears".
  2. Meanwhile a stock clerk adds kiwis, the cheapest fruit yet. They go to the very top of the list, so everything moves down one.
  3. Suppose the shopper now says "skip the first three". The pears have moved down to fourth, so page two starts with pears again.
  4. With the slip it just works, even at desk 2. The slip says where to carry on, so no desk needs to remember the shopper.
ETags and conditional requestsStep 5 of 5
๐Ÿ‘ฉAnahas v4๐Ÿ‘จBenhas v4๐Ÿ›Ž๏ธPrice desk๐Ÿท๏ธ Shelf tag 42v4, 2.10
  1. Ana and Ben both read shelf tag 42. Each notes its sticker number, 3.
  2. Ana asks to change the price, "only if it's still sticker 3". It is, so the price changes and the tag gets sticker 4.
  3. Ben, still thinking it's sticker 3, asks for a different price. It isn't sticker 3 any more, so the desk refuses and changes nothing.
  4. Ben asks "still sticker 3?" No, so he gets the new tag, sees Ana's price and decides again.
  5. Ana asks "still sticker 4?" Yes, so the desk just says "nothing new", with no details to read again.
A REST controller in SpringStep 4 of 4
๐Ÿง‘Shopper๐Ÿง‘โ€๐Ÿ’ผDesk clerk๐Ÿ“Complaints desk๐Ÿ“ฆ Stock room
  1. A good list arrives. The clerk has the stock room pack the bag, then writes "created" and the bag's spot on the receipt.
  2. The shopper asks for bag 99. The stock room has nothing there, and the clerk stops what they're doing.
  3. The clerk passes the problem to the complaints desk, which fills in the usual slip, and the clerk hands it over with the receipt.
  4. A list asks for minus three apples. The clerk spots it before going to the stock room, and the complaints desk writes the slip.

๐Ÿงฉ Key questions

A shopper hands in the same order list twice because no receipt came back. What happens, and what stops it?
Two bags get packed, unless the list carries a ticket number the desk remembers. Looking, replacing and removing are safe to repeat; adding is not, so the ticket number is what makes a second try harmless.
Bag 7 already left in the van, and the shopper asks to cancel it. Which receipt fits?
That clashes with how things are now. The list was clear and the bag exists; the problem is that its situation has moved on.
Ana and Ben both read sticker 3. Ana changes the price first. Ben then asks for a change, only if it's still sticker 3. What happens?
The desk refuses Ben's change, so Ana's price isn't silently undone. The sticker is now 4, so Ben's condition fails. He looks again and decides with Ana's price in front of him.
While a shopper pages through the fruit, a cheaper fruit is added at the top. Which way of asking for the next page still works?
Hand in the slip that says carry on after the pears. Skipping counts places, and the places moved. The slip names the last thing you saw, and any desk can use it.