🌙
☀️ Dark
PART 21

APIs

REST, pagination, webhooks, backwards compatibility.

Advanced 45 min read

Revision Sheet

REST = Representational State Transfer. Noun-based URLs + HTTP Methods. Use Status Codes. Standardize errors. Version endpoints. Make POSTs safe with idempotency keys. Use GraphQL for flexible client queries. Use gRPC for fast internal communication.

Connections

This chapter connects tightly with Chapter 18 (HTTP and Networking), Chapter 22 (Security & Auth), and Chapter 25 (Microservices).

🏠 Curriculum NextVolume 2

Mini Project (20-30 min)

▶ View Solution

Build a simple API with Express handling GET /tasks, complete with offset pagination, JSON error formatting, and hardcoded rate limiting middleware (using an in-memory map of IPs to request counts).

Bigger Project (1-2 hours)

Apply all concepts from this volume to build a comprehensive feature.

▶ View Solution
typescript
// Example project code here

Interview Questions

Easy: What is the difference between PUT and PATCH?

PUT replaces the entire resource. PATCH partially updates it.

Medium: What does Idempotency mean in the context of REST?

An operation is idempotent if making multiple identical requests has the same effect as making a single request (e.g., GET, PUT, DELETE).

Hard: How do you design an API endpoint for a bulk update of resources without hitting URL length limits?

Use a POST request with the bulk payload in the JSON body, or a specialized PATCH endpoint with a standardized payload.

Senior: Describe how you would implement cursor-based pagination on a high-throughput endpoint, and contrast it with offset pagination.

Offset pagination (`LIMIT X OFFSET Y`) becomes slow on large datasets because the database must scan and discard Y rows. Cursor pagination uses an indexed column (like ID or timestamp) to fetch rows `WHERE id > last_seen_id LIMIT X`, which is much faster and stable against concurrent inserts.