Web Development

What is REST API?

An API that follows REST (Representational State Transfer): resources identified by URLs and manipulated statelessly with standard HTTP methods.

Definition

A REST API is a web API designed according to REST, which stands for Representational State Transfer. Roy Fielding defined REST in his 2000 doctoral dissertation at UC Irvine as an architectural style describing what had made the web itself scale. REST is not a protocol or a standard; it is a set of constraints. A RESTful web service is one that follows them, and in practice almost all REST APIs run over HTTP and exchange JSON. In simple terms, a REST API exposes resources, the nouns of your system (users, orders, invoices), each with its own URL such as /users/42. Clients act on those resources with standard HTTP methods: GET reads, POST creates, PUT replaces, PATCH partially updates, and DELETE removes. The server answers with a representation of the resource, usually JSON, and a status code that says what happened: 200 OK, 201 Created, 204 No Content, 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 409 Conflict, 429 Too Many Requests, or 500 Internal Server Error. GET, PUT, and DELETE are idempotent, so repeating them has the same effect as sending them once; POST is not. What makes an API RESTful is Fielding's six constraints: client-server separation; statelessness, meaning each request carries everything needed to process it (such as an auth token) and the server keeps no session between requests; cacheability, where responses declare whether they may be cached; a uniform interface (resource identifiers, manipulation through representations, self-descriptive messages, and hypermedia as the engine of application state, or HATEOAS); a layered system, so proxies, gateways, and CDNs can sit in between transparently; and optional code on demand. Most real-world 'REST' APIs skip HATEOAS, which is why some people prefer to call them HTTP or JSON APIs. REST vs SOAP: SOAP is a protocol with a strict XML envelope, a WSDL contract, and add-on standards for security and reliable messaging (WS-*), and it typically sends every call as a POST to a single endpoint. REST uses HTTP's own methods, URLs, and status codes, allows any format, and is lighter and easier to cache. SOAP persists in banking, telecom, and older enterprise integrations; REST dominates public web APIs.

When to Use REST API

REST is the default choice for public and partner web APIs: every language has an HTTP client, responses can be cached by browsers and CDNs, and tools such as curl, Postman, and OpenAPI code generators work out of the box. It fits resource-oriented domains where clients mostly read and write whole entities. Consider GraphQL when many different clients need different slices of deeply related data and you keep adding bespoke endpoints to avoid over-fetching. Consider gRPC for high-throughput internal service-to-service calls, where a binary protocol and generated typed clients matter more than browser friendliness. Use WebSockets or server-sent events for real-time streams, and webhooks to notify other systems. If you choose REST, document it with OpenAPI, version it (/v1/ in the path or a header), paginate list endpoints, and return consistent error bodies.

REST API in Practice

  • CRUD on a books resource: GET /books -> 200, list of books GET /books/17 -> 200, one book (or 404) POST /books -> 201 Created, Location: /books/18 PUT /books/17 -> 200, book fully replaced PATCH /books/17 -> 200, selected fields updated DELETE /books/17 -> 204 No Content
  • Creating a resource over HTTP: POST /api/orders HTTP/1.1 Host: shop.example.com Authorization: Bearer eyJhbGciOi... Content-Type: application/json {"productId": 42, "quantity": 2} HTTP/1.1 201 Created Location: /api/orders/9001 Content-Type: application/json {"id": 9001, "status": "pending", "total": 59.98}
  • A minimal pair of REST endpoints in Express: app.get('/users/:id', async (req, res) => { const user = await db.users.findById(req.params.id); if (!user) return res.status(404).json({ error: 'Not found' }); res.json(user); }); app.post('/users', async (req, res) => { const user = await db.users.create(req.body); res.status(201).location(`/users/${user.id}`).json(user); });
  • A hypermedia (HATEOAS) response, the part most REST APIs omit, where the server tells the client which actions are available next: {"id": 9001, "status": "pending", "_links": {"self": {"href": "/orders/9001"}, "cancel": {"href": "/orders/9001/cancel"}, "pay": {"href": "/orders/9001/payment"}}}

Common Mistakes

  • Putting verbs in URLs (/getUser?id=42, /createOrder) instead of nouns plus HTTP methods (GET /users/42, POST /orders).
  • Returning 200 OK with an error message in the body. Use the status code that matches the outcome so clients, caches, and monitoring can react correctly.
  • Using GET for operations that change state. Crawlers, link prefetchers, and caches assume GET is safe and may trigger it unexpectedly.
  • Keeping session state on the server between requests, which breaks the stateless constraint and makes horizontal scaling behind a load balancer harder.
  • Assuming any JSON-over-HTTP API is REST. By Fielding's definition, an API without hypermedia controls is not fully RESTful, even if it is perfectly usable.

Keep the references coming

One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.