Standardized HTTP responses for Node.js

A lightweight, flexible utility to format and send consistent HTTP responses in Express, Fastify, or any Node.js framework.

bash
npm install http-reply

Features

Everything you need for clean API responses.

Framework Agnostic

Compatible with Express, Fastify, or custom frameworks via adapters.

Standardized Responses

Ensures consistent response structure across your entire API.

Configurable Options

Customize response fields, timestamps, logging, and more.

Static Methods

Send responses quickly without instantiating the class.

TypeScript Support

Fully typed with included TypeScript declarations for a better DX.

ESM + CJS Support

Native support for both ES6 modules and CommonJS without hacks.

Usage

Quick Start with common patterns.

javascript
import { HttpReply } from 'http-reply';
import express from 'express';

const app = express();

app.get('/example', (req, res) => {
  HttpReply.success(res, {
    message: 'Operation successful',
    data: { user: 'John Doe' },
    metaData: { requestId: '12345' },
  });
});

app.listen(3000, () => console.log('Server running on port 3000'));

Supported Methods

Built-in helpers for common HTTP status codes.

MethodStatus CodeDescription
success200Successful request
created201Resource created successfully
accepted202Request accepted for processing
noContent204No content to return
badRequest400Invalid request
unauthorized401Authentication required
forbidden403Access denied
notFound404Resource not found
conflict409Resource conflict
tooManyRequests429Rate limit exceeded
error500Internal server error
notImplemented501Feature not implemented
serviceUnavailable503Service temporarily unavailable
responseCustomGeneric response with custom status code

Configuration Options

Customize HttpReply to fit your needs.

OptionTypeDefaultDescription
includeTimestampBooleanfalseInclude a timestamp in the response.
includeCodeBooleantrueInclude the status code in the response body.
includeMessageBooleantrueInclude the message in the response body.
includeErrorBooleantrueInclude error details in the response body.
includeMetaDataBooleantrueInclude metadata in the response body.
enableLoggingBooleantrueEnable error logging for invalid configurations.
stringifyBooleanfalseStringify the response body before sending.
customFieldsObject{}Additional fields to include in every response.
dateFormatString'unix'Format for timestamps ('iso' or 'unix').
adapterFunctionnullCustom adapter for non-Express/Fastify frameworks.

Custom Adapter

For non-Express/Fastify frameworks, provide a custom adapter to bridge HttpReply with your server's response object.

javascript
import { HttpReply } from 'http-reply';

const reply = new HttpReply({
  adapter: (res, statusCode, payload) => {
    res.setStatusCode(statusCode);
    res.setBody(payload);
    return res;
  },
});

reply.success(res, {
  message: 'Custom adapter response',
  data: { key: 'value' },
});

Response Shape

Standardized JSON output structure.

Default Output
json
{
  "message": "User fetched",
  "data": { "id": 1, "name": "John" },
  "metaData": { "total": 1 },
  "code": 200
}
With Timestamp
json
{
  "message": "User fetched",
  "data": { "id": 1, "name": "John" },
  "metaData": { "total": 1 },
  "code": 200,
  "timestamp": "2025-06-13T17:25:00.000Z"
}