API Documentation Generator
raw JSON →apidoc is a command-line utility and programmatic library for generating comprehensive RESTful web API documentation directly from comments embedded within source code. It supports a wide array of programming languages by parsing specialized JSDoc-style comments (e.g., `/** ... */` for JavaScript/Java/C#, `""" ... """` for Python) marked with `@api` tags. The current stable version is 1.2.0, released in February 2024, reflecting a consistent, moderate release cadence focused on stability and feature enhancements. Its key differentiators include a language-agnostic parsing engine that understands various comment styles, a flexible plugin architecture for extending functionality (such as integrating API schemas), and robust programmatic integration options for custom build pipelines. The output is static HTML documentation, making it easy to host and integrate into any project.
Common errors
error apidoc: command not found ↓
npm install -g apidoc or yarn global add apidoc. Verify your npm/yarn global bin directory is correctly set up in your shell's PATH. error Error: Node.js v14 is not supported. Please upgrade to v16 or higher. ↓
nvm install 16 && nvm use 16. error TypeError: Cannot read properties of undefined (reading 'data') or 'project' ↓
typeof docResult !== 'boolean' after calling createDoc. If it's true, an error occurred. Ensure silent: false in your configuration to see console errors, and verify your src directory exists and contains valid @api comments. error The generated API documentation HTML bundle is empty or contains only boilerplate, despite source files having comments. ↓
--input (or src) paths and ensure --exclude (or excludeFilters) options are not overly restrictive. Verify your source files contain correctly formatted @api comments. If on an older apidoc version, upgrading to v1.0.0+ resolves a known bundle generation bug. Warnings
breaking Starting with apidoc v1.0.0, Node.js v16 or newer is explicitly required. Running on older Node.js versions (e.g., v14) will result in execution errors. ↓
gotcha Prior to apidoc v1.0.0, there was a known issue where the documentation bundle might not be generated into the output directory unless the tool was run with the `--debug` flag. This could lead to seemingly successful runs with empty documentation folders. ↓
gotcha Older versions of apidoc (pre-1.0.2) experienced issues with group names containing non-Latin characters or parentheses, which could cause rendering problems, specifically affecting scrollspy navigation. ↓
gotcha Incorrectly formatted or incomplete JSDoc-style `@api` comments can lead to silent failures, partial documentation, or parsing errors during generation. This is especially common with complex `@apiParam` or `@apiSuccessExample` blocks. ↓
Install
npm install apidoc yarn add apidoc pnpm add apidoc Imports
- createDoc wrong
const { createDoc } = require('apidoc')correctimport { createDoc } from 'apidoc' - apidoc CLI wrong
node apidoc -i src/ -o doc/correctapidoc -i src/ -o doc/ - ApidocReturn type
import { createDoc, ApidocReturn } from 'apidoc'
Quickstart
import path from 'path';
import { createDoc } from 'apidoc';
// Resolve paths relative to the current script execution
const projectRoot = path.resolve(__dirname, '..');
// Using 'example' folder from a cloned apidoc repository for demonstration
const srcPath = path.resolve(projectRoot, 'example');
const destPath = path.resolve(projectRoot, 'docs');
console.log(`Attempting to generate API documentation from: ${srcPath} to: ${destPath}`);
const docResult = createDoc({
src: srcPath,
dest: destPath,
// Set dryRun to true to parse and return data without writing files to disk
dryRun: false,
// Set silent to true to suppress apidoc's console output during generation
silent: false,
// Optional: Define custom parsing options or template paths here
});
if (typeof docResult !== 'boolean') {
console.log('API documentation generated successfully!');
console.log('Parsed API definitions count:', docResult.data.length);
console.log('Project information:', docResult.project.name, docResult.project.version);
} else {
console.error('Failed to generate API documentation. Check logs for detailed errors and ensure source paths are correct and comments are well-formed.');
}