A REST API is a way for two programs to exchange data over HTTP using plain, predictable web addresses. Each address points at a resource, such as a user, an order or a list of articles, and you read or change it with a standard HTTP method like GET, POST, PUT or DELETE. Think of it as a menu of URLs: the kitchen does the work, and you order by name.
Here is a concrete example. Suppose a small shop keeps its products online. A request to GET /products/42 returns the product with the ID 42 as JSON, something like {"id": 42, "name": "Mug", "price": 9}. To add a product, you send POST /products with the product data in the request body. To rename it, you send PUT /products/42 with the whole updated record. To remove it, you send DELETE /products/42. Four addresses, four actions, no hidden back doors.
What Makes an API RESTful?
REST stands for representational state transfer. In practice it means an API follows a few habits that keep clients and servers independent. The defining features are:
- Resources with nouns. URLs name things, not actions. Use
/orders/17, not/getOrder?id=17. - HTTP methods carry the verb. GET reads, POST creates, PUT replaces, PATCH partly updates, DELETE removes.
- Stateless requests. Each request contains everything the server needs, usually an authentication token. The server does not rely on remembering earlier requests.
- Representations. The same resource can come back as JSON, HTML or XML, chosen by the
Acceptheader. JSON is the common default. - Layered caching. Responses can be marked cacheable so a browser or proxy can reuse them.
An API that uses HTTP but hides state in a session, or puts verbs in URLs, still works. It is just harder to learn, cache and reuse.
How a REST Request Works, Step by Step
Every call follows the same short journey. Understanding each stop makes errors easier to read.
- Build the URL. Combine the base address, the resource path and any query filters, for example
/products?category=mugs&limit=20. - Choose a method. Pick GET to read, POST to create, PUT or PATCH to update, DELETE to remove. Choosing the wrong one confuses every future reader.
- Add headers. Set
Content-Type: application/jsonwhen sending a body, andAuthorization: Bearer <token>when the API needs credentials. - Send the body. GET and DELETE usually carry none. POST and PUT send JSON with the fields to store.
- Read the status code. A code in the 200s means success, 400s mean the request was refused, 500s mean the server failed.
- Parse the response. The body usually holds JSON, plus headers that tell you the content type and caching rules.
The status codes worth memorising
You do not need all of them. These cover most situations:
- 200 OK and 201 Created: the request worked. 201 usually includes a
Locationheader pointing at the new resource. - 204 No Content: success with nothing to return, common after DELETE.
- 400 Bad Request: the body was malformed or a field failed validation.
- 401 Unauthorized: no valid credentials. 403 Forbidden: credentials are valid but lack permission.
- 404 Not Found: no resource at that address. 429 Too Many Requests: you hit a rate limit.
- 500 Internal Server Error: something broke on the server side, not in your request.
The status code is the first thing to check when a call misbehaves. A 401 and a 404 need very different fixes.
Which HTTP Method Should You Use?
The rule of thumb is that methods fall into two groups. Safe methods, GET and HEAD, never change data. Idempotent methods, GET, PUT, DELETE and HEAD, give the same result if you repeat them. POST is neither: sending it twice can create two records.
That distinction matters for retries. If a network timeout hits a GET or DELETE, sending it again is safe. If it hits a POST, you may have created a duplicate, so check before retrying. Many APIs accept an idempotency key header exactly for this reason.
PUT versus PATCH
PUT replaces the whole resource: fields you leave out are cleared or reset to defaults. PATCH sends only the fields that change. If a product has a name, price and stock level, a PATCH with only {"price": 12} changes the price and leaves the rest alone. A PUT with the same body may wipe the name and stock. Use PATCH for small edits and PUT when you genuinely hold the full record.
Where REST APIs Go Wrong
Most frustration comes from a short list of habits. Each has a fix.
- Verbs in URLs.
/createOrderand/deleteOrdermultiply endlessly. Model the noun/ordersand let methods do the work. - Deep nesting.
/users/5/orders/9/items/3/reviewsis brittle. Stop nesting after one level and link the rest with query parameters. - Ignoring pagination. A list endpoint that returns everything will eventually be slow and huge. Offer
limitandoffsetor cursor parameters from the start. - Leaking errors as 200s. Returning a success code with
{"error": "not found"}in the body breaks every client that checks status codes. - Sending secrets in the URL. Query strings end up in logs and browser history. Put tokens in the
Authorizationheader. - Forgetting CORS. A browser client on another domain is blocked unless the server sends the right access-control headers.
- No versioning plan. Adding a breaking field later affects every client. Prefix the path, as in
/v1/products, so old and new callers can coexist.
What Should a Good Response Look Like?
A response should be predictable. Return JSON with consistent field names, keep dates in one format such as ISO 8601, and wrap lists in an object like so you can add totals or links without breaking clients. Use plural nouns for collections ( /products ) and singular paths for single items ( /products/42 ).
Errors deserve the same care. A useful error body names the problem and the field at fault, for example {"error": "validation", "field": "price", "message": "must be a number"}. That saves the caller from guessing.
Finally, test with a simple tool before writing code. Send a GET by hand, inspect the status code and headers, then confirm the JSON shape. If the raw response is clear, the code that consumes it will be clearer too.
REST or GraphQL or gRPC?
REST suits most everyday needs: it is simple, cacheable and easy to debug with a browser or a command-line tool. GraphQL lets a client ask for exactly the fields it wants, which helps when screens need many related records at once. gRPC is compact and fast for service-to-service calls. None of them is wrong; choose REST when predictable addresses and standard HTTP behaviour are what you need, and reach for the others when a specific problem demands it.