/todos/1milk/todos/2eggs/todos/3/todos/4/todos/5GET /todos HTTP/1.1Host: api.example.comAccept: application/jsonPOST /todos HTTP/1.1{"title": "jam"}PUT /todos/3 HTTP/1.1{"title": "tea", "done": false}PATCH /todos/3 HTTP/1.1{"done": true}DELETE /todos/2 HTTP/1.1GET /todos/2 HTTP/1.1GET /todos/1 HTTP/1.1Authorization: Bearer abc123(sent twice, same token)PUT /todos/3 HTTP/1.1 x2{"title": "chai", "done": true}POST /todos HTTP/1.1 x2{"title": "rice"}GET /v1/todos?page=2 HTTP/1.1HTTP/1.1 200 OK[{"id": 1, "title": "milk", "done": false}, {"id": 2, "title": "eggs", "done": false}]HTTP/1.1 201 CreatedLocation: /todos/3{"id": 3, "title": "jam", "done": false}HTTP/1.1 200 OK{"id": 3, "title": "tea", "done": false}HTTP/1.1 200 OK{"id": 3, "title": "tea", "done": true}HTTP/1.1 204 No ContentHTTP/1.1 404 Not Found{"error": "no todo 2"}HTTP/1.1 200 OK{"id": 1, "title": "milk", "done": false}HTTP/1.1 200 OK x2(row 3 changed once)HTTP/1.1 201 Created x2Location: /todos/4Location: /todos/5HTTP/1.1 200 OK[{"id": 4, "title": "rice", "done": false}, {"id": 5, "title": "rice", "done": false}]requestresponseGET /todos200 OKPOST /todos201 CreatedPUT /todos/3200 OKPATCH /todos/3200 OKDELETE /todos/2204 No ContentGET /todos/2404 Not FoundGET /todos/1200 OKGET /todos/1200 OKPUT /todos/3200 OKPUT /todos/3200 OKPOST /todos201 CreatedPOST /todos201 CreatedGET /v1/todos?page=2200 OK
Back row: the client, the API server and its todos table (one row per to-do, each with its URL)Front: the request card and the response cardEnvelopes: requests (method + path) and responses (status code); tokens: data read from or written to a rowAt the back: the to-do app (the client), the API server and its todos table, one cell per to-do, each labelled with its URL. In front: the request the client wrote and the response the server wrote. Each call is an envelope labelled with its method and path. The server reads or changes a row, then an envelope labelled with the status code hops back. The cards leave out headers real messages also carry, such as Host (shown once), Content-Type: application/json and Content-Length. Each JSON body is exactly what Python's json.dumps writes, split across lines.

APIs and resources

  1. An API (application programming interface) is a set of rules one program uses to ask another for things. Here a to-do app, the client, asks an API server. A REST API sends those requests over HTTP, the web's protocol.

Reading and writing

Rules of the road

What happens

;; step 1/12 · api
API     application programming interface:
        the requests a program accepts, and its answers
client  the program that asks   (our to-do app)
server  the program that answers (the API server)
REST    a style for web APIs, built on HTTP

The cards leave out headers real messages also carry, such as Host (shown once), Content-Type: application/json and Content-Length. Each JSON body is exactly what Python's json.dumps writes, split across lines. REST is an architectural style described by Roy Fielding in 2000. Many APIs called REST follow only part of it. Ids, the page size and the error bodies here are this example API's own choices: there is no single standard for pagination.