paskvilwiki

up: programming

RESTful APIs

last updated 2026-11

REST is a powerful way to design APIs because it's built on the most basic HTTP building blocks - URLs, methods, headers and status codes. any HTTP client, from curl to a browser, can talk to it, and caches, proxies and load balancers already understand it, without any extra tooling

what is RESTful?

REST (representational state transfer) is an architectural style, not a protocol - Roy Fielding described it in his 2000 dissertation as a set of constraints. there's no standard for what a REST API looks like, only a lot of precedent. in practice, a web REST API means reading and changing resources on a server with plain HTTP requests, usually with JSON bodies

the constraints, and what they mean for an HTTP API:

  • client-server - the client and the server only meet in the interface, so either can change on its own as long as the interface stays
  • stateless - every request carries everything needed to handle it, the server keeps no session between requests. this includes authentication - the client sends a token (Authorization: Bearer …, e.g. an OAuth 2 access token or a JWT) or an API key with every request, instead of logging in once and relying on a server-side session
  • cacheable - responses say whether, and for how long, they can be cached (Cache-Control), and give a way to check if a cached copy is still current (ETag / If-None-Match -> 304 Not Modified). good caching saves whole round trips
  • layered - the client can't tell whether it talks to the server itself or to a proxy, cache, load balancer or API gateway in between
  • uniform interface:
    • every resource has its own URI, e.g. /users/42 - the resource is separate from its representation (JSON, CSV, an image, …), and the client picks one with the Accept header
    • a client holding a representation has enough information to modify or delete the resource
    • each message is self-descriptive - method, headers and media type say how to process it
    • links in responses drive what the client can do next (HATEOAS) - this is the part almost every "REST" API skips, clients build URLs themselves from the documentation

web REST APIs typically have:

  • a base URL, often with a version - https://api.example.com/v1/
  • JSON as the data format (Content-Type: application/json) - XML is mostly gone outside of older enterprise systems
  • the standard HTTP methods for the CRUD actions (see below)
  • the HTTP status codes for the outcome, not a 200 with an error inside
  • a machine-readable description - today that's OpenAPI (formerly Swagger), which also gives you the docs, client code and request validation

REST isn't the only option any more - GraphQL suits clients that need to pick exactly which fields and related data they get in one request, and gRPC fast service-to-service calls. for a public API, plain REST with JSON is still the default

interface recommendations

my personal checklist for the gray areas, where REST leaves the choice to you - many points are a matter of taste, and some aren't really REST, but make life easier for you as the backend dev, for your API's consumers, or both

HTTP methods and CRUD

each CRUD action has its HTTP method, used on either a collection (/users) or a single item in it (/users/42):

action method URL on success
create POST /users 201 Created, Location: /users/42 header, the new item in the body
read GET /users/42 200 OK with the item
list GET /users 200 OK with the list (paginated, filtered by query parameters)
update PATCH /users/42 200 OK with the updated item, or 204 No Content
replace PUT /users/42 200 OK / 204 No Content, or 201 Created if it didn't exist
delete DELETE /users/42 204 No Content

two more that are handy - HEAD is a GET without the body (does it exist, has it changed), and OPTIONS lists the allowed methods in the Allow header (browsers also send it on their own, as the CORS preflight before cross-origin requests)

GET, HEAD and OPTIONS are safe - they never change anything. PUT and DELETE are idempotent - sending the same request twice has the same effect as once, so a client can just retry them after a timeout. POST and PATCH are neither, a retried POST can create a duplicate (APIs that care, like payment ones, take an Idempotency-Key header for that)

PUT vs PATCH - by the spec, PUT replaces the whole item with the body, so any field left out is cleared or reset to its default, while PATCH changes only the fields that are in the body. the convention today:

  • PATCH for edits - the client sends just what changed, and two clients editing different fields don't overwrite each other. the simplest body is a JSON merge patch (application/merge-patch+json, RFC 7396) - the fields to change, null removes a field
  • PUT only where replacing the whole thing is what you mean - a settings document, a file upload, or creating an item at an ID the client chooses (PUT /users/jo)
  • a PUT that behaves like a PATCH is a common shortcut, and a trap for clients that follow the spec - if you do it, document it

URLs

collections in plural or singular (/users/42 vs /user/42) is a matter of taste - but pick one and use it everywhere, a mix means the client has to look up every single URL. plural is the more common choice. the one good reason for an exception is a singleton, a resource that exists exactly once - /users/42/profile, /me, /settings - there's no collection to name in plural

the URL hierarchy says what belongs to what - /users/42/orders are the orders of user 42, and /users/42/orders/7 one of them. creating one with POST /users/42/orders needs no user_id in the body, the path already says it. keep it to a level or two - if a sub-entity has an identity of its own (an order can be looked up without its user), give it its own top-level URL too (/orders/7), rather than making clients walk the whole path

path vs query parameters

the path says which resource, the query how you want it - filtering, sorting, pagination, picking fields:

GET /users/42/orders
GET /users/42/orders?status=open&sort=-created&limit=20&cursor=abc123

a simple test - if dropping the parameter points to a different resource, it belongs in the path, if it only narrows or shapes the same one, in the query. data to create or change goes in the request body, never in the query - and so do secrets, URLs with their query strings end up in server logs, proxies and browser history

versioning

the API version goes in the path - /v1/users/42. bump it only for breaking changes (a removed or renamed field, a changed meaning), new fields and endpoints go into the current version. keep the old version running for a while, and announce its end

the version of an entity (when it keeps a history of revisions) is a query parameter - GET /documents/42 is the current one, GET /documents/42?version=3 an older revision. it's still the same document, only a different state of it, so it doesn't get its own path