# API Contracts with OpenAPI

> Describe your HTTP API in an OpenAPI document to get docs, validation, mocks and client generation. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/openapi-docs
Last updated: 2026-11-28

Describe your HTTP API in an OpenAPI document to get docs, validation, mocks and client generation.

Intermediate · about 4 hours · difficulty 2/5

Learn first: [Resource-Oriented REST APIs](https://www.internhack.in/roadmaps/tracks/backend/rest-api-design)

The **OpenAPI Specification** (formerly Swagger) is a machine-readable description of an HTTP API in YAML or JSON: paths, parameters, request bodies, responses, schemas and authentication. It turns your API into a contract that both humans and tools can read.

### What you get from it

- Interactive documentation (Swagger UI, Redoc, Scalar) that stays in sync with the code.
- Request and response validation middleware.
- Generated client SDKs and server stubs for many languages.
- Mock servers so frontend work can start before the backend is ready.
- Contract tests and breaking-change detection in CI.

### Code-first vs design-first

- **Code-first**: annotate routes or schemas and generate the spec (FastAPI, NestJS, Fastify schemas, springdoc). Fast to start.
- **Design-first**: write the spec, review it with consumers, then implement. Better for public APIs with many clients.

Either way, publish the spec, version it with the code and check it in CI.

### Pitfalls

- A spec that drifts from reality because it is maintained by hand and nobody tests it.
- Documenting only the happy path; document error responses too.

You are ready to move on when your API serves an OpenAPI document with request and error schemas, and you have generated a working client from it.

## Key concepts

- **Paths, operations and schemas**
- **Swagger UI, Redoc, Scalar**
- **Code-first generation**
- **Design-first workflow**
- **Client SDK generation**
- **Breaking-change checks in CI**

## Mini project

Add an OpenAPI document to your REST API, serve interactive docs at /docs, and generate a TypeScript or Python client that a small script uses to call two endpoints.

## Check yourself

What are the risks of maintaining an OpenAPI file by hand, and how can CI reduce them?

## Free resources

- [OpenAPI Initiative](https://www.openapis.org/) (OpenAPI Initiative)
- [OpenAPI Specification (latest)](https://spec.openapis.org/oas/latest.html) (OpenAPI Initiative)
- [Swagger documentation](https://swagger.io/docs/) (SmartBear)

Previous: [Pagination, Versioning and Rate Limits](https://www.internhack.in/roadmaps/tracks/backend/api-evolution-and-limits) · Next: [GraphQL APIs](https://www.internhack.in/roadmaps/tracks/backend/graphql)

## More in Designing and Documenting APIs

- [Resource-Oriented REST APIs](https://www.internhack.in/roadmaps/tracks/backend/rest-api-design)
- [Pagination, Versioning and Rate Limits](https://www.internhack.in/roadmaps/tracks/backend/api-evolution-and-limits)
- [API Contracts with OpenAPI](https://www.internhack.in/roadmaps/tracks/backend/openapi-docs)
- [GraphQL APIs](https://www.internhack.in/roadmaps/tracks/backend/graphql)
- [gRPC and Protocol Buffers](https://www.internhack.in/roadmaps/tracks/backend/grpc)
- [SOAP and Legacy Integrations](https://www.internhack.in/roadmaps/tracks/backend/soap-legacy-apis)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
