What is a REST API? REST API Explained

Blog / What is a REST API? REST API Explained
What is a REST API? And why is it the way almost every web and mobile app talks to its backend?

The Problem: Every API Invents Its Own Shape

Without a shared style, every API invents its own shape. Developers name operations their own way, usually as verbs in the URL, like POST /createUser, GET /getUser?id=42 or GET /deleteOrder.Every new API has to be learned from scratch. Using a GET request to delete something is also surprising and unsafe.This is where REST comes in. It provides one uniform way to design an API on top of HTTP.

Resources and URIs

In REST, everything is a resource, like a user or an order. Every resource is identified by a URI.You should use plural nouns and never verbs, for example /users or /users/42. You can show relationships by nesting them, like /users/42/orders.The URI names the thing, and the HTTP method says what to do with it.

HTTP Methods and CRUD

Standard HTTP methods map cleanly to create, read, update and delete actions on the same /users URI.
  • GET reads data.
  • POST creates new data.
  • PUT replaces the whole resource entirely.
  • PATCH updates only part of the resource.
  • DELETE removes it.

A Request End to End

Let's walk through a client creating an order. The client sends a request with the POST method and the /orders URI.The request includes headers for Content-Type: application/json and an Authorization token, along with a small JSON body.The server responds with 201 Created, a Location header pointing at the new order, and the order itself as JSON.
A POST /orders request with its headers and JSON body, and the 201 Created response with a Location header pointing at /orders/42

Status Codes

The status code tells the client what happened before it reads the body.
  • Codes starting with 2 indicate success, like 200 OK, 201 Created and 204 No Content.
  • Codes starting with 4 mean the client made a mistake, like 400 Bad Request, 401 Unauthorized, 403 Forbidden and 404 Not Found.
  • Codes starting with 5 mean the server failed, like 500 Internal Server Error.
Watch out for these two interview traps.401 vs 403401 means the server does not know who you are. 403 means it knows who you are, but you are not allowed to do the action.
401 Unauthorized: the server doesn't know who you are. 403 Forbidden: the server knows you, but you are not allowed
200 OK with an errorReturning 200 OK with an error message in the body is a mistake. Clients, caches and monitoring systems read the status code to know what happened.

Stateless

The server keeps no session for the client between requests. Every request carries everything needed to handle it, including the authentication token.Any server behind a load balancer can handle any request, which makes REST APIs very easy to scale horizontally.Tokens like JWTs are a topic of their own, and we have another video explaining them.

Safe vs Idempotent Methods

Safe means the request does not change anything on the server. GET and HEAD are safe methods.Idempotent means sending the request once or many times leaves the server in the exact same state. GET, PUT and DELETE are idempotent.POST is not idempotent. If a network timeout makes the client retry a payment, the user can be charged twice.
A POST /payments request times out, the client retries, and the server records two $50 charges
The fix is an Idempotency-Key header, so the server can spot the retry.You might face an interview trap about deletions. A second DELETE request returns a 404 Not Found, but DELETE is still idempotent because the server's state is exactly the same.

What Makes an API RESTful

REST is an architectural style from Roy Fielding's 2000 PhD dissertation, not a protocol or a standard. To be truly RESTful, an architecture must follow six constraints.
  • Client-server separates the user interface from data storage.
  • Stateless means no client context is stored on the server.
  • Cacheable requires responses to declare if they can be reused.
  • Layered system means the client does not know if it is connected to the end server or an intermediary.
  • Uniform interface provides a standard way to communicate.
  • Code on demand is optional and allows sending executable scripts to the client.
Sending JSON over HTTP does not make an API RESTful by itself. In practice, most so-called REST APIs do not follow every constraint.A common omission is HATEOAS, a constraint where responses must include links to the next available actions.

Recap

IdeaWhat to remember
ResourcesResources are named by URIs
HTTP methodsMethods define the actions
Status codesStatus codes communicate the outcome
StatelessStatelessness allows horizontal scaling
Safe and idempotentMethods are categorized as safe and idempotent
RESTAn architectural style rather than a strict protocol

About TechPrep

Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.