Design · lesson 6 of 10
Ideas in this lesson
AAPI, endpointan agreed way to ask a system for something
BStatus codes2xx worked, 4xx your mistake, 5xx ours
CIdempotenttwice has the same effect as once
DInterfacefrom lesson 4.3

Designing an API

Last updated

The parts of Pila talk to each other, and to the SMS company, through APIs: agreed ways of asking for something and getting an answer. A good API is easy to call correctly and hard to call wrongly; a bad one hides its errors. In this lesson you'll design Pila's three main endpoints, give each the right status codes, and see how the SMS gateway's retries shape the design.

By the end of this lesson you can

  • Explain what an API is, and describe an HTTP endpoint's request and response
  • Choose status codes so callers can tell success from their mistake from the server's
  • Design an endpoint that's safe to call twice

A promise between programs

An API (application programming interface) is an agreed way for one program to ask another for something. Lesson 4.3's interfaces were APIs inside one program: send_text(phone, message). Between programs on a network, APIs are usually made of endpoints: web addresses that accept requests over HTTP and send back responses.

Pila has callers it doesn't control: the SMS company's gateway, the counter page, the waiting-room screen, and perhaps one day a health office's system. Each relies on the API keeping its promise. So design it from the caller's side: what do they need to ask, and what do they need to know when it goes wrong?

Pila's three endpoints

An HTTP request has a method (what to do), a path (to what), and sometimes a body. The response has a status code and usually a body, often in JSON. Pila's three main endpoints:

POST /v1/tickets              take a number
     body:  {"phone": "09171234567", "message_id": "sms-88412"}
     201 Created  {"number": 38, "wait_minutes": 95}

GET  /v1/queue/now            who is being served
     200 OK       {"serving": 14, "next": [15, 16, 17]}

DELETE /v1/tickets/{id}       cancel a number
     204 No Content

Some design choices are visible already:

  • Nouns in the path, verbs in the method. POST /v1/tickets, not /v1/makeNewTicketNow. RFC 9110, which defines HTTP's meaning, says what each method is for.
  • A version in the path. /v1/ lets Pila change the API later as /v2/ while old callers keep working.
  • Names the caller understands. wait_minutes, not wt.

Status codes say what happened

The status code is the first thing a caller reads, and often the only thing a program checks. Its first digit carries the meaning: 2xx it worked, 4xx the request can't be done as sent (retrying the same thing won't help), 5xx the server failed (trying later might).

0 of 7 placed right

  1. A new ticket was made: number 38

  2. 60 numbers are already out today

  3. The phone number in the request has letters in it

  4. The counter page asks for the queue without signing in

  5. The database is down

  6. A patient cancels number 38, and it’s cancelled

  7. A ticket id that never existed is asked for

Errors also need a body a person can act on: {"error": "full", "opens": "07:00"}, not "Error 7". The SMS module turns that into the text Lola Remy reads: "Sorry, today's numbers are all taken. Try again tomorrow from 7 a.m."

When the gateway tries again

Networks fail. The SMS company's gateway knows this, so if Pila doesn't answer with a 2xx in time, it sends the same text again. Walk through what that means for Pila's design.

[yes][no]try againReceive a textPOST it to Pila,with its message id2xx within10 seconds?WaitMark itdelivered

Press Walk through it to follow the work round the chart, one stop at a time.

  1. A text arrives at the gateway, which POSTs it to Pila with its message id.
  2. 2xx within 10 seconds? If yes, mark it delivered.
  3. If no, wait, then try again (a loop back to the POST).
  4. Pila remembers message ids, so a retried text gets the same number, never a second one.
The SMS gateway delivering a text to Pila, with retries, as a UML activity diagram. In UML’s notation: the filled dot is where it starts and the ringed dot where it ends; rounded boxes are actions; a diamond with a question is a decision, with the condition for each way out in [brackets], and an empty diamond is a merge, where a way back joins the flow; Pink arrows go back to an earlier action.

An operation is idempotent if doing it twice has the same effect as doing it once. RFC 9110 defines GET and DELETE as idempotent; POST isn't, unless the design makes it so. Pila does, by remembering each message id. Without that, every slow morning would give some patients two numbers, and push everyone else back.

Spot the trap: 200 OK, with an error inside

Spot the trapMarco

To keep things simple, Marco makes every Pila endpoint reply 200 OK, and puts any error in the body, so a failure comes back as 200 OK with “status: error, reason: database down” inside.

What happens if…

Check what you learned

The queue is full. Which response is best?
Why does Pila remember each text’s message id?
What does putting /v1/ in Pila’s paths allow?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development