REST APIs
A common, predictable way to design APIs so any app can talk to a server the same way.
What is it?
An API (Application Programming Interface) is how one piece of software lets another ask it to do things. APIs can be organized in endless inconsistent ways, which makes them hard to learn. REST (Representational State Transfer) is a widely used style that keeps APIs consistent and predictable.
The core idea: everything is a "resource" (a user, a post, a product) with its own URL. You act on it with standard HTTP methods: GET to read, POST to create, PUT/PATCH to update, DELETE to remove.
Explain like I'm 10
REST is like a filing cabinet where every drawer has the same layout and supports the same actions: look inside, add a paper, replace the contents, or remove it. Learn one drawer and you know how to use any drawer in any cabinet built the same way.
Examples
REST-style endpoints for a 'posts' resource
GET /posts // get all posts
GET /posts/42 // get one specific post
POST /posts // create a new post
PUT /posts/42 // replace post 42 entirely
PATCH /posts/42 // partially update post 42
DELETE /posts/42 // delete post 42The URL says what you're acting on (posts, or one post), and the HTTP method says what action you're taking.
How it works
A REST API exposes resources as URLs and uses standard HTTP methods and status codes instead of custom verbs for every action. If you've used one REST API, you can usually guess how another works without much reading.
Why does it exist?
Before conventions like REST, every API had its own rules, so integrating was slow and error-prone. REST gives everyone shared conventions, which speeds up building and using APIs across companies and codebases.
When to use it
Use REST for a general-purpose API around resources (users, posts, products) that other developers, possibly outside your team, need to learn and use.
When not to use it
For action-style operations that don't map to a resource (like "run this report" or "send this batch job"), a REST shape can feel forced. An RPC-style or GraphQL API sometimes fits better.
Common mistakes
Using verbs in URLs (
/getUser) instead of nouns with proper HTTP methods (GET /user).Returning 200 OK for every response, even errors, instead of proper status codes.
Designing endpoints that don't map to a resource, making the API inconsistent and confusing.
Practice exercises
- Easy:
Design REST-style endpoints for a 'comments' resource: list, get one, create, update, delete.
- Medium:
Explain why
POST /users/delete/42is not a RESTful way to delete a user, and rewrite it properly. - Hard:
Design a small REST API (endpoints + methods + expected status codes) for a simple to-do list app.
Interview questions
What does REST stand for, and what's its core idea?
Representational State Transfer. Everything is a resource with a URL, and you act on it with standard HTTP methods.
What makes an API 'RESTful'?
Resource-based URLs, standard HTTP methods for actions, standard status codes for outcomes, and no state kept between requests.
What's the difference between PUT and PATCH?
PUT usually replaces the whole resource; PATCH changes only the fields you send.
What's the difference between a resource and an endpoint in a REST API?
A resource is the thing being represented (e.g. 'a user'). An endpoint is the URL clients use to reach it (e.g. /users/42). One resource can have more than one endpoint, such as a nested and a top-level route.
Why should REST URLs use nouns for resources rather than verbs for actions?
The HTTP method already states the action (GET, POST, PUT, DELETE). A verb in the URL (like /deleteUser) duplicates or conflicts with it, and makes the API inconsistent since everyone phrases actions differently.
What status code should a successful POST that creates a new resource return, and what else is commonly included?
201 Created, usually with a Location header giving the new resource's URL so the client can find it later.
What status code fits a successful DELETE that returns no response body?
204 No Content. It confirms success and signals there's no body to read.
Why is statelessness considered a defining constraint of REST rather than just a nice-to-have?
Each request must carry everything needed to process it, with no server-side session state between requests. That lets any server instance handle any request, which is key to horizontal scaling and simple load balancing.
What's the difference between a REST-style API and an RPC-style API?
REST models everything as resources acted on with a small set of standard HTTP methods. RPC exposes named actions directly (e.g. POST /createUser, POST /banUser), which is more flexible for arbitrary operations but loses REST's uniform structure.
What's the difference between REST and GraphQL, and when might GraphQL be a better fit?
REST has fixed endpoints that each return a set data shape. GraphQL has one endpoint where the client asks for exactly the fields it needs. GraphQL fits better when clients have very different, changing data needs and over- or under-fetching becomes a real problem.
What does HATEOAS mean, and why do most real-world 'REST' APIs skip it?
Hypermedia As The Engine Of Application State: responses include links to the next available actions or resources, so clients don't hardcode URLs. Most APIs skip it because it adds real complexity for a benefit (loosely coupled clients) few consumers need.
Name a couple of common approaches to versioning a REST API.
In the URL path (e.g. /v1/users) or in a request header (e.g. Accept: application/vnd.myapi.v1+json). URL versioning is simpler and more visible; header versioning keeps URLs stable but is harder to discover.
When is nesting resources in a URL (e.g. /users/1/posts) useful, and when does it become an anti-pattern?
It's useful when a resource truly belongs to its parent (posts that only make sense under a user). It gets awkward when nesting goes several levels deep, or the same resource must be reached both on its own and through a parent, causing duplicate or inconsistent routes.
How should a REST API represent pagination for a large collection?
With query parameters (like ?page=2&limit=20 or cursor-based ?after=<id>), plus metadata in the response (or headers/links) giving the total or how to get the next page. Don't return the whole collection at once.
What's the difference between filtering, sorting, and pagination as REST query parameters?
Filtering picks which items are returned (e.g. ?status=active). Sorting sets their order (e.g. ?sort=-createdAt). Pagination sets how many and which slice come back. They're often combined but independent.
Why should a REST API generally avoid exposing raw internal database ids or implementation details in its responses?
It ties clients to storage choices that should be free to change (like the database or id scheme), and can leak information (sequential ids reveal record counts) clients shouldn't depend on.
Is a REST API required to return JSON? What does the word "representational" in REST actually refer to?
No. REST doesn't require a format; JSON is just the most common today. "Representational" means clients work with a representation of a resource's state (JSON, XML, or anything else), not the resource itself.
How should a REST API communicate a validation error to the client?
With a 4xx status (usually 400 Bad Request or 422 Unprocessable Entity) and a body saying which fields failed and why, so the client can show something useful instead of a generic failure.
What's an idempotency key, and why might a REST API need one for a POST request?
A unique value the client sends with a request. If the request is retried (e.g. after a timeout, when the client can't tell whether the first try worked), the server spots the duplicate and doesn't do the action twice. It's common for payments, where POST not being idempotent is dangerous.
Why is caching often easier to reason about in REST than in RPC-based or GraphQL APIs?
REST uses GET on stable resource URLs, which fits standard HTTP caching by URL. RPC and GraphQL usually send varied requests through one endpoint (often via POST), which browsers, proxies, and CDNs don't cache the same way without custom logic.
Why does it help for a REST API's error responses to follow one consistent structure across all endpoints?
A predictable error shape (e.g. always a code, message, and optional field details) lets clients handle errors in one place instead of per endpoint, and makes docs and debugging more consistent.
Why is over-fetching or under-fetching a common criticism of REST APIs, and how does it come about?
REST endpoints return a fixed data shape. A client needing two fields still gets the whole resource (over-fetching), and a client needing several resources often makes several requests (under-fetching). Both waste bandwidth or round trips compared to asking for exactly what's needed.
What does it mean for a REST API to be "resource-oriented," and how does that shape its URL design?
The API is built around nouns from the domain (users, orders, posts), not actions. URLs follow /resource and /resource/:id, acted on with standard HTTP methods, instead of a growing list of custom action endpoints.
How would you design an endpoint to update just one field of a resource, and which HTTP method fits best?
PATCH /resource/:id with a body holding only the fields to change (e.g. { status: "archived" }). PATCH is meant for partial updates; PUT implies replacing the whole resource.
Why is it considered bad practice for an API to always return 200 OK, even for errors?
Every client must parse the body just to learn whether the request worked, which defeats status codes. It also breaks tools (HTTP caches, monitoring, generic error handling) that rely on the status code matching the real outcome.