API Design Introduction 00:00:01 Search the catalogue A shopper types headphones and taps Search. Later the same shopper taps Buy. The two actions may travel over the same connection, yet one can safely be repeated far more casually than the other. The shopper types headphones and taps search. The gateway validates the token and the rate limit. The request goes to the server that owns products. The database returns the matching rows. Rows become JSON with paging information. The app draws product cards with pictures and prices. A search is a read. Define how filters, sorting and pagination affect the returned set so callers can interpret an empty result without treating it as an outage. 00:00:41 Commit one order A write that involves stock, money and a new record. The app asks for an order and sends a unique key. Every item in the cart must be available. The payment company charges the card. Another company does this, outside our system. The order is inserted. The confirmation goes back with the address of the new order. Give a successful creation a durable identity and a way to recover the original result after a lost reply. A button being pressed once does not imply one network attempt. 00:01:12 Identify the caller The app calls GET /orders and forgets the header. Authorization is missing. The answer says that authentication is required. The server and database saw nothing. It branches on the status code, not on the text. After logging in, the app repeats the request with a token. A missing or invalid credential and an authenticated caller lacking permission are different failures. Choose a response the caller can act on while avoiding unnecessary disclosure of private resources. 00:01:42 Name the resources people own Turn a requirement into five lines of API. Shoppers look for products, fill a cart and order. Looking is reading, so GET on the collection. The same noun with an id. One cart per user. Part of it changes, so PATCH. A new thing is created, so POST to the collection. The shopper wants to check it later. A clear resource boundary keeps an implementation detail from becoming part of every client. Describe the order and its state instead of promising which internal table or queue will hold it. 00:02:14 The second request lands elsewhere Where the cart lives decides whether any server can answer. The first request reaches server A, which keeps the cart in its own memory. The next request reaches server B. It has never heard of the cart. The request carries the token, the cart lives in the database. A can answer. B can answer too. Adding or losing a server changes nothing for the shopper. If the next server can interpret the request and reach the authoritative state, the caller need not stay attached to the first worker. This does not make the persistent order itself stateless. Design the commitment the shopper can rely on, then the request and response that express it. Changing the database should not require teaching an existing client a different meaning for a confirmed order.