REST
REST is a style for HTTP APIs: you treat URLs as resources and HTTP methods as verbs. GET /users/7 reads a user; POST /users creates one. Real APIs follow this loosely.
Detailed Explanation
Roy Fielding's REST includes constraints (statelessness, cacheability, a uniform interface) that most "REST APIs" only approximate. In everyday JavaScript work, REST means:
- Resources are nouns:
/orders/12/items, not/getOrder. - Methods matter:
GETis safe and cacheable,POSTcreates,PUTreplaces,PATCHupdates a subset,DELETEremoves. - Representations are JSON (usually) with
Content-Type: application/json. - Status codes carry meaning:
201+Locationafter create,409on conflict,422on validation (some APIs use400).
Statelessness: each request carries the credentials it needs (Authorization header, cookie). The server does not depend on a sticky in-memory login from the last TCP connection. Sessions stored server-side and referenced by a cookie are still common; they are a pragmatic mix.
HATEOAS (links in the body that tell you the next URL) is the part most teams skip. Clients hardcode paths. That is fine if you version (/v1/users) and do not break shapes.
GraphQL and RPC (/api/getUser) are alternatives. They are not wrong; they just are not REST. A single POST /graphql is a different contract.
What it costs you: bikeshedding nouns, and clients that ignore idempotency. A "REST" client that POSTs to delete is not using the style. Pagination (?cursor=), filtering, and partial responses (?fields=) are where APIs grow.
Reach for REST when resources map cleanly to URLs and HTTP caching helps. Reach for RPC when you have actions that are not resource updates (/checkout/charge). Reach for WebSockets when the resource changes too often for poll.
Errors need a body shape the client can parse: { error: { code, message } } plus the HTTP status. Returning 200 with { success: false } forces every caller to remember a house rule and breaks caches and probes. Use 429 with Retry-After for rate limits.
Partial updates should fail closed: unknown fields ignored or rejected, never silently stored, or you will not be able to add a field later without clients already sending junk into it.
Code Examples
const api = "/api";
export const users = {
list: () => fetch(`${api}/users`).then(read),
get: (id) => fetch(`${api}/users/${id}`).then(read),
create: (body) =>
fetch(`${api}/users`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
}).then(read),
remove: (id) => fetch(`${api}/users/${id}`, { method: "DELETE" }).then(read),
};
async function read(res) {
if (res.status === 204) return null;
if (!res.ok) throw new Error(`http ${res.status}`);
return res.json();
}Common Gotchas
`POST /deleteUser` is RPC wearing JSON
you lose HTTP cache and method-based proxies.
`GET` must not change server state
crawlers and prefetch will call it.
`PUT` vs `PATCH`
`PUT` sends the full resource; `PATCH` sends a delta. Mixing them confuses clients.
Version in the URL or `Accept` header
pick one and keep old clients working.