Developer & Network Tools

REST APIs Explained

See how REST APIs use resources, HTTP methods, status codes and links, with worked requests and a checklist for good design.

Key takeaways

  • REST organizes an API around resources such as users or orders, each identified by a URL.
  • HTTP methods supply the verbs, and status codes report the outcome, so you rarely need custom conventions for either.
  • Each request carries everything the server needs to handle it, which makes servers easier to scale.
  • Most APIs labeled REST follow only part of the original style, so read the documentation instead of assuming.
  • Consistent naming, clear errors, pagination and versioning separate a pleasant API from a frustrating one.

A REST API is a web API that treats everything it manages as a resource with its own address, and lets you read or change those resources with ordinary HTTP methods. To list orders you send a GET to the orders address; to add one you send a POST there. Responses usually arrive as JSON.

REST stands for representational state transfer. Roy Fielding described it in his doctoral dissertation in 2000 as an architectural style, meaning a set of constraints on how a system should be arranged. It isn't a protocol or a file format, which is why two APIs that both call themselves REST can look quite different.

Resources and their addresses

A resource is any thing the API lets you refer to: a customer, an invoice, a photo, or a collection of them. Each gets a URL. By common convention, collections use plural nouns and individual items add an identifier.

/customers              the collection
/customers/58           one customer
/customers/58/orders    that customer's orders
/orders/1042            one order, addressed directly

Paths name things, and methods say what to do with them. An address such as /getCustomer or /deleteOrder?id=1042 mixes the verb into the noun, which REST style avoids. If the parts of an address are unfamiliar, URL anatomy labels each one.

What you receive is a representation of the resource: its state written out in some format. The same customer could be represented as JSON for a program or as HTML for a person, and content negotiation is how a client says which it prefers.

Methods do the work

HTTP already defines a small set of verbs with agreed meanings, and REST leans on them.

Method Typical use Safe Idempotent
GET Read a resource Yes Yes
POST Create under a collection, or trigger processing No No
PUT Replace a resource at a known address No Yes
PATCH Change part of a resource No Not guaranteed
DELETE Remove a resource No Yes

"Safe" means the method isn't meant to change anything. "Idempotent" means sending the same request twice has the same effect on the server as sending it once. Those properties decide what a client may repeat after a network error, which retries, backoff and idempotency covers in detail. For more on each verb, see HTTP methods explained.

The life of one resource through HTTP methods
DELETE /orders/1042 Remove or cancel it. Later reads report it is gone. PATCH /orders/1042 Change one field, such as the delivery note. POST /orders Create a new order. The server picks its address. GET /orders/1042 Read the order as it stands now. 1 2 3 4 POST /orders Create a new order. The server picks its address. 1 GET /orders/1042 Read the order as it stands now. 2 PATCH /orders/1042 Change one field, such as the delivery note. 3 DELETE /orders/1042 Remove or cancel it. Later reads report it is gone. 4

A worked example

Suppose you're adding a task to a to-do service. You send the new data to the collection:

curl -i https://api.example.com/v1/tasks \
  -H "Content-Type: application/json" \
  -d '{"title": "Renew domain", "done": false}'

A well-behaved server replies with 201 Created, a Location header pointing at the new resource, and usually the stored object:

HTTP/1.1 201 Created
Location: /v1/tasks/77
Content-Type: application/json

{"id": 77, "title": "Renew domain", "done": false}

To mark it finished, you'd send PATCH /v1/tasks/77 with {"done": true}. To read your open tasks, you might request GET /v1/tasks?done=false. Query parameters filter, sort and page through collections without inventing new paths.

The example uses the Bash line-continuation character, so on Windows PowerShell you'd put it on one line or adjust the quoting.

The constraints that define the style

Fielding's description rests on a few constraints, each with a practical payoff.

  • Client and server are separate. Each side can change independently as long as the interface holds.
  • Communication is stateless. The server doesn't rely on remembering your previous request. Credentials and context travel with every call, so any server in a pool can answer.
  • Responses say whether they can be cached. Reusing a stored reply saves time and load.
  • The interface is uniform. Resources, representations and self-describing messages work the same way across the whole system.
  • The system is layered. Proxies, caches and gateways can sit in the middle without the client knowing.

There is also an optional constraint called code on demand, where a server sends executable code to the client, as web pages do with scripts.

A REST request passes through layers the client never sees
Client Sends a complete request with its own credentials. Cache or CDN May answer a repeat read from a stored copy. Gateway Checks identity and traffic limits. Application servers Any one can handle it, since no session is held. Data store Holds the actual state of each resource. Client Sends a complete request with its own credentials. Cache or CDN May answer a repeat read from a stored copy. Gateway Checks identity and traffic limits. Application servers Any one can handle it, since no session is held. Data store Holds the actual state of each resource.

Status codes and errors

Using status codes correctly lets generic tools react sensibly. Common ones in REST APIs include 200 for success, 201 for creation, 204 for success with no body, 400 for a malformed request, 401 for missing or invalid credentials, 403 for a refused action, 404 for a missing resource, 409 for a conflict with the current state and 429 for too many requests. The HTTP status reference lists the rest.

The code alone isn't enough. A useful error body names the problem and the field involved. RFC 9457 defines a "problem details" JSON format for this, with members such as type, title, status and detail. Many APIs use their own shape, which is fine if it's consistent.

An API that returns 200 OK with an error message inside forces every client to inspect the body to discover failure. Avoid that when you design one.

Pagination, filtering and sorting

A collection with thousands of items can't come back in one response, so REST APIs split it into pages. Two designs are common.

Offset pagination asks for a slice by position, such as /orders?limit=20&offset=40. It's easy to understand and lets you jump to any page, but if items are added or removed while you're reading, you can see one twice or skip one. Large offsets can also be slow for the database.

Cursor pagination returns an opaque marker with each page. You send it back to get what comes next, as in /orders?limit=20&after=eyJpZCI6NDB9. It copes better with changing data and large sets, but you can only walk forward or backward from where you are.

Filtering and sorting also belong in the query string, for example ?status=open&sort=-created. There's no REST rule for the syntax, so the minus sign for descending order is one provider's habit, not a standard. Read the documentation, and always follow the pagination fields to the end before assuming you have everything. A script that silently processes only the first page is a classic bug, because it works perfectly while the test data is small.

Updating safely when two clients collide

Suppose two people open the same record and both save changes. With a plain PUT, the second write silently erases the first.

HTTP has a tool for this. The server sends an ETag header, a version identifier for the representation, with each read. When you update, you send it back in an If-Match header. If the resource has changed in the meantime, the server answers 412 Precondition Failed, and your client can fetch the new version and merge or ask the user. This technique is called optimistic concurrency.

The same ETag helps reads. Send it in If-None-Match, and an unchanged resource comes back as a short 304 Not Modified with no body, which saves bandwidth.

Where "REST" gets loose

Fielding's style includes hypermedia: responses contain links that tell the client what it can do next, much as web pages do. Few JSON APIs do this fully. Most publish fixed URL patterns in documentation and expect clients to build addresses from them.

People often call that "REST-like" or "HTTP JSON APIs". The distinction rarely matters when you're consuming an API. What matters is that the provider is consistent and documents its choices, since REST itself doesn't dictate field naming, pagination, date formats or error shapes.

REST also isn't the only option. It can be wasteful when a screen needs bits of many resources, which is one reason teams consider the alternative described in GraphQL vs. REST.

Designing or judging a good one

When you build an API or evaluate someone else's, check these points:

  1. Nouns in paths, consistent plurals, and no verbs where a method will do.
  2. Every collection paginated, so a list can't grow into an enormous response.
  3. Dates in a single unambiguous format, ideally ISO 8601 with a time zone.
  4. Errors with correct status codes and a stable machine-readable identifier.
  5. A versioning plan before the first outside client arrives.
  6. Authentication on every request, always over HTTPS.
  7. A way to repeat a POST safely, such as an idempotency key.

Resist exposing your database tables one to one. Resources should reflect what callers want to do, and internal column names make a poor long-term promise.

Common questions

Does REST require JSON?

No. REST says nothing about the format. JSON is popular because it's compact and easy to read in most languages, but XML, HTML and binary representations are all allowed.

Is HTTPS part of REST?

REST doesn't specify security, but any real API should use HTTPS so credentials and data are encrypted in transit.

How do I explore a REST API safely?

Start with GET requests, use the provider's sandbox or test credentials if offered, and read what each method changes before sending anything else. The introduction to what an API is covers the basics if a term here was new.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Developer & Network Tools →

Latest news on this topic