node-api-document API Reference

A utility function that integrates API documentation into an Express.js application. Provides a simple way to inject headers and API documentation into your app, ensuring consistency and reusability.

Quick Navigation

Jump to specific sections of the API reference.

Installation

Install node-api-document

Install the package using npm

npm install node-api-document

createDoc Function

Function
createDoc()

Creates middleware for API documentation and integrates it into your Express app.

Syntax

createDoc(app, acceptHeaders, apiDoc)

Parameters

NameTypeRequiredDescription
app
Express.Application
Required
The Express.js application instance.
acceptHeaders
string
Optional
A string representing custom headers to be added to requests.
apiDoc
Array<Object>
Optional
An array of objects defining the API documentation structure.

Returns

Promise<void>

Complete Example

const express = require('express');
const createDoc = require('node-api-document');

const app = express();

const apiDoc = [
  {},
  {
    "new_tag": "0",
    "color": "blue",
    "icon": "http",
    "title": "DIRECT LINK",
    "name": "FAQ",
    "meth": "Link",
    "link": "http://localhost:3000/faq",
    "imp": "FAQ"
  }
];

createDoc(app, 'token, api-key', apiDoc);

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

Environment Variables

Required
APP_NAME

The name of your project.

APP_NAME=Your Project Name

Optional
LOGO_URL

The URL for your project logo. This will be displayed in relevant sections.

LOGO_URL=Your Project Logo URL

Required
BASE_URL

The base URL for your project's API. Ensure this points to the correct API endpoint.

BASE_URL=Your Project API Base URL

Optional
API_PASSWORD

Password to access the API documentation. This adds an additional layer of security.

API_PASSWORD=123

Important Notes

  • • Ensure all URLs are accessible and correctly formatted.
  • • Keep the API_PASSWORD secure to prevent unauthorized access.
  • • Optional variables enhance the UI but are not critical for core functionality.

API Documentation Structure

API Doc Object Properties

Each object in the apiDoc array should follow this structure:

PropertyTypeRequiredDescription
new_tag
string
Optional
A flag to denote new items ("0" or "1").
color
string
Optional
Display color for the item.
icon
string
Optional
Icon representing the item.
title
string
Optional
Title of the documentation section.
name
string
Optional
Name of the API or link.
meth
string
Optional
HTTP method (e.g., GET, POST, Link).
link
string
Optional
The API endpoint or link.
mandatory
string
Optional
Mandatory parameters for the API.
optional
string
Optional
Optional parameters for the API.
is_header
string
Optional
Whether headers are required ("YES" or "NO").
is_push
string
Optional
Whether push notifications are enabled ("YES" or "NO").
header
string
Optional
Header name (e.g., "API-KEY").
notes
string
Optional
Additional notes about the API.
example
string
Optional
Example usage of the API.
status
string
Optional
Status codes and their meanings.
imp
string
Optional
Additional important notes.

Usage Examples

Basic Setup

Simple integration with Express.js

const express = require('express');
const createDoc = require('node-api-document');

const app = express();

const apiDoc = [
  {},
  {
    "new_tag": "0",
    "color": "blue",
    "icon": "http",
    "title": "DIRECT LINK",
    "name": "FAQ",
    "meth": "Link",
    "link": "http://localhost:3000/faq",
    "imp": "FAQ"
  }
];

createDoc(app, 'token, api-key', apiDoc);

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

Error Handling

Error Handling

How errors are handled during documentation setup

If an error occurs during the setup of the documentation, the promise will reject, and the error will be logged to the console:

error in CreateDoc =-----> Error Details