API Fundamentals
/Intermediate
REST Principles
Definition
The six guiding constraints established by Roy Fielding that define a true RESTful system: Client-Server, Stateless, Cacheable, Uniform Interface, Layered System, and Code on Demand (optional).
Explain Like I'm New
To call your API 'RESTful', you must follow the rules. 1. Client and Server must be separate. 2. Server must not memorize client sessions (Stateless). 3. Responses must declare if they can be cached. 4. Endpoints must be uniform and predictable. 5. The client shouldn't know if it's talking to the real server or a proxy (Layered).
Real World Example
A mobile app (Client) talking to a Node.js API (Server). Because the API is Stateless, if the Node server crashes and restarts, the mobile app doesn't break, because the next request it sends contains the Auth Token anyway.
Common Use Cases
- •System Architecture
- •API Design Reviews
Terminal Output
bash / terminal
/*
Focus on the 'Stateless' Principle
*/
// ❌ Stateful (Violates REST)
// Request 1: POST /login (Server saves 'isLoggedIn = true' in its memory)
// Request 2: GET /account (Server checks its memory. Yes, they are logged in!)
// *If Server crashes between Request 1 & 2, Request 2 fails.*
// ✅ Stateless (True REST)
// Request 1: POST /login (Server returns a JWT token to the client, saves nothing)
// Request 2: GET /account + Header: "Authorization: Bearer <Token>"
// (The server verifies the token math. No memory needed. Infinitely scalable.)
Interview Questions
basic
- Which REST constraint dictates that the client UI and the backend database must be completely separate entities?
intermediate
- What is the 'Uniform Interface' constraint?