LearnThatStack Ace your next interview

System Design Concepts · API Design

Documentation: the docs come from the spec, not a hand-written copy

Hand-written docs are a second copy of what the code does, and copies go out of date. A spec that generates the reference, clients and checks cannot go out of date.

documentationTwo copies, and only one runsthe page nobody checkswrong on the page3checks that read it0unchanged. still nonewhat runs/v1/users/{id}v1.5iduuidemailstringnamestringcreated_attimestamprenamedavatar_urlurlremoveddisplay_namestringnewwhat they readtyped in by handdocs.acme.com/usersstill v1.4iduuidemailstringnamestringcreatedtimestampwrong nameavatar_urlurlstill listeddisplay_namenot listedwhat runs on that commitunit412 passintegration38 passdeployv1.5 is livethe reference pagenothing runs itsomeone else's codeacme-checkoutwritten on day 9every line of it came off the pagePATCH /v1/users/8842avatar_url: ...400 unknown field: avatar_urlGET /v1/users/8842read user.createdundefinedno error at allit reaches you as ticket 4471one call failed loudly. the other returned undefined and shipped
documentationTwo copies, and only one runswrong on the page3checks that read it0still nonewhat runs/v1/users/{id}v1.5emailcreated_atrenamedavatar_urlremoveddisplay_namenewwhat they readtyped in by handdocs.acme.com/usersstill v1.4emailcreatedwrong nameavatar_urlstill listeddisplay_namenot listed3 checks passthe page, no checksomeone else's codeacme-checkoutwritten on day 9PATCH avatar_url400 unknown fieldGET user.createdundefined

Two copies, and only one runs

  1. The reference page lists the same fields as the handler, but only the handler runs. The page was typed in by hand from the handler's code, and nothing links the two.
  2. Release v1.5 renames created to created_at, removes avatar_url and adds display_name in the handler. Nothing links the handler to the hand-written reference page, so the page does not change and is now wrong in three places.
  3. The pipeline runs on the v1.5 commit that made the page wrong, and all three checks pass. But none of the three checks reads the reference page, and no fourth check does, so the pipeline cannot see that the page is wrong.
  4. Nine days later, a partner writes their integration by reading the out-of-date reference page. So they send avatar_url, read created, and never learn that display_name exists.
  5. The partner's write gets a 400 for unknown field avatar_url, but reading created returns undefined without an error, so that code ships. The API team's bug costs the partner an afternoon, and the team finds out from a support ticket, not a failing build.

© LearnThatStack - diagrams may not be republished without permission.

The same API shape is written down twice, but only the copy in the handler code runs. A person typed the reference page from that code some time ago. Nothing links the reference page to the handler. Every documentation problem you have ever had starts with these two unlinked copies.

No line of code refers to the hand-written docs page. So release v1.5 changes only the handler. The handler renames the field created to created_at, removes avatar_url, and adds display_name. The docs page stays the same. So the page is now wrong in three places: one field renamed, one field removed, and one new field it never mentions.

The commit that made the docs page wrong passes the unit tests and the integration tests. The deploy then ships v1.5 on that same commit. There is no fourth check for the docs page, so zero checks read it. Nothing you own reads that page.

A partner has only the hand-written docs page, so the partner trusts it. Nine days later, the partner's integration sends avatar_url and reads the created field. The partner never learns that display_name exists. Now someone who does not work for you is doing your testing.

Two calls fail in two different ways, and the quiet failure is worse. The write gets a 400 response that names the field, so the bug gets fixed within an hour. The read returns undefined with no error, so the code that makes that call ships. Both failures reach you as a support ticket, the slowest way you have to find a bug.

There are two ways to get one OpenAPI file. Write the file first and generate the handlers from it, or annotate the handlers and generate the file from them. Both ways produce the same file: 14 paths and 9 schemas, with 22 examples and 11 error codes. Five outputs could come from that file, but today a person types every one of them.

The reference page is now an output of the spec file, so nobody types the page any more. Every path, field and example on the page comes from that file, so the page always matches the code. The difference from chapter one is in how the page is built. The page does not depend on anyone remembering to update it.

In chapter one, a renamed field broke the partner's integration. Here the same spec file generates client libraries in four languages. Nobody writes those libraries by hand. So a field renamed in the spec changes in all four libraries on the next release. A build step now handles the exact change that broke the partner's integration.

The examples in the spec file are enough for a mock server to answer requests. The mock server replies before the real endpoint exists. So your partner waits zero days and starts building on day one. The examples in a spec are also worth writing properly, because a server is going to return them.

At the edge, the schema refuses any request with a field the schema does not define. The schema returns a 400 that names that field. So the spec file has a new role: the document that describes the API now enforces the API. A document you can run cannot be wrong without anyone noticing.

Drift, where the code no longer matches the spec, now fails the build. In CI, contract tests use the spec file as the test fixture and check every response against the matching schema. So a handler that drops a field fails the build. Chapter one's bug would have failed the build on your machine the day the bug shipped, not nine days later in a stranger's ticket.

Generation from the spec stops here, and this is the honest half of the answer. People write why to call this endpoint and how auth works end to end. They also write what to do about each error, and migration notes for a breaking change. An integrator reads these four documents first. Generation removes drift, but it does not write the explanation.

Time to the first working call is a number you can measure and use as a design target. The measurement starts when a partner opens your docs. The partner then has to get past five gates, steps that block the call until each one is done. On most APIs, the first gate is a form and a call back from sales.

A key the partner can make on their own saves three days at the first gate, getting access. The partner signs up, creates a sandbox key on the page and pastes it in, all in two minutes. The key is scoped, so it cannot touch real money. A key that needs a sales call costs those three days before anyone writes a line of code.

An example that runs is worth more than a page that describes one. At gate two, the request runs exactly as the partner pasted it, with real values and the partner's own key already in it. The answer is printed beside the request, and the whole step takes two minutes. Building the same call from prose spread over four pages takes most of a day, and the call comes out wrong.

Gate three, the error response, is the one interviewers are listening for. The partner's first call fails with a 422 that carries a code the partner can look up. The error also names the field, the broken rule and what to send instead, so the fix takes three minutes, with no support ticket. The error you return is documentation, and integrators read it more closely than any other page.

Gate four is a sandbox, a safe place for the partner to be wrong. The sandbox holds seeded accounts (test accounts set up in advance) and fake money. So the partner can send twenty bad requests before lunch and has nothing to apologise for. Without a sandbox, the partner has two choices: test against production, or wait two days for someone to make them an account.

Gate five is not about the first call at all, but about turning one working call into a system somebody will build a business on. That system needs published rate limits, a changelog and a deprecation policy with dates. The same system also needs signed webhooks and idempotency keys, so a retry cannot charge twice. After twenty minutes, the partner is live.

A hand-written page is a second copy that nothing in your pipeline reads. So the page goes wrong without any warning. The fix is one OpenAPI file that generates the reference, client libraries and mock server. Request validation and contract tests come from the same file, so drift between code and spec fails a build. People still write the guides, error remedies and migration notes. For third-party integrations, name your design target: the time from opening your docs to one working call.

In interviews · 3 questions

Related Questions

Also helps with

← All concepts