Chapter 77: APIs
An API (application programming interface) is a defined way for one piece of software to ask another for data or actions. The program that asks is the client, such as a phone app or a web page. The program that answers is the server. Most screens a designer draws are filled by API calls, so the API decides what a screen can show, how fast and how it fails.
Endpoints
An endpoint is a URL that an API exposes for one resource or action. A resource is a thing the API lets clients read or change, named as a noun: /restaurants is the list of restaurants, and /orders/123 is one order. Query parameters follow a ? and narrow the request, so /restaurants?cuisine=thai&page=2 asks for the second page of Thai restaurants. That last part is pagination: long lists arrive a page at a time, and the design must say what happens at the bottom of each. Before drawing a filter, ask whether the endpoint accepts it as a parameter.
Requests and responses
A request is the message the client sends, and a response is the one the server sends back. Every API call follows the same cycle:1
- The client sends a request: a method (GET, POST), a URL, headers such as the user’s token, and sometimes a body.
- The server checks the request, does the work (→ 78 Data and databases) and builds a response.
- The response comes back with a status code, headers and usually a body.
- The client reads the status code first, then the body, and updates the screen.
Latency is the time between sending the request and getting the response. It depends on the network and on how much work the server does, and it’s why loading states exist (→ 46 Affordances, signifiers, feedback and constraints). A timeout is the point at which the client gives up and treats the request as failed. Developers set it; ask what it is, because the screen after a timeout needs a message and a retry (→ 61 Interaction patterns, forms and states).2
HTTP methods and status codes
An HTTP method is the verb in a request. Most APIs use five:34
- GET reads a resource and changes nothing.
- POST asks the server to process data, usually to create something, such as placing an order.
- PUT replaces a resource with the version sent.
- PATCH changes part of a resource.
- DELETE removes a resource.
GET, PUT and DELETE are idempotent: sending the same request twice has the same intended effect as sending it once. POST is not, so a double-tapped “Place order” button can create two orders unless the design disables it after one tap.3
A status code is the three-digit number at the start of every response. The first digit gives the class: 2xx succeeded, 3xx means the client must go elsewhere, 4xx means the request was wrong, 5xx means the server failed.3 Map each code a screen can receive to a designed state. For the restaurant list:
| Code | Name | What happened | UI state |
|---|---|---|---|
| 200 | OK | Request succeeded | Show the list. If the list is empty, show the empty state |
| 201 | Created | A new resource exists | Confirm, such as “Order placed” |
| 400 | Bad Request | The server rejected the input | Inline error on the field at fault |
| 401 | Unauthorized | No valid sign-in | Send to sign-in, then return here |
| 403 | Forbidden | Signed in, not allowed | Explain the restriction; don’t offer sign-in |
| 404 | Not Found | Resource doesn’t exist | “This restaurant is no longer listed” |
| 429 | Too Many Requests | Hit the rate limit | Ask the user to wait; retry later |
| 500 | Internal Server Error | The server failed | Apologize, keep the user’s input, offer Retry |
An empty list is a success, 200 with zero items, and only the design can say what that means. A 429 comes from a separate standard, RFC 6585, and may carry a Retry-After header saying how long to wait.5
Payloads and JSON
A payload is the data carried in a request or response body. Most web APIs send it as JSON (JavaScript Object Notation), a text format built from objects, arrays, strings, numbers, true, false and null.6 One restaurant might arrive as:
{ "id": 481, "name": "Baan Thai", "rating": null,
"deliveryMinutes": 35 }
Here rating is null: the field exists but holds nothing, perhaps because the restaurant is new. The photoUrl the mockup shows is missing altogether. Each needs a layout decision: hide the stars or show “New”, use a placeholder or close the gap. A schema describes which fields exist, their types and which are required; ask for it before designing.
REST and GraphQL
REST (Representational State Transfer) is an architectural style, not a protocol. Roy Fielding described it in his 2000 doctoral dissertation as a set of constraints, among them client-server separation, statelessness and a uniform interface.7 In a REST API each resource has its own endpoint, and the server fixes each response’s shape.
GraphQL is a query language for APIs, open-sourced in 2015.8 A GraphQL server usually has one endpoint, often /graphql, and the client lists the exact fields it wants.9
For designers the difference shows up as two problems. Over-fetching is receiving more than the screen needs, such as full menus when the list shows only names. Under-fetching is the opposite: one call doesn’t return enough, so a screen makes several requests, each of which can load or fail on its own. With REST, ask how many calls a screen makes and whether parts of it can appear before the rest. With GraphQL, a screen’s data can usually come from one query, though one slow field can delay all of it.
Authentication and authorization
Authentication is checking who is making a request; authorization is checking what that user may do.10 A user signs in once, and the app proves it on every request with a token (a string sent in a header) or a session (a record on the server, tied to the browser by a cookie). OAuth is the standard behind “Sign in with…” buttons: it lets one app get limited access to a user’s account on another service without seeing their password.11
Authorization is usually organized by role: viewer, editor, admin. Design each role’s version of a screen, and decide whether forbidden actions are hidden or disabled with a reason. Hiding a button isn’t security: OWASP says access must be checked on the server, on every request, and denied by default, so the design still needs a 403 state.10
See also
73 How the internet and the web work · 76 Frontend, backend and architecture · 78 Data and databases · 61 Interaction patterns, forms and states · 81 Handoff and accessibility in code · 95 Privacy and data ethics
Sources
- Dusseault, L. and Snell, J. (2010). RFC 5789, PATCH Method for HTTP. IETF.
- Ecma International (2017). ECMA-404, The JSON Data Interchange Syntax, 2nd edition.
- Fielding, R. T. (2000). Architectural Styles and the Design of Network-based Software Architectures. Doctoral dissertation, University of California, Irvine.
- Fielding, R., Nottingham, M. and Reschke, J., eds. (2022). RFC 9110, HTTP Semantics. IETF.
- GraphQL Foundation (n.d.). “Introduction to GraphQL” and “Serving over HTTP.” graphql.org.
- Hardt, D., ed. (2012). RFC 6749, The OAuth 2.0 Authorization Framework. IETF.
- MDN Web Docs (n.d.). “An overview of HTTP”; “PATCH”; “401 Unauthorized”; “429 Too Many Requests”; “AbortSignal: timeout()”. Mozilla.
- Nottingham, M. and Fielding, R. (2012). RFC 6585, Additional HTTP Status Codes. IETF.
- OWASP (n.d.). “Authorization Cheat Sheet.” OWASP Cheat Sheet Series.
Notes
- MDN Web Docs (n.d.). “An overview of HTTP.” https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Overview ↩
- MDN Web Docs (n.d.). “AbortSignal: timeout() static method.” https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static ↩
- Fielding, R., Nottingham, M. and Reschke, J., eds. (2022). RFC 9110, HTTP Semantics, sections 9.2 and 15. IETF. https://www.rfc-editor.org/rfc/rfc9110.html ↩ ↩ ↩ ↩
- Dusseault, L. and Snell, J. (2010). RFC 5789, PATCH Method for HTTP. IETF. Summarized at https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/PATCH ↩
- Nottingham, M. and Fielding, R. (2012). RFC 6585, Additional HTTP Status Codes, section 4. IETF. Summarized at https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/429 ↩
- JSON.org (n.d.). “Introducing JSON.” Ecma International, ECMA-404. https://www.json.org/json-en.html ↩
- Fielding, R. T. (2000). Architectural Styles and the Design of Network-based Software Architectures, chapter 5. Doctoral dissertation, University of California, Irvine. https://ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm ↩
- GraphQL Foundation (n.d.). “Introduction to GraphQL.” https://graphql.org/learn/introduction/ ↩
- GraphQL Foundation (n.d.). “Serving over HTTP.” https://graphql.org/learn/serving-over-http/ ↩
- OWASP (n.d.). “Authorization Cheat Sheet.” https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html ↩ ↩
- Hardt, D., ed. (2012). RFC 6749, The OAuth 2.0 Authorization Framework. IETF. https://www.rfc-editor.org/rfc/rfc6749.html ↩
- MDN Web Docs (n.d.). “401 Unauthorized.” https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/401 ↩