ASP.NET Core Web API Interview Question #99

What is contract-first versus code-first API development?

Routing, Versioning, OpenAPI/Swagger & API Documentation Mid-Level Intermediate

Quick Interview Answer

In code-first development, the application code and metadata are used to generate the API description. In contract-first development, an OpenAPI or similar contract is designed first and implementation is built to satisfy it.

Detailed Explanation

In code-first development, the application code and metadata are used to generate the API description. In contract-first development, an OpenAPI or similar contract is designed first and implementation is built to satisfy it.

Code-first fits naturally with many ASP.NET Core projects, while contract-first can be valuable when multiple teams need to agree on the interface before implementation. Both approaches require discipline to keep implementation and documentation synchronized.

Code Example

// Code-first:
// C# controller + DTOs -> generated OpenAPI

// Contract-first:
// openapi.yaml -> agreed contract -> implementation/client generation