API / Swagger Interview questions
Why is contract-first API design preferred over code-first in some teams?
Contract-first means writing the OpenAPI specification before writing any implementation code, then generating server stubs and client SDKs from that agreed-upon contract; code-first means writing the implementation first and generating the OpenAPI document from annotations on that code afterward.
- Early alignment: frontend, backend, and third-party consumer teams can review and agree on the API shape before any implementation exists, catching design problems while they're still cheap to change.
- Parallel development: once the contract is agreed, frontend and backend teams can work simultaneously against mocks generated from the spec, rather than the frontend waiting on a working backend.
- Language independence: the same contract can drive server stub generation for any backend language, useful if implementation language isn't fixed yet.
- Deliberate design: the API's shape is a first-class design artifact reviewed on its own merits, rather than an incidental byproduct of whatever internal code structure happened to produce it.
The trade-off is added upfront process: contract-first requires genuine design discipline and stakeholder review before coding starts, which some teams find slows down early-stage, rapidly-changing projects where code-first's "annotate as you build" flow feels lighter weight.
More Related questions...