A REST API is a way for two programs to talk over the web using plain HTTP requests, where each URL names a thing and each request says what to do with it. The core idea is that you treat data as resources, address them with URLs, and act on them with a small set of standard methods. You do not need to memorise everything at once — four ideas carry most of the weight: resources, methods, representations, and statelessness.
Resources Are the Nouns, Methods Are the Verbs
Every REST API is built around resources. A resource is any thing worth naming: a user, an order, a book, a comment. Each resource gets a URL, and the URL should read like a noun, not a verb. /books is a collection of books; /books/42 is one specific book. That plural-then-identifier pattern is the most common convention and it makes an API predictable.
The method is the verb. HTTP gives you a small, fixed set, and REST uses them the same way everywhere:
- GET reads a resource. It should never change anything.
- POST creates a new resource inside a collection.
- PUT replaces a resource entirely with what you send.
- PATCH updates part of a resource.
- DELETE removes a resource.
Keeping verbs out of the URL is what makes an API feel calm to use. If you find yourself writing /getBookById, you have slipped away from REST and into something more like RPC. The URL names the thing; the method says what happens to it.
Representations Carry the Data, Usually as JSON
A representation is the actual bytes sent back when you request a resource. Today that is almost always JSON, though XML and plain text are also valid. The key point is that the resource and its representation are separate: the book exists on the server, and the JSON you receive is one snapshot of it. The same resource could be served as JSON or as HTML depending on what the client asks for.
Representations should be self-describing enough that a client can act on them. A typical response for GET /books/42 looks like this:
{
"id": 42,
"title": "The Quiet Hours",
"author": "A. Writer",
"published": "2021-03-01",
"available": true
}
Notice the field names are plain nouns and the values use standard formats: numbers as numbers, dates as ISO 8601 strings, booleans as true or false. Consistency here matters more than cleverness. A client that has to guess what a field means will break the first time the shape changes.
Statelessness Keeps Each Request Self-Contained
REST is stateless, which means the server does not remember you between requests. Every request must carry everything needed to understand it: the URL, the method, any authentication token, and the body. The server can be restarted, scaled to many machines, or swapped out, and the next request still works because nothing important lived in the server's memory.
This is why tokens and headers matter. If you log in and receive a token, you send that token on every subsequent request. The server checks it, does the work, and forgets you again. Statelessness is what lets an API grow to serve many clients without each one needing a dedicated session on a specific machine.
The trade-off is that each request is a little heavier, because it repeats context. That is usually a good bargain. It also means caching works well: a GET response can be cached by a browser or a proxy because it does not depend on hidden session state.
Status Codes Tell You What Happened
Every response carries a three-digit status code, and the first digit tells you the family. Codes in the 200s mean success. Codes in the 300s mean a redirect or that the resource has moved. Codes in the 400s mean the client did something wrong — a bad URL, missing authentication, or invalid data. Codes in the 500s mean the server failed.
The most useful ones to know by heart are 200 (OK), 201 (Created, usually after a POST), 204 (No Content, often after a DELETE), 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), and 500 (Internal Server Error). Returning the right code is a kindness to whoever writes the client, because it lets them branch on the code instead of parsing error text.
Creating and Reading a Book
Suppose you want to add a book and then read it back. The sequence is short and shows every concept at once.
- Send
POST /bookswith a JSON body containing the title, author, and published date. - The server creates the resource and responds with status 201 and a
Locationheader pointing to/books/43. - Send
GET /books/43with no body. The server responds with 200 and the JSON representation. - Send
PATCH /books/43with{"available": false}to change one field. The server responds with 200 and the updated representation. - Send
DELETE /books/43. The server responds with 204 and the resource is gone; a later GET returns 404.
Each step is self-contained. No step depends on the server remembering the previous one, and each response tells you exactly what state the resource is in.
Where REST Fits and Where It Stretches
REST suits APIs where clients read and write distinct resources at a steady pace: a catalogue, a task list, a set of user profiles. It stretches when you need real-time updates, because REST is request-and-response, not push. For live data, something like WebSockets or server-sent events fits better, and many APIs offer both.
It also stretches when a single screen needs data from many resources at once, because that becomes many round trips. Some APIs offer a way to request related resources together, or a query parameter to expand them, but that is a departure from pure REST rather than a core part of it.
If you are learning, the practical rule is this: name things with nouns, act on them with the standard methods, send JSON, return honest status codes, and keep each request self-contained. Those five habits will carry you through most APIs you meet.
Do I need to use JSON?
No. REST does not require JSON, and older APIs often use XML. JSON is simply the most common representation today because it is compact and easy to parse in most languages.
What is the difference between PUT and PATCH?
PUT replaces the whole resource with what you send, so any field you omit is removed or reset. PATCH changes only the fields you include. Use PATCH when you want to update one field without resending the rest.
Why does a GET sometimes return 404 when the resource exists?
Usually the URL is wrong, the resource was deleted, or the client is not authorised to see it and the server chooses to hide its existence. Check the exact path and whether your token is being sent.