Standardized Response Patterns
A consistent response format for all your API endpoints. Every response includes a success boolean, making it easy for frontend developers to handle responses without checking HTTP status codes.
On this page
We Created a Standard
Every API response includes a success boolean.
Simple, consistent, and type-safe.
The Standard Format
The success boolean is always present—that's what makes it work.
Why We Created This Standard
Frontend developers waste hours dealing with inconsistent API responses. We fixed that.
Without a standard, you're checking HTTP status codes everywhere: response.status === 404, response.status === 400, and still guessing the response format.
The Problem
- •Each endpoint returns a different format
- •Errors come in different shapes
- •No TypeScript type safety
- •Custom handling for every endpoint
The Solution
- •One format for all endpoints
- •Check
result.successinstead of status codes - •TypeScript knows what properties exist
- •One handler works everywhere
It's This Easy
Use simple helper methods. That's it.
Backend - Just use the helpers
typescript
Why It Makes Sense
Consistent
Same format everywhere
Type-Safe
TypeScript knows the shape
Simple
One handler, zero overhead
Note: This is a recommended pattern, not a requirement.
Use it to create consistent, maintainable APIs.
How It Works
Backend: Send Responses
Frontend: Use Responses
The Benefit
Every response has a success boolean. TypeScript automatically knows:
- If
success: true, thendataexists - If
success: false, thenerrorexists - No need to check HTTP status codes—just check
result.success
Common Patterns
Success Responses
typescript
Error Responses
typescript
Pagination
typescript
Frontend Usage
TypeScript Setup
typescript
Using Responses - TypeScript Knows Everything
typescript
Why This Is Better
- No status code checking: Just check
result.success - TypeScript auto-complete: TypeScript knows what properties exist
- Consistent format: All endpoints work the same way
- One handler: Write one function that works for all endpoints
Reference
Success Response
Error Response
Three Ways to Send Responses
Method 1: Direct res.* Methods (Recommended)
The most intuitive way - use methods directly on the response object. These automatically set the correct HTTP status codes.
Direct res.* Methods
typescript
Method 2: Import the response Helper Object
For building response objects (you set the status manually).
Using response Helper
typescript
Method 3: Use ResponseBuilder for Complex Scenarios
For complex scenarios with method chaining.
Using ResponseBuilder
typescript
Direct res.* Methods (Recommended)
Success Methods
typescript
Error Methods
typescript
Validation Errors
Manual Validation with res.validationError()
typescript
With Zod Validation (Automatic)
typescript
Pagination
Using res.paginated()
typescript
Pagination Metadata
The pagination object automatically includes:
page- Current page numberlimit- Items per pagetotal- Total number of itemstotalPages- Total number of pages (calculated)hasNext- Boolean indicating if there's a next pagehasPrev- Boolean indicating if there's a previous page
TypeScript Support
Full Type Inference
typescript
Type-Safe Response Functions
typescript
Complete Example
Complete REST API with Standardized Responses
typescript
Best Practices
Do
- • Use direct res.* methods for simplicity
- • Always use standardized responses
- • Provide helpful, actionable messages
- • Use pagination for lists
- • Combine with Zod validation
- • Handle errors consistently
- • Use res.noContent() for delete operations
Don't
- • Mix response formats across endpoints
- • Return all items without pagination
- • Use generic error messages
- • Skip validation for "trusted" inputs
- • Forget to set proper status codes
- • Return inconsistent error structures
- • Ignore TypeScript type safety
All Available Response Methods
Direct res.* Methods (Recommended)
Success responses
res.success(data, message?)- 200 OKres.created(data, location?)- 201 Createdres.noContent()- 204 No Contentres.paginated(data, pagination)- 200 OK with pagination
Error responses
res.badRequest(message?)- 400 Bad Requestres.unauthorized(message?)- 401 Unauthorizedres.forbidden(message?)- 403 Forbiddenres.notFound(resource?)- 404 Not Foundres.conflict(message)- 409 Conflictres.validationError(errors)- 422 Unprocessable Entityres.rateLimited(retryAfter?)- 429 Too Many Requestsres.internalError(message?)- 500 Internal Server Error