Back to all terms
ClientAPIrequestresponse
APIintermediate

OpenAPI / Swagger Specification

A machine-readable YAML/JSON specification format for describing REST API endpoints, request/response schemas, authentication, and examples.

Also known as: OpenAPI Spec, Swagger, OAS, API Specification

Description

The OpenAPI Specification (OAS, formerly Swagger) is the industry standard for describing REST APIs in a machine-readable format. An OpenAPI document defines endpoints (paths and operations), request parameters and bodies, response schemas, authentication schemes, and example payloads using YAML or JSON. This single source of truth can be used to generate documentation, client SDKs, server stubs, mock servers, and validation middleware, ensuring consistency between the specification and the implementation.

OpenAPI 3.1 (the latest version) aligns its schema definition with JSON Schema 2020-12, enabling full JSON Schema compatibility. Key sections include info (API metadata), servers (environment URLs), paths (endpoints with operations), components (reusable schemas, parameters, responses, security schemes), and security (authentication requirements). Well-structured specs use $ref extensively to reference shared components, keeping the document DRY and maintainable.

The spec-first (design-first) approach writes the OpenAPI document before implementation and generates server stubs from it, ensuring the API contract is deliberate. The code-first approach generates the OpenAPI spec from annotated code (e.g., using tsoa, NestJS Swagger, or FastAPI). Both approaches benefit from CI-based spec validation using tools like Spectral for linting, oasdiff for breaking change detection, and Prism for contract testing against the spec.

Prompt Snippet

Maintain an OpenAPI 3.1 spec as the single source of truth in /docs/openapi.yaml. Use $ref to extract shared schemas into components/schemas (e.g., $ref: '#/components/schemas/Invoice'). Validate the spec in CI using Spectral with custom rules enforcing naming conventions, required error response schemas, and pagination patterns. Run oasdiff in CI to detect breaking changes on PRs targeting main. Generate TypeScript client types from the spec using openapi-typescript and publish them as an internal npm package. Use Prism as a mock server for frontend development against the spec.

Tags

openapiswaggerspecificationdocumentationtooling