OpenAPI (Swagger)
A machine-readable YAML/JSON specification for describing REST APIs that automates interactive docs, typed client SDK generation, mocks, and contract tests
Table of Contents
Structure Diagram
What is OpenAPI (Swagger)?
A machine-readable YAML/JSON specification for describing REST APIs that automates interactive docs, typed client SDK generation, mocks, and contract tests. It is a core concept in APIs & Networking within Web Dev.
Core Principles
The OpenAPI Specification (formerly Swagger) is a language-agnostic YAML/JSON standard for describing REST APIs—endpoints, HTTP methods, parameters, JSON Schema request/response bodies, status codes, and security schemes.
Because it is machine-readable, a single openapi.yaml acts as a Single Source of Truth powering interactive documentation (Swagger UI, Scalar, Redoc), auto-generated TypeScript/Swift/Kotlin clients, mock servers, and request validation.
Practical Takeaways
- Choose Schema-First or Code-First intentionally: Use Schema-First when external partners or separate FE/BE teams must agree on contracts before coding; use Code-First (FastAPI, NestJS, Zod-to-OpenAPI) in fast-moving monorepos so docs never drift from implementation.
- Gate breaking changes in CI: Run tools like
oasdiffon pull requests to catch removed fields or narrowed types before they break mobile clients. - Automate frontend types: Pair
openapi-typescriptororvalwith your OpenAPI spec to eliminate hand-written API interfaces.
Notable Examples
- FastAPI, NestJS, and SpringDoc: Generate live OpenAPI 3.1 schemas and interactive playgrounds straight from route definitions.
- Stripe and OpenAI developer portals: Drive multi-language SDK generation and reference docs from centralized OpenAPI specs.