REST APIs

Resources and verbs · Part of Architecture

Key idea

Practice

7 questions · about 4 min

Explained step by step

API

API stands for Application Programming Interface. It's a set of definitions and protocols for building and integrating programs. It's often seen as a contract between whoever provides information and whoever uses it: what must be asked for and what will be received. It's like a restaurant menu: you don't see the kitchen, but you know that if you order dish 4, you get a schnitzel with fries.

REST

A REST (or RESTful) API follows the rules of the REST architectural style, which stands for Representational State Transfer and was introduced by Roy Fielding in 2000. In REST, the basic unit is the resource: anything with a name, like a user or an order, with its own address, like /users/42. It's like a library: each book has its fixed location and for any book you do the same 4 things: borrow, return, renew or report it.

To be RESTful, an API must meet constraints: uniform interface (every resource is used the same way), client-server over HTTP, stateless (the server keeps nothing about the client between requests), cacheable (each response says whether it can be cached and for how long), layered system and, optionally, code on demand. It's like an ATM: each operation carries your card and PIN, and the ATM doesn't remember you from one time to the next.

REST is the default standard for designing APIs and fits almost every case. An example with the users resource: GET /users gets all, GET /users/{id} gets 1, POST /users adds one, PATCH /users/{id} updates it and DELETE /users/{id} removes it. With 1 resource and 5 verb-address combinations, everything is covered. It's like a card file: always the same drawer, and the action changes with what you do to the card.

HTTP verb

HTTP defines methods to say which action you want on a resource; they're called HTTP verbs. GET asks for a resource. HEAD is like GET but without the response body. POST sends something new and usually changes state. PUT replaces the whole resource. PATCH changes only part of it. DELETE removes it. It's like a form with checkboxes: «look up», «create», «replace», «correct» or «cancel».

Status code

HTTP status codes say whether a request worked. There are 5 classes: 1xx informational, 2xx success, 3xx redirection, 4xx client error and 5xx server error. For example, 200 is «ok», 404 is «not found» and 503 is «server busy». It's like a traffic light for a request: the first digit already tells you if it's green, yellow or red, and whose fault it is.

Over-fetching

REST is simple and easy to understand, flexible and portable, works very well with caching and decouples client from server. But it has 2 problems. Over-fetching: getting more data than you need, because /users/42 returns all 30 fields even if you want 2. And sometimes several round trips to the server are needed to build 1 screen. It's like ordering a whole pizza when you wanted 1 slice, and then having to order the drink at another shop.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Resources and verbs