Back to Hub
Low-Level Design (LLD)

API Design & UML Diagrams

#REST#API#UML#class-diagram#sequence-diagram#versioning

API Design & UML Diagrams

RESTful API Design

REST Principles

PrincipleMeaning
StatelessEach request contains all info needed; server stores no client state
Resource-basedURLs represent nouns (resources), not verbs (actions)
Uniform InterfaceStandard HTTP methods, consistent URL patterns
HATEOASResponses include links to related resources

URL Design

# ✅ Good — resource-oriented nouns, plural
GET    /api/v1/users          — List users
GET    /api/v1/users/123      — Get user 123
POST   /api/v1/users          — Create user
PUT    /api/v1/users/123      — Replace user 123
PATCH  /api/v1/users/123      — Partial update user 123
DELETE /api/v1/users/123      — Delete user 123

# Nested resources
GET    /api/v1/users/123/orders       — User 123's orders
GET    /api/v1/users/123/orders/456   — Specific order

# ❌ Bad — verbs in URLs, inconsistent
GET    /api/getUser?id=123
POST   /api/createNewUser
GET    /api/user/delete/123

HTTP Status Codes

CodeMeaningWhen to Use
200OKSuccessful GET, PUT, PATCH
201CreatedSuccessful POST (resource created)
204No ContentSuccessful DELETE
400Bad RequestValidation error, malformed input
401UnauthorizedMissing/invalid authentication
403ForbiddenAuthenticated but not authorized
404Not FoundResource doesn't exist
409ConflictDuplicate resource, version conflict
422Unprocessable EntityValid syntax but semantic errors
429Too Many RequestsRate limited
500Internal Server ErrorServer bug
503Service UnavailableServer overloaded/maintenance

Request/Response Design

typescript
// POST /api/v1/users // Request { "name": "Alice Chen", "email": "alice@example.com", "role": "admin" } // Response (201 Created) { "data": { "id": "usr_abc123", "name": "Alice Chen", "email": "alice@example.com", "role": "admin", "createdAt": "2024-01-15T10:30:00Z" }, "links": { "self": "/api/v1/users/usr_abc123", "orders": "/api/v1/users/usr_abc123/orders" } } // Error Response (400) { "error": { "code": "VALIDATION_ERROR", "message": "Request validation failed", "details": [ { "field": "email", "message": "Invalid email format" }, { "field": "name", "message": "Name is required" } ] } }

Pagination

typescript
// Offset-based (simple, common) GET /api/v1/users?page=2&limit=20 { "data": [...], "pagination": { "page": 2, "limit": 20, "total": 150, "totalPages": 8 } } // Cursor-based (better for large datasets, real-time) GET /api/v1/users?cursor=eyJpZCI6MTAwfQ&limit=20 { "data": [...], "pagination": { "nextCursor": "eyJpZCI6MTIwfQ", "hasMore": true } }

API Versioning Strategies

StrategyExampleProsCons
URL path/api/v1/usersSimple, explicitURL bloat
Query param/api/users?version=1Easy to defaultEasy to forget
HeaderAccept: application/vnd.api.v1+jsonClean URLsLess visible
Content negotiationAccept: application/json; version=1Standards-basedComplex

Recommendation: URL path versioning (/v1/) — most explicit and widely understood.


UML Diagrams

Class Diagram — E-Commerce Domain

Sequence Diagram — Checkout Flow

State Diagram — Order Lifecycle

API Design Checklist

  • Resources are nouns, not verbs
  • Consistent naming convention (camelCase or snake_case, never mixed)
  • Proper HTTP status codes
  • Pagination for list endpoints
  • Rate limiting with 429 and Retry-After header
  • Versioning strategy decided
  • Error responses are structured and consistent
  • Authentication/authorization on all endpoints
  • Input validation and sanitization
  • CORS configured for web clients
  • Request/response logging for debugging
  • API documentation (OpenAPI/Swagger)