API Design Quiz
API Design Quiz — Study Guide
API Design: A Comprehensive Study Guide
APIs are the backbone of modern software — they let applications talk to each other, power mobile apps, and enable third-party integrations. Designing APIs well means building systems that are predictable, secure, and easy to use. Whether you're building a REST endpoint or a gRPC service, understanding these core concepts will make you a better engineer and prepare you for real-world API challenges.
HTTP Fundamentals & Status Codes
HTTP is the protocol that powers the web. Every API request/response follows its structure: a method, headers, a URL, and optionally a body.
Common HTTP Methods
| Method | Purpose | Idempotent? | Safe? |
|---|---|---|---|
| GET | Retrieve data | ✅ Yes | ✅ Yes |
| POST | Create resource | ❌ No | ❌ No |
| PUT | Replace resource | ✅ Yes | ❌ No |
| PATCH | Partial update | ❌ No | ❌ No |
| DELETE | Remove resource | ✅ Yes | ❌ No |
PUT /users/1 with the same body always results in the same state — safe to retry. POST /users creates a new user each time — not idempotent.Status Codes You Must Know
💡 401 vs 403: Think of a nightclub. 401 means "I don't know who you are — show your ID." 403 means "I know who you are, but you're not on the VIP list."
REST & Architecture
REST (Representational State Transfer) is an architectural style with key constraints:
/users/42 not /getUser?id=42HATEOAS
HATEOAS (Hypermedia As The Engine Of Application State) means responses include links to related actions:{
"id": 42,
"name": "Alice",
"_links": {
"self": "/users/42",
"orders": "/users/42/orders",
"delete": "/users/42"
}
}This lets clients discover available actions dynamically rather than hardcoding URLs.
Authentication & Security
Auth Patterns
X-API-Key: abc123). Easy but hard to rotate.username:password. Only use over HTTPS.CORS (Cross-Origin Resource Sharing)
Browsers block requests from one origin to another by default. CORS headers tell browsers which origins are allowed:Access-Control-Allow-Origin: https://myapp.com
Access-Control-Allow-Methods: GET, POSTSecurity Best Practices
Pagination
When returning large datasets, pagination is essential.
| Type | How It Works | Best For |
|---|---|---|
| Offset | ?page=2&limit=20 | Simple, small datasets |
| Cursor | ?after=cursor_xyz | Large/real-time data |
| Keyset | ?after_id=100 | Sorted, stable datasets |
Rate Limiting
Rate limiting protects your API from abuse and ensures fair usage. Common algorithms:
HTTP/1.1 429 Too Many Requests
Retry-After: 30
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 0API Versioning
Versioning prevents breaking changes from affecting existing clients.
/v1/users ← URL versioning (most common)
/users (Accept: application/vnd.api.v2+json) ← Header versioningAPI Gateway
An API Gateway sits in front of your services and handles:
Think of it as a security checkpoint + traffic director for all incoming API calls.
Webhooks
Instead of polling "do you have new data?", webhooks push data to you when events happen.
Your App ──registers──▶ API: "POST to https://myapp.com/webhook on new order"
API ──fires──▶ POST https://myapp.com/webhook { "event": "order.created", ... }Always verify webhook signatures to confirm the request is legitimate.
GraphQL vs REST vs gRPC
| REST | GraphQL | gRPC | |
|---|---|---|---|
| Protocol | HTTP | HTTP | HTTP/2 |
| Data format | JSON | JSON | Protobuf (binary) |
| Flexibility | Fixed endpoints | Client-defined queries | Strongly typed contracts |
| Best for | Public APIs | Complex data needs | Internal microservices |
Protobuf
gRPC uses Protocol Buffers (protobuf) — a binary serialization format that's faster and smaller than JSON. You define a schema:message User {
int32 id = 1;
string name = 2;
}Content Negotiation
Clients can request specific formats using theAccept header:
Accept: application/json
Accept: application/xml
The server responds with the Content-Type header indicating what it actually returned.