liveBook · Manning

Code-first approach

As explained in The Design of Web APIs, Second Edition

A code-first approach is an API development method in which implementation code is authored before the API specification is generated or finalized.

A code-first approach generates or maintains an OpenAPI document from implementation code, typically beginning with controllers and data models rather than an independent specification. It can still support design-first work, but directly coding an API without design risks costly changes after consumption. section 6.2.1

Choose it by weighing its implementation alignment against its design and maintenance costs:

  • Keeps OpenAPI aligned with the implementation and can use convenient framework-native annotations. section 6.2.2
  • May expose implementation details, require early stack selection, or produce incomplete documentation when frameworks lack OpenAPI features. section 6.2.2
  • Requires monitoring because implementation changes can go unnoticed without an independent description of the expected API. section 6.2.2

Retro-documenting an existing API

Add an OpenAPI library to an existing implementation to generate a quick description of its operations and successful output data. Treat the result as useful but not necessarily exhaustive documentation. section 6.2.1

Figure 6.6 When deciding between specification-first and code-first approaches, it’s essential to check the pros and cons of each approach in the context in which you work.
figure

Compare specification-first and code-first approaches against the context, constraints, and capabilities of the API team. section 6.2.2

How The Design of Web APIs, Second Edition covers it

How deep it goes
Explains controllers, data models, annotations, monitoring, and trade-offs, but stops short of detailed generation configuration.
Examples
Uses controllers, data models, framework annotations, generated OpenAPI documents, and a /products resource path.
What it assumes
Assumes coding ability and an implementation stack, while distinguishing code-first from specification-first and design-first practices.
What you can do after
Evaluate when to generate OpenAPI from code, apply annotations or libraries, and recognize documentation and coupling risks.
How it connects
Connects OpenAPI, specification-first approach, design-first approach, API gateways, and API lifecycle documentation.

Also explained in

Compare how 4 books explain code-first approach →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.