Restful API service
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A RESTful API service is a web service that exposes application data as resources and uses standard HTTP semantics to work with them. In practice, that means clients use predictable URLs, verbs such as GET and POST, and self-contained requests instead of RPC-style custom commands.
Model the System as Resources
The first design step is to identify resources, not actions. A bookstore API usually has resources such as books, authors, and orders. Each resource gets a stable URL pattern:
- '
GET /booksreturns a collection' - '
GET /books/42returns one book' - '
POST /bookscreates a book' - '
PATCH /books/42updates part of a book' - '
DELETE /books/42removes a book'
That structure matters because it makes the API predictable. A client should be able to guess how to navigate the service without learning a new naming scheme for every feature.
Use HTTP Semantics Correctly
REST is not just "JSON over HTTP." The protocol details carry meaning:
- '
GETshould be safe and read-only' - '
POSTis commonly used for creation' - '
PUTreplaces a full resource representation' - '
PATCHapplies a partial update' - '
DELETEremoves a resource'
Status codes are part of the contract too. Returning 200 OK for everything throws away useful information. A better service distinguishes between success and failure states:
- '
200 OKfor a normal successful read or update' - '
201 Createdafter a new resource is created' - '
204 No Contentwhen deletion succeeds with no body' - '
400 Bad Requestfor malformed input' - '
404 Not Foundwhen the resource does not exist' - '
409 Conflictwhen the request breaks a business rule'
Keep Requests Stateless
One of the core REST constraints is statelessness. The server should not depend on hidden conversational state from an earlier request. Every request must include the authentication data, target resource, and body needed to process it.
That design makes scaling easier because any application instance can handle any request. It also simplifies retries: if a network failure happens, the client can repeat the request without needing a special session object stored on the server.
Example Service in Express
The following Node.js example shows a small RESTful service for books:
This is still a simple in-memory example, but it demonstrates the important shape of a RESTful service: resources, verbs, status codes, and JSON representations.
Representations, Validation, and Versioning
Clients do not manipulate database rows directly. They exchange representations, usually JSON documents. That separation gives the service room to validate input, hide internal fields, and evolve storage independently.
Validation is part of that contract. If a client sends a book without a title, the service should reject it explicitly rather than letting bad data leak into persistence.
Versioning also belongs at the contract layer. Some teams use /v1/books; others prefer header-based versioning. The key is consistency and a clear deprecation path when a representation changes.
Common Pitfalls
- Designing endpoints around verbs such as
/createBookand/getBookusually means the API is drifting toward RPC instead of resource-oriented REST. - Ignoring HTTP status codes forces clients to guess what happened, which makes error handling brittle.
- Storing hidden session state on the server undermines RESTful scaling and complicates retries.
- Returning raw database models often leaks fields that should stay internal and makes future schema changes harder.
- Treating
PUTandPATCHas identical creates ambiguity around partial versus full updates.
Summary
- A RESTful API service exposes resources through predictable URLs and standard HTTP methods.
- Good REST design uses status codes, stateless requests, and explicit representations as part of the contract.
- The main design question is usually "what is the resource?" rather than "what action should I name?"
- Validation, clear error responses, and consistent versioning matter as much as the route shape.

