One of the biggest questions you need to answer when starting an OpenAPI project is:
Where does the OpenAPI definition live?
Or, as we used to say back in the mid-2000s:
“Where is your authoritative source?”
This question is critical because your entire API ecosystem depends on it.
When I first started using OpenAPI (then called Swagger), I did what many developers still do today:
- Set up Swashbuckle in an ASP.NET Core project.
- Let it automatically generate
swagger.json
based on controllers. - Use NSwag Studio to generate client proxy classes for consuming the API.
For internal APIs with a technical audience, this is fast, simple, and lean.
πΉ Change an API controller?
β‘οΈ Swagger updates automatically.
πΉ Using CI/CD scripts to generate client proxies
β‘οΈ If there’s a breaking API change, depending on the change something - somewhere will likely fail to build- so you know you broke something
This approach is great for sensing change, but is it the best long-term strategy?
π¨ The API Design Dilemma: Code-First vs. Design-First
When building APIs, you have two approaches:
1οΈβ£ Code-First (Generate OpenAPI from Code)
β
Use tools like Swashbuckle (C#), FastAPI (Python), or SpringDoc (Java) to generate OpenAPI from controllers.
β
API changes automatically reflect in OpenAPI docs.
β
Fast and lightweight for internal teams.
β API design is tightly coupled to the implementation.
β No clear API review process before implementation.
β Can result in inconsistent API design over time.
2οΈβ£ Design-First (OpenAPI Defines the Code)
β
Use visual tools like Stoplight Studio, Swagger Editor, or Postman to design APIs.
β
The OpenAPI spec becomes the single source of truth.
β
Code is generated from OpenAPI rather than the other way around.
β
Encourages API governance, consistency, and review before implementation.
β Requires discipline to keep code in sync.
β More upfront effort.
π Key Difference?
- Code-First = “Write Code, Docs Follow”
- Design-First = “Write API Spec, Code Follows”
π Tools for API Design-First Workflows
Tool | Purpose | Pros |
---|---|---|
Stoplight Studio | Visual API editor | No YAML needed, built-in mocking |
Swagger Editor | OpenAPI editor in the browser | Simple, fast, free |
Postman | API design & testing | Great for team collaboration |
Apicurio | API governance & lifecycle management | Ideal for large teams |
With these tools, API design comes first.
Developers then use the OpenAPI definition to generate code stubs instead of letting the code generate OpenAPI.
π Code-First vs. Design-First: Pros & Cons
Feature | Code-First (Swashbuckle, NSwag) | Design-First (Stoplight, Swagger Editor) |
---|---|---|
Speed | β Fast setup | β οΈ Slower initial setup |
Documentation | β οΈ Auto-generated, but not always user-friendly | β Structured, well-documented APIs |
Consistency | β Can be inconsistent between teams | β Encourages standardization |
Collaboration | β Harder to collaborate before implementation | β API design is reviewed before coding |
Breaking Changes | β CI/CD detects breaking changes | β Manual validation required |
Long-Term Maintainability | β οΈ Can get messy over time | β More scalable |
π Code-First for Internal APIs, Design-First for Public APIs
So which approach should you use?
πΉ For internal, tech-focused APIs?
β‘οΈ Code-first is fast and good enough (Swashbuckle + NSwag).
πΉ For public, customer-facing APIs?
β‘οΈ Design-first ensures consistency, better documentation, and API governance.
Hybrid Approach?
You can start with Code-First and transition to Design-First as your API grows.
π₯ How to Implement Design-First API Development
If youβre moving toward Design-First, hereβs a solid workflow:
1οΈβ£ Design the API First
Use Stoplight Studio or Swagger Editor to draft the API before coding.
2οΈβ£ Generate Code Stubs
Use tools like NSwag, OpenAPI Generator, or AutoRest to generate controllers/models.
Example: Generate C# API controllers:
|
|
3οΈβ£ Keep API & Code in Sync
- Validate API updates using Spectral Linting:
1
spectral lint openapi.yaml
- Use contract tests (like Pact) to verify API behavior.
4οΈβ£ Automate API Documentation
Deploy OpenAPI docs with ReDoc or Swagger UI:
|
|
π₯ Conclusion: Pick Your Source of Truth Wisely
π If code generates OpenAPI (Code-First), your API evolves organically but may lack consistency.
π If OpenAPI defines the code (Design-First), your API is structured and governed, but requires discipline.
For small, internal APIs, stick with Swashbuckle + NSwag.
For public APIs, shift to Design-First with Stoplight or Swagger Editor.
At the end of the day, the most important thing is to choose your authoritative source earlyβbecause fixing API design later is painful. π
π Key Takeaways
Summary | Details |
---|---|
What is the “Single Source of Truth”? | The authoritative place where your API is defined. |
Code-First Approach? | API is defined by implementation, OpenAPI is auto-generated. |
Design-First Approach? | OpenAPI is written first, then code is generated from it. |
Best for Internal APIs? | Code-First (Swashbuckle + NSwag). |
Best for Public APIs? | Design-First (Stoplight, Swagger Editor, Postman). |
How to switch to Design-First? | Use OpenAPI design tools, generate stubs, and automate validation. |
|
|