API Documentation Generator
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
-
apidoc: command not found
cause The `apidoc` CLI tool is not installed globally on your system, or its installation path is not included in your system's PATH environment variable.fixInstall apidoc globally: `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: Node.js v14 is not supported. Please upgrade to v16 or higher.
cause You are attempting to run apidoc v1.0.0 or later with an unsupported Node.js version, which is now a hard requirement.fixUpgrade your Node.js version to 16 or higher. For example, using nvm: `nvm install 16 && nvm use 16`. -
TypeError: Cannot read properties of undefined (reading 'data') or 'project'
cause When using `createDoc` programmatically, the function returned `true` (indicating an error occurred) instead of the expected documentation object, often due to parsing issues or misconfiguration.fixAlways check if `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. -
The generated API documentation HTML bundle is empty or contains only boilerplate, despite source files having comments.
cause This can happen if input files are incorrectly filtered, if no valid `@api` comments are found, or in very old versions, due to a bug where the bundle wasn't written.fixDouble-check your `--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
const { createDoc } = require('apidoc')import { createDoc } from 'apidoc' - apidoc CLI
node apidoc -i src/ -o doc/
apidoc -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.');
}