1. Front Matter
Title: API Response Standardization
Author: scorevi (Sean Patrick Caintic)
Reviewers: itswyza (Yza Santiago)
Created: January 2026
Status: Approved
References:
Issue: [W1.4] Quest W1.4 #113
Milestone: [W1] Backend Infrastructure
2. Introduction & Goals
Problem Summary: Before W1.4, API routes returned inconsistent formats (
{ success, data },{ data },{ message }). Error codes were ad-hoc or missing, making frontend parsing unreliable.Goals:
Unified response format:
{ success, data, error }Standardized error codes (ApiErrorCode enum)
Helper class to enforce consistency
Auto-format Zod validation errors
Non-Goals:
GraphQL support
Response compression
Response caching headers
Glossary:
ApiResponseHelper: Utility class for building standardized responses
ApiErrorCode: Enum of standardized error codes
3. High-Level Architecture
System Diagram:

Technologies Used:
Next.js Route Handlers - API endpoints
Zod - Input validation
TypeScript - Type-safe responses
4. Detailed Design & Implementation
Data Model / Schema:
Success Response:
Field
Type
Description
successtrueAlways true for success
messagestring?Optional success message
dataTResponse payload
errornullAlways null on success
Error Response:
Field
Type
Description
successfalseAlways false on error
messagestringHuman-readable error
datanullAlways null on error
error.codeApiErrorCodeStandardized error code
error.messagestringTechnical description
error.detailsunknown?Optional context
API Specification:
Error Codes:
VALIDATION_ERROR(400) - Zod schema failedBAD_REQUEST(400) - Malformed requestUNAUTHORIZED(401) - Not authenticatedFORBIDDEN(403) - Insufficient permissionsNOT_FOUND(404) - Resource not foundCONFLICT(409) - Duplicate/conflictTOKEN_LIMIT_EXCEEDED(429) - Rate limit hitINTERNAL_ERROR(500) - Server failure
Logic & Workflows:
Helper Methods:
success(data)- Standard success (200)created(data)- Resource created (201)badRequest(msg)- Invalid request (400)unauthorized()- Not logged in (401)forbidden()- No permission (403)notFound()- Missing resource (404)validationError(msg, details)- Schema fail (400)internalError(msg)- Server error (500)fromError(error)- Auto-parse exception (varies)
Key Files:
lib/api/response.ts- ApiResponseHelper classlib/api/validators.ts- Zod validation utilitieslib/api/verifyContentOwnership.ts- Access verification
5. Infrastructure & Operations
Dependencies:
Next.js - Route handling
Zod - Validation
Monitoring & Alerting: Standard error logging via
console.errorfor 500 errors.Deployment Plan:
Deploy helper class
Migrate existing routes incrementally
No breaking changes to frontend
6. Testing & Quality Assurance
Test Strategy:
Unit: ApiResponseHelper methods
Integration: Route response formats
Known Limitations:
No response caching
No pagination helpers (separate concern)
7. Maintenance & Support
Troubleshooting:
Frontend not parsing errors → Verify
error.codeexistsValidation error details missing → Check
error.details
Changelog:
1.0 (Feb 2026): Initial implementation
Document Version
1.0 - Approved, Infrastructure deployed to production, 02/15/2026