Skills / Back-end development and APIs / Designing a REST API
Designing a REST API
A good API is predictable: once a developer understands one part, they can guess the rest. REST is the most common style for web APIs, and it is built around resources: the things your system deals with, such as users, orders and products.
Use nouns for paths, and methods for actions. Avoid paths like /getOrders or /deleteOrder?id=7. Instead:
GET /orderslists ordersPOST /orderscreates an orderGET /orders/7reads order 7PATCH /orders/7changes part of order 7DELETE /orders/7removes it
Nest only for clear ownership: GET /orders/7/items lists the items of order 7.
Return honest status codes. Send 201 with the new resource after a create, 400 with a clear message when the input is wrong, 404 when something does not exist, and 401 or 403 for sign-in and permission problems. Never send 200 with an error hidden inside the body.
Make errors useful. A response like {"error": "quantity must be at least 1"} saves the next developer an hour.
Paginate lists. Never return a million rows at once: GET /orders?page=2&per_page=50.
Version from the start, for example /v1/orders, so you can improve the API later without breaking the apps that already use it.
Write the API down before you build it: a short list of paths, methods, inputs and responses. It is much cheaper to fix a design on paper than in code that people already depend on.
The tutor gives hints, not finished answers. Log in to use it.