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 ContentSome 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, notwt.
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
A new ticket was made: number 38
60 numbers are already out today
The phone number in the request has letters in it
The counter page asks for the queue without signing in
The database is down
A patient cancels number 38, and it’s cancelled
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.
Press Walk through it to follow the work round the chart, one stop at a time.
- A text arrives at the gateway, which POSTs it to Pila with its message id.
- 2xx within 10 seconds? If yes, mark it delivered.
- If no, wait, then try again (a loop back to the POST).
- Pila remembers message ids, so a retried text gets the same number, never a second one.
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
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
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.
- RFC 9110: HTTP Semantics (2022): methods, idempotent methods, and the meaning of each status code
- IEEE Computer Society: SWEBOK Guide, version 4.0 (2024): interface design between software components
- David Parnas, “On the Criteria to Be Used in Decomposing Systems into Modules” (1972): an interface that hides how the work is done