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
- TypeScript and JavaScript: TypeDoc reads TypeScript types and TSDoc or JSDoc comments. Microsoft's API Extractor goes further, producing an API report file you can diff in review, alongside documentation input.
- Python: Sphinx with the autodoc extension is the long-established route; mkdocstrings does the same for MkDocs sites. Both read docstrings and, with the right settings, type hints.
- Go: nothing to configure. pkg.go.dev renders documentation from the doc comments on exported identifiers once the module is fetched through the proxy, and
go docshows the same thing locally. - Rust:
cargo docbuilds rustdoc output locally, and docs.rs builds and hosts it automatically for crates published to crates.io.
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:
- Point it at your public entry points, not the whole source tree. For TypeDoc that means listing the files named in your
exportsmap. - Use the language's privacy convention: unexported names in Go, a leading underscore or an
__all__list in Python,@internaltags with API Extractor. - 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
- Run the examples. Python doctests, Rust doc tests (run by
cargo test) and GoExamplefunctions (run bygo test) all execute documentation code, so a broken example fails CI. - Build docs on every pull request. A broken cross-reference is easier to fix in the change that caused it.
- Publish per version. Users on an older major need the reference for that major. docs.rs and pkg.go.dev do this automatically; for self-hosted docs, keep a version switcher.
- Mark deprecations in the docs, not only at runtime.
@deprecatedin TSDoc and aDeprecated:paragraph in Go doc comments are both understood by tooling; see writing deprecation warnings for the message itself.
Finally, link the generated reference from your manifest's documentation or homepage field, as described in discoverability metadata.