Generating API Reference Documentation for a Library

A README gets people started; an API reference keeps them unblocked. Writing one by hand rarely survives the second release, so most maintainers generate it from the source. The tooling is mature in every major ecosystem. The hard part is deciding what belongs in it and stopping it from drifting.

Generators by ecosystem

Document the public surface, and only that

A generator documents whatever it can see. If internal helpers show up in the reference, users will reasonably assume they are supported. Before you worry about wording, make sure the generator's view matches your intended API:

  1. Point it at your public entry points, not the whole source tree. For TypeDoc that means listing the files named in your exports map.
  2. Use the language's privacy convention: unexported names in Go, a leading underscore or an __all__ list in Python, @internal tags with API Extractor.
  3. Fail the build if a public symbol has no doc comment. Most of these tools can warn on undocumented items; turn the warning into an error in CI.

The broader question of what should be public is covered in the API design outline.

What a good entry contains

For each public function or class, aim for: a one-sentence summary that starts with a verb; each parameter with its unit or allowed values; what it returns; what it throws or returns on failure; and a short example. A Python docstring in that shape:

def retry(fn, attempts=3, delay=0.5):
    """Call fn until it succeeds or attempts run out.

    Args:
        fn: Zero-argument callable to invoke.
        attempts: Maximum number of calls, at least 1.
        delay: Seconds to wait between calls.

    Returns:
        Whatever fn returns on its first successful call.

    Raises:
        The last exception raised by fn if every attempt fails.

    Example:
        >>> retry(lambda: 42)
        42
    """

The example line is also a doctest, which brings us to drift.

Keeping the reference honest

Finally, link the generated reference from your manifest's documentation or homepage field, as described in discoverability metadata.

Related pages