Examples & Code Samples

Complete Examples for node-api-document

Learn how to integrate beautiful API documentation into your Express.js applications with real-world examples, best practices, and production-ready configurations.

Key Features

Automatic Documentation
Express.js Integration
Custom Headers Support
Security Features
Responsive UI

Basic Example

Simple integration with minimal configuration

Basic Setup

A minimal example showing how to integrate node-api-document into your Express.js application.

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"
  },
  {},
  {
    "new_tag": "1",
    "color": "black",
    "title": "COMMON API",
    "icon": "list"
  },
  {
    "new_tag": "0",
    "color": "orange",
    "icon": "flag",
    "title": `1 : COMMON`,
    "name": "Get Country List",
    "meth": "GET",
    "link": `http://localhost:3000/country_list`,
    "mandatory": "",
    "optional": "",
    "is_header": "YES",
    "is_push": "NO",
    "header": "API-KEY",
    "notes": "This API is used to get the country list",
    "example": "",
    "status": "<br>1. HTTP_OK [200] - status : success <br>2. HTTP_OK [201] - status : error <br>3. HTTP_NOT_FOUND [204]",
    "imp": ""
  },
  {}
];

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

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

Advanced Example

Production-ready configuration with comprehensive API documentation

Advanced Configuration

Complete example with environment variables, authentication, and comprehensive API documentation.

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

const app = express();

// Environment variables
const config = {
  APP_NAME: process.env.APP_NAME || 'My API Service',
  LOGO_URL: process.env.LOGO_URL || 'https://example.com/logo.png',
  BASE_URL: process.env.BASE_URL || 'http://localhost:3000',
  API_PASSWORD: process.env.API_PASSWORD || 'secure123'
};

// Comprehensive API documentation
const apiDoc = [
  {
    "new_tag": "1",
    "color": "purple",
    "title": "AUTHENTICATION",
    "icon": "shield"
  },
  {
    "new_tag": "0",
    "color": "red",
    "icon": "lock",
    "title": "1 : AUTH",
    "name": "User Login",
    "meth": "POST",
    "link": `${config.BASE_URL}/auth/login`,
    "mandatory": "email, password",
    "optional": "remember_me",
    "is_header": "NO",
    "is_push": "NO",
    "header": "",
    "notes": "Authenticate user and return JWT token",
    "example": '{"email": "user@example.com", "password": "password123"}',
    "status": "<br>1. HTTP_OK [200] - Login successful <br>2. HTTP_UNAUTHORIZED [401] - Invalid credentials <br>3. HTTP_BAD_REQUEST [400] - Missing fields",
    "imp": "Store the returned token for subsequent requests"
  },
  
  // User management section
  {
    "new_tag": "1",
    "color": "blue",
    "title": "USER MANAGEMENT",
    "icon": "users"
  },
  {
    "new_tag": "0",
    "color": "green",
    "icon": "user-plus",
    "title": "2 : USERS",
    "name": "Create User",
    "meth": "POST",
    "link": `${config.BASE_URL}/users`,
    "mandatory": "name, email, password",
    "optional": "phone, avatar",
    "is_header": "YES",
    "is_push": "NO",
    "header": "Authorization: Bearer <token>",
    "notes": "Create a new user account",
    "example": '{"name": "John Doe", "email": "john@example.com", "password": "secure123"}',
    "status": "<br>1. HTTP_CREATED [201] - User created <br>2. HTTP_CONFLICT [409] - Email already exists <br>3. HTTP_BAD_REQUEST [400] - Validation error",
    "imp": "Password must be at least 8 characters"
  },
  {
    "new_tag": "0",
    "color": "blue",
    "icon": "user",
    "title": "2 : USERS",
    "name": "Get User Profile",
    "meth": "GET",
    "link": `${config.BASE_URL}/users/profile`,
    "mandatory": "",
    "optional": "",
    "is_header": "YES",
    "is_push": "NO",
    "header": "Authorization: Bearer <token>",
    "notes": "Retrieve current user profile",
    "example": "",
    "status": "<br>1. HTTP_OK [200] - Profile retrieved <br>2. HTTP_UNAUTHORIZED [401] - Invalid token",
    "imp": "Requires valid JWT token"
  },
  
  // Data section
  {
    "new_tag": "1",
    "color": "orange",
    "title": "DATA OPERATIONS",
    "icon": "database"
  },
  {
    "new_tag": "0",
    "color": "yellow",
    "icon": "list",
    "title": "3 : DATA",
    "name": "Get Countries",
    "meth": "GET",
    "link": `${config.BASE_URL}/countries`,
    "mandatory": "",
    "optional": "limit, offset, search",
    "is_header": "NO",
    "is_push": "NO",
    "header": "",
    "notes": "Retrieve list of countries with optional filtering",
    "example": "?limit=10&offset=0&search=united",
    "status": "<br>1. HTTP_OK [200] - Countries retrieved <br>2. HTTP_BAD_REQUEST [400] - Invalid parameters",
    "imp": "Supports pagination and search"
  }
];

// Initialize documentation with custom headers
createDoc(app, 'Authorization, Content-Type, Accept', apiDoc);

// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`🚀 Server running on port ${PORT}`);
  console.log(`📚 API Documentation available at: http://localhost:${PORT}/api-doc/`);
});

Environment Setup

Configure your environment variables for optimal performance

Environment Variables

Required and optional environment variables for node-api-document configuration.

# .env file configuration
APP_NAME=My Awesome API
LOGO_URL=https://example.com/logo.png
BASE_URL=https://api.example.com
API_PASSWORD=your_secure_password_here

# Optional configurations
PORT=3000

Required Variables

  • • APP_NAME - Your project name
  • • LOGO_URL - Project logo URL
  • • BASE_URL - API base URL
  • • API_PASSWORD - Access password

Optional Variables

  • • NODE_ENV - Environment mode
  • • PORT - Server port
  • • DATABASE_URL - Database connection
  • • JWT_SECRET - JWT signing key

Best Practices

Follow these guidelines for optimal implementation

Security

Use strong API passwords
Implement rate limiting
Use HTTPS in production
Validate all inputs

Configuration

Use environment variables
Organize API documentation
Include proper error handling
Add comprehensive examples

User Experience

Provide clear descriptions
Include request/response examples
Document all status codes
Group related endpoints

Performance

Use caching strategies
Implement pagination
Optimize database queries
Monitor API performance

Ready to Get Started?

Start building beautiful API documentation for your Express.js applications today. Join thousands of developers who trust node-api-document for their documentation needs.