API Response Standardization
BuildAppsWith implements a comprehensive API response standardization system to ensure consistency, type safety, and predictable error handling across all API endpoints.Core Standardization Pattern
All APIs follow the StandardApiResponse pattern, which provides a consistent structure across all domains:Key Benefits
- ✅ Consistent Response Structure: All APIs return the same response format
- ✅ Type Safety: Full TypeScript support with generic data types
- ✅ Predictable Error Handling: Standardized error codes and messages
- ✅ Marketplace Compatibility: Aligned with proven marketplace API patterns
- ✅ Component Integration: UI components can rely on consistent data shapes
Success Response Example
Error Response Example
Paginated Response Example
Standardized Error Codes
All APIs use consistent error codes from theApiErrorCode enum:
Utility Functions
toStandardResponse()
Convert data to standardized response format:convertToMarketplaceFormat()
Convert legacy responses to marketplace-compatible format:normalizeNullToUndefined()
Transform database null values to undefined for consistent API responses:Implementation Examples
API Route Implementation
Client-side API Function
Component Usage
Migration Guidelines
From Legacy APIs
For existing APIs that don’t follow the standardized pattern:-
Wrap existing responses with
toStandardResponse(): -
Update error handling to use standardized error codes:
-
Update client functions to handle StandardApiResponse:
Domain-Specific Implementations
Profile APIs
- BuilderProfileResponse: Flattened response structure for component compatibility
- Session type handling: Proper null/undefined property normalization
- Validation tier alignment: Consistent enum usage across marketplace and profile domains
Marketplace APIs
- Reference implementation: Marketplace APIs serve as the proven foundation
- Pagination support: Built-in pagination following marketplace patterns
- Filter handling: Standardized query parameter processing
Scheduling APIs
- Session type compatibility: Database property alignment (color: string | null)
- Booking confirmation: Standardized booking response formats
- Calendar integration: Consistent Calendly API response handling
Best Practices
1. Always Use Standardized Responses
2. Consistent Error Handling
3. Type-Safe Client Functions
4. Component Error Boundaries
Testing Standardized APIs
Unit Tests
Integration Tests
Impact and Results
The API standardization implementation has delivered significant improvements:- Error Reduction: 27+ TypeScript errors resolved
- Type Safety: Consistent interfaces across all APIs
- Component Reliability: Predictable data structures for UI components
- Developer Experience: Clear error messages and consistent patterns
- Marketplace Alignment: All APIs follow proven marketplace patterns