A lightweight, type-safe API response helper for consistent backend responses
npm install typed-api-responseA lightweight, framework-agnostic utility to standardize API responses for Express and NestJS applications.
Stop repeating the same response structure in every controller.
---
- ✅ Clean & consistent API responses
- ✅ Works with Express & NestJS
- ✅ Fully written in TypeScript
- ✅ Zero dependencies
- ✅ Opinionated but flexible
- ✅ Tiny and fast
---
- Stop repeating the same response logic in every controller
- Keep API responses consistent and predictable
- Fully typed for TypeScript projects
- Easy to send success, error, or paginated responses
- Framework-agnostic: works with Express, NestJS, Fastify
- Tiny and dependency-free: minimal footprint, zero bloat
---
``bash`Using npm
npm install typed-api-response
`bash`Using yarn
yarn add typed-api-response
`bash`Using pnpm
pnpm add typed-api-response
---
🚀 Basic Usage
Express example
`ts
import express from "express";
import { success, error } from "typed-api-response";
const app = express();
app.get("/users", (req, res) => {
const users = [{ id: 1, name: "John" }];
success(res, users, "Users fetched successfully");
});
app.get("/error", (req, res) => {
error(res, 400, "Invalid request");
});
`
---
NestJS example
`ts
import { Controller, Get, Res } from "@nestjs/common";
import { Response } from "express";
import { success } from "typed-api-response";
@Controller("users")
export class UsersController {
@Get()
getUsers(@Res() res: Response) {
const users = [{ id: 1, name: "John" }];
return success(res, users, "Users fetched");
}
}
`
---
📄 Response Format
---
✅ Success Response
`ts`
{
"success": true,
"message": "Operation successful",
"data": {},
"meta": {}
}
❌ Error Response
`ts`
{
"success": false,
"message": "Something went wrong",
"errors": []
}
📘 API Reference
success(res, data, message?, status?, meta?)
Send a successful response.
`ts`
success(res, data, "Success", 200, { page: 1 });Parameters:
- res – HTTP response object
- data – Response payload
- message – Optional message (default: "Success")
- status – HTTP status code (default: 200)
- meta – Optional metadata object
---
error(res, status?, message?, errors?)
Send an error response.
`ts`
error(res, 404, "User not found");
---
paginated(res, data, page, limit, total, message?)
Send paginated response.
`ts`
paginated(res, users, 1, 10, 100);
Response meta example:
`ts``
{
"page": 1,
"limit": 10,
"total": 100,
"totalPages": 10
}
---
⭐ Why this package?
Most projects repeat the same response logic again and again.
typed-api-response keeps your API responses:
Consistent
Readable
Maintainable
If you like it, consider giving it a ⭐ on GitHub!