Skip to content

Lesson 8 of 9 · 22m

Documentation-Driven API Design

Write the contract before implementation, use OpenAPI as a machine-checkable source of truth, and generate the artifacts teams actually consume.

Documentation written after implementation is usually autobiography: it describes what the code happened to do that day. Documentation-driven design flips the order. You write the contract first, argue about it while changing it is cheap, then implement to satisfy it and test against it continuously.

OpenAPI is a contract, not decoration

An OpenAPI document can describe paths, parameters, schemas, security requirements, and errors precisely enough for validation and client generation.

openapi: 3.0.3
info:
  title: Billing API
  version: "1.4.0"
paths:
  /v1/invoices/{invoiceId}:
    get:
      operationId: showInvoice
      security: [{bearerAuth: []}]
      parameters:
        - {name: invoiceId, in: path, required: true, schema: {type: string, pattern: '^inv_[A-Za-z0-9]+$'}}
      responses:
        '200':
          description: Invoice found
          content: {application/json: {schema: {$ref: '#/components/schemas/Invoice'}}}
        '404':
          description: Invoice not found or not visible
          content: {application/problem+json: {schema: {$ref: '#/components/schemas/Problem'}}}

The useful parts are the boring parts: exact status codes, exact media types, examples, error schemas, and parameter constraints.

Review the contract like code

Before implementation, ask the questions implementation makes expensive:

  • Is the resource name stable, and can clients distinguish retryable failures from permanent ones?
  • Are nullable fields meaningful? Does pagination support the largest expected collection?
  • Which fields are sensitive, who may see them, and can mutation be idempotent?

A schema review catches design confusion before it becomes backwards-compatible forever.

Generate artifacts, but do not worship generation

OpenAPI can generate docs, TypeScript clients, PHP server stubs, validators, and mock servers. These are force multipliers, not substitutes for judgment. Generated clients are only as good as the schema precision. If every field is type: string and nullable, generation will happily produce a precisely documented mess.

Useful tooling in the PHP ecosystem includes validators such as league/openapi-psr7-validator, static analysis in CI, documentation renderers like Redoc or Stoplight Elements, and contract tests in PHPUnit or Pest.

Keep examples honest

Use realistic examples. A fake invoice for $10 that omits tax, currency, timestamps, and pagination teaches clients the wrong integration. Include examples for success, validation failure, unauthorized access, conflict, and rate limiting.

Documentation can be executable

The strongest pattern is contract testing: CI checks that implementation responses conform to the OpenAPI document. If the implementation adds an undocumented field or changes a type, the build fails. That makes documentation a living constraint instead of an archaeological artifact.

Field notes

  • Version the document with the API, and publish diffs. Clients care what changed, not just what exists now.
  • operationId values become generated method names. Pick stable, boring names.
  • Use examples with stable fake IDs like inv_9c8b7a, not copied production identifiers.
  • If docs and implementation disagree, treat it as a bug in both until you know which one is wrong.

Was this lesson helpful?

Sign in to rate