REST API
A stateless web API architectural style that identifies resources via URIs and manipulates them using standard HTTP methods and status codes
Table of Contents
Structure Diagram
What is REST API?
A stateless web API architectural style that identifies resources via URIs and manipulates them using standard HTTP methods and status codes. It is a core concept in APIs & Networking within Web Dev.
Core Principles
REST (Representational State Transfer) is an architectural style defined by Roy Fielding that aligns API design with the native semantics of HTTP. Domain entities are modeled as resources addressed by noun-based URIs (such as /users/42/orders), while actions map to standard HTTP verbs—GET (read), POST (create), PUT/PATCH (update), and DELETE (remove).
Because REST is stateless, every request carries all information required to authenticate and execute it. Servers do not need sticky sessions in memory, which makes horizontal scaling behind load balancers and standard HTTP caching (Cache-Control, ETag) effortless.
Practical Takeaways
- Use plural nouns in URIs and honest HTTP status codes: Favor
GET /ordersover/getOrders, and return accurate codes (201 Created,400 Bad Request,401 Unauthorized,404 Not Found,429 Too Many Requests) so clients and proxies behave predictably. - Design for idempotency:
GET,PUT, andDELETEshould leave the system in the same state even when retried. For non-idempotentPOSToperations like payments, require anIdempotency-Keyheader to prevent duplicate charges. - Plan pagination and versioning early: Introduce cursor-based pagination (
?cursor=...&limit=20) and a clear versioning policy (/v1/...) upfront so breaking schema changes never brick older mobile clients.
Notable Examples
- Stripe, GitHub, and Twilio APIs: Industry benchmarks for clean resource modeling, consistent error payloads, and idempotent mutations.
- Unified Web & Mobile Backends: Serving JSON representations to web SPAs, iOS, and Android apps from a single HTTP surface.
- Edge-cached public endpoints: Pairing
GETREST endpoints with CDNCache-Controlheaders to serve millions of reads without hitting origin databases.
Practical Code Snippet
Copy and drop straight into your project// GET /api/terms?domain=dev&limit=20export async function GET(request: Request) { const { searchParams } = new URL(request.url); const domain = searchParams.get("domain"); const terms = await db.terms.findMany({ where: domain ? { domain } : undefined, take: Number(searchParams.get("limit") ?? 20), }); return Response.json({ data: terms }, { status: 200 });}