REST

1 post tagged with REST

API Design Patterns for Modern Applications

Modern API design has evolved far beyond simple CRUD operations. Today’s applications require APIs that are resilient, scalable, and developer-friendly while supporting diverse client needs and complex business workflows. This guide explores proven patterns that address these challenges.

Foundational Design Principles

API-First Development

Design your API before implementation to ensure consistency and usability:

// Define API contract first
interface UserAPI {
  // Resource operations
  getUser(id: string): Promise<User>;
  updateUser(id: string, updates: Partial<User>): Promise<User>;
  deleteUser(id: string): Promise<void>;
  
  // Collection operations
  listUsers(filters: UserFilters, pagination: Pagination): Promise<PagedResult<User>>;
  searchUsers(query: SearchQuery): Promise<SearchResult<User>>;
  
  // Business operations
  activateUser(id: string): Promise<User>;
  deactivateUser(id: string): Promise<User>;
  resetUserPassword(id: string): Promise<void>;
}

// OpenAPI specification (generated or hand-written)
const userAPISpec = {
  openapi: '3.0.0',
  info: {
    title: 'User Management API',
    version: '1.0.0'
  },
  paths: {
    '/users/{id}': {
      get: {
        summary: 'Get user by ID',
        parameters: [
          {
            name: 'id',
            in: 'path',
            required: true,
            schema: { type: 'string', format: 'uuid' }
          }
        ],
        responses: {
          200: {
            description: 'User found',
            content: {
              'application/json': {
                schema: { $ref: '#/components/schemas/User' }
              }
            }
          },
          404: {
            description: 'User not found',
            content: {
              'application/json': {
                schema: { $ref: '#/components/schemas/Error' }
              }
            }
          }
        }
      }
    }
  }
};

Resource-Oriented Design

Structure APIs around resources, not actions: