lesson
API Design
REST, GraphQL, gRPC — design principles, versioning, error handling.
API Design
REST API Best Practices
URL Structure
GET /api/v1/users → list users
GET /api/v1/users/123 → get user 123
POST /api/v1/users → create user
PATCH /api/v1/users/123 → update user 123
DELETE /api/v1/users/123 → delete user 123
GET /api/v1/users/123/orders → list user 123's ordersResponse Format
{
"data": { "id": 123, "name": "Alice" },
"meta": { "request_id": "abc-123" }
}Error format:
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Email is required",
"field": "email"
}
}Status Codes
| Code | Meaning | When |
|---|---|---|
| 200 | OK | Successful GET/PATCH |
| 201 | Created | Successful POST |
| 204 | No Content | Successful DELETE |
| 400 | Bad Request | Validation error |
| 401 | Unauthorized | Missing/invalid auth |
| 403 | Forbidden | Valid auth, insufficient permissions |
| 404 | Not Found | Resource doesn't exist |
| 429 | Too Many Requests | Rate limited |
| 500 | Internal Server Error | Bug in your code |
Pagination
GET /api/v1/users?page=2&per_page=20
GET /api/v1/users?cursor=abc123&limit=20 (cursor-based, preferred for large datasets)REST vs GraphQL vs gRPC
| REST | GraphQL | gRPC | |
|---|---|---|---|
| Format | JSON over HTTP | JSON over HTTP | Protocol Buffers over HTTP/2 |
| Best for | Public APIs, CRUD | Flexible client queries, mobile | Internal microservices, streaming |
| Overhead | Medium | Higher (query parsing) | Low (binary, multiplexed) |
| Versioning | URL-based (/v1, /v2) | Schema evolution | Proto file versioning |
Sign in to use the AI study buddy on this lesson.