Notes
All notes

API Documentation Tools (API Design #8)

Your API launches and the support channel fills with one question, asked forty ways: “how do I authenticate?”, “what does cursor look like?”, “why do I get 422?” Every one of those tickets is a documentation failure — the knowledge existed, locked in the heads of the three people who built the thing, behind a one-question-at-a-time human queue.

Documentation is how the contract (Chapter 3) reaches the humans who integrate against it, and the modern toolchain makes one artifact — the OpenAPI spec — generate almost everything: reference docs, interactive consoles, client SDKs, mock servers, and tests. This chapter covers that toolchain: the spec itself, the docs platforms (Swagger UI, Readme.com, Stoplight), Postman as the exploration workbench, and the discipline that keeps docs from becoming fiction.

By the end you will be able to stand up a docs pipeline where the reference is generated, examples are tested, and “the docs are wrong” becomes a rare bug instead of the default state.

The spec is the source

Everything starts with an OpenAPI document describing paths, parameters, schemas, and errors. You saw a fragment in Chapter 3; here is the part that makes docs good — descriptions and examples written for the reader, not just types for the validator:

paths:
  /invoices:
    get:
      summary: List invoices
      description: |
        Returns invoices for the authenticated account, newest first,
        using cursor pagination. Pass `next_cursor` from the previous
        response to fetch the next page.
      parameters:
        - name: status
          in: query
          description: Filter by lifecycle state.
          schema: { type: string, enum: [draft, sent, paid], example: sent }
      responses:
        '200':
          description: A page of invoices.
          content:
            application/json:
              example:
                data: [{ id: inv_0009, status: sent, total: 1999 }]
                next_cursor: eyJsYXN0IjoiaW52XzAwMDkifQ==
                has_more: true

The description and example fields are where documentation actually lives. A schema says cursor is a string; the example shows a real one, and the description says what to do with it. Rule of thumb: if a support ticket was ever answered by explaining a field, that explanation belongs in the spec, where it answers the next thousand developers automatically.

One spec, many artifacts

The leverage of a machine-readable contract is that everything downstream generates from it:

\begin{tikzpicture}[font=\rmfamily, >=stealth,
  b/.style={draw, thick, rounded corners=4pt, minimum width=2.7cm, minimum height=0.72cm, align=center, font=\rmfamily\small},
  a/.style={->, thick}]
  \node[b, fill=orange!20] (s) at (0,0) {OpenAPI spec};
  \node[b, fill=blue!10] (d) at (-3.8,-1.5) {reference docs\\(Swagger UI)};
  \node[b, fill=green!10] (c) at (-1.3,-1.5) {client SDKs\\(codegen)};
  \node[b, fill=green!10] (m) at (1.3,-1.5) {mock server};
  \node[b, fill=gray!12] (t) at (3.8,-1.5) {contract tests\\(Chapter 12)};
  \draw[a] (s) -- (d); \draw[a] (s) -- (c); \draw[a] (s) -- (m); \draw[a] (s) -- (t);
\end{tikzpicture}

The economic argument is compounding: every hour spent making the spec accurate pays off in four artifacts simultaneously, and every correction propagates to all of them at once.

The tools, side by side:

Tool Slot Renders spec Standout feature
Swagger UI reference renderer yes the default; try-it console, self-hostable
Redoc / Scalar reference renderer yes cleaner reading experience, three-panel layout
Readme.com docs platform yes per-developer keys + request logs in the docs
Stoplight design + platform yes visual spec editor with style-guide linting
Postman exploration workbench import shareable collections, monitors, mock servers

The business case for the whole pipeline is arithmetic. If poor docs cost each integrating developer \(q\) extra support questions, each question costs \(c\) in support time (yours and theirs), and \(n\) developers integrate per year, docs quality is worth

\[ \text{savings} = n \cdot q \cdot c \]

— with \(n = 500\) integrations, \(q = 3\) questions, \(c = \\)40\( of loaded time, that's \\)60,000 a year, before counting the integrations that silently gave up instead of asking. Silent abandonment never files a ticket; it just doesn’t come back.

Docs platforms: what they add over a raw spec

Swagger UI on a static host covers the reference, but a public API usually wants a docs platform — and this is where Readme.com and Stoplight come in. What they add on top of rendering:

The choice is mundane — hosting, pricing, editor preference — but the decision to have a platform is not: reference docs that exist outside your product’s navigation, with no guides and no versioning, communicate “you’re on your own.”

The metric that summarizes all of this is time to first successful call: from landing on your docs to a 200 in the developer’s own terminal. Everything competes with it — signup friction, key issuance delay, missing examples, a try-it console that doesn’t pre-fill auth. The best docs platforms treat five minutes as the budget: read the auth guide, get a test key, fire the console’s example request, see real JSON come back. Every minute past that is developers evaluating your competitor’s API instead.

Postman: the exploration workbench

Postman occupies a different slot: not publishing docs for consumers, but giving developers a workbench to explore an API. Collections are ordered, shareable request folders — “Invoices: list, create, get, refund” — with environments holding variables like {{base_url}} and {{api_key}}. Around that core: mock servers (from collections rather than specs), monitors (scheduled runs that ping your API and alert on failures — a poor man’s Chapter 13), and test scripts (JavaScript assertions attached to requests).

For a public API, a published Postman collection is a genuine deliverable: developers fork it and have working requests in minutes. Internally, collections become the executable complement to the spec — the spec says what the API is, the collection proves what it does every night in CI.

Drift: how docs become fiction

Docs fail in one way: they stop matching the server. A field renamed in code, spec untouched; three months later a customer’s integration breaks on the documented behavior. The only durable fix is mechanical — make the docs pipeline refuse drift:

  1. Spec-first review — the spec changes in the same PR as the code; CI diffs the spec and flags undocumented changes.
  2. Tested examples — every example in the spec is replayed against a staging server in CI; a renamed field turns the docs test red.
  3. Generated, not hand-copied — reference pages render from the spec on every deploy; nobody edits HTML.
import requests, yaml

spec = yaml.safe_load(open("openapi.yaml"))
example = spec["paths"]["/invoices"]["get"]["responses"]["200"] \
              ["content"]["application/json"]["example"]

r = requests.get("https://staging.api.example/invoices",
                 headers={"Authorization": "Bearer test_key"})
body = r.json()
assert set(example) <= set(body), f"docs promise {set(example)}, server has {set(body)}"
assert set(example["data"][0]) <= set(body["data"][0]), "invoice fields drifted"

Twenty lines, run nightly, and “the docs lied” becomes a CI failure instead of a support escalation.

A last habit ties the chapter together: treat docs changes as product changes. A renamed field gets a changelog entry and a migration note the same day it merges; a new endpoint is undocumented until the spec PR lands — “code shipped, docs later” is how the drift debt starts compounding. Teams that hold this line find their support channel shifts from “how do I authenticate?” to questions that are genuinely new.

Pitfalls

QQuestionSelf-check

Name the four artifacts a maintained OpenAPI spec can generate, and the compounding benefit. Why does “tested examples” beat “careful editing” for keeping docs accurate? What’s the difference between what Postman collections and OpenAPI specs each prove about your API?

ASolution

Reference docs, client SDKs, mock servers, and contract tests — one hour improving the spec improves all four, and every fix propagates everywhere at once. Careful editing decays: humans forget, reviewers miss renames, and the first drifted field teaches developers to distrust everything else; replaying examples against staging in CI makes drift a failing build, which cannot be ignored silently. The spec declares what the API is (its contract, for validation and generation); a collection demonstrates what it does (executable requests with assertions) — you want the spec as the source of truth and the collection as the nightly proof.

Next: API Security (API Design #9) — the OWASP API Top 10, and why the worst API vulnerabilities are logic bugs, not crypto bugs.