npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

api-response-handler

v2.0.0

Published

A universal API error handler for Node.js applications with standardized responses and error management.

Readme

🚀 Api Response Handler

Api Response Handler is a utility library designed for consistent, standardized, and user-friendly API responses in Express.js applications. It simplifies success, error, validation, and pagination responses while reducing repetitive boilerplate code.


📦 Installation

Install the package via npm:

npm install api-response-handler

📚 Usage

Import the Module

import HandleResponse from 'api-response-handler';

API Response Methods

🔹 1. Success Response

Send a generic success response.

app.get('/success', (req, res) => {
  HandleResponse.success(res, 'Data fetched successfully', { id: 1, name: 'Example' });
});

Response:

{
  "success": true,
  "message": "Data fetched successfully",
  "data": {
    "id": 1,
    "name": "Example"
  },
  "status": 200
}

🔹 2. Created Response

Send a success response specifically for resource creation.

app.post('/create', (req, res) => {
  HandleResponse.created(res, 'User created successfully', { id: 1 });
});

Response:

{
  "success": true,
  "message": "User created successfully",
  "data": {
    "id": 1
  },
  "status": 201
}

🔹 3. Validation Error

Handle validation failures.

app.post('/validate', (req, res) => {
  const errors = { field: 'email', message: 'Invalid email format' };
  HandleResponse.validationError(res, 'Validation failed', errors);
});

Response:

{
  "success": false,
  "message": "Validation failed",
  "details": {
    "field": "email",
    "message": "Invalid email format"
  },
  "status": 422
}

🔹 4. Unauthorized Access

Handle unauthorized access attempts.

app.get('/unauthorized', (req, res) => {
  HandleResponse.unauthorized(res);
});

Response:

{
  "success": false,
  "message": "Unauthorized Access",
  "status": 401
}

🔹 5. Forbidden Access

Handle forbidden requests.

app.get('/forbidden', (req, res) => {
  HandleResponse.forbidden(res);
});

Response:

{
  "success": false,
  "message": "Forbidden Access",
  "status": 403
}

🔹 6. Not Found

Handle requests to non-existent resources.

app.get('/not-found', (req, res) => {
  HandleResponse.notFound(res);
});

Response:

{
  "success": false,
  "message": "Resource Not Found",
  "status": 404
}

🔹 7. Server Error (Try-Catch Handling)

Handle unexpected server errors gracefully.

app.get('/server-error', (req, res) => {
  try {
    throw new Error('Unexpected error');
  } catch (error) {
    HandleResponse.serverError(res, error);
  }
});

Response:

{
  "success": false,
  "message": "Internal Server Error",
  "error": "Unexpected error",
  "status": 500
}

🔹 8. Paginated Response

Send a structured paginated response.

app.get('/paginated', (req, res) => {
  const data = [{ id: 1 }, { id: 2 }];
  HandleResponse.paginated(res, data, 1, 10, 50);
});

Response:

{
  "success": true,
  "message": "Data Retrieved Successfully",
  "data": [
    { "id": 1 },
    { "id": 2 }
  ],
  "pagination": {
    "page": 1,
    "limit": 10,
    "total": 50,
    "totalPages": 5
  },
  "status": 200
}

🔹 9. Generic Error

Send a custom error response with details.

app.get('/custom-error', (req, res) => {
  HandleResponse.genericError(res, 'Something went wrong', 400, { info: 'Custom issue' });
});

Response:

{
  "success": false,
  "message": "Something went wrong",
  "details": {
    "info": "Custom issue"
  },
  "status": 400
}

🔹 10. No Content

Send a 204 No Content response.

app.delete('/delete', (req, res) => {
  HandleResponse.noContent(res);
});

Response: (No Body Returned, HTTP Status Code 204)


📊 API Methods Overview

| Method | Parameters | Description | |--------------------|------------------------------------------------|--------------------------------------| | success | res, message, data, status | Generic success response | | created | res, message, data | Resource creation response | | validationError | res, message, details | Validation error response | | unauthorized | res, message | Unauthorized access | | forbidden | res, message | Forbidden access | | notFound | res, message | Resource not found | | serverError | res, error, message | Server error response | | paginated | res, data, page, limit, total, message | Paginated response | | genericError | res, message, status, details | Custom error response | | noContent | res, message | No content response (204) |


🤝 Contributing

Contributions are always welcome!

  • Open issues or feature requests on the GitHub repository.
  • Submit pull requests with descriptive explanations.

📄 License

This project is licensed under the MIT License.


🧑‍💻 Author

Pratik Panchal