Part 6: API Documentation and Versioning

The Undocumented API Nightmare

Six months into my first microservices project, I went on vacation. When I returned, the team had integrated 3 new services with my user API. Each integration was different:

  • Frontend team: Sending userId in body

  • Mobile team: Sending user_id in body

  • Analytics team: Sending id in query params

Why? No documentation. They guessed. All three guesses were wrongβ€”I expected userId in URL params.

We spent 2 days fixing integrations. I spent the weekend writing API documentation.

Lesson learned: Undocumented APIs create more problems than writing documentation ever will.

This article shows how I document and version APIs now.

Swagger/OpenAPI

OpenAPI is the standard for REST API documentation. Swagger provides tools to generate and display it.

Setup

npm install swagger-jsdoc swagger-ui-express
npm install -D @types/swagger-jsdoc @types/swagger-ui-express

Swagger Configuration

Setup Swagger UI

Document Endpoints with JSDoc

Automated Documentation with TypeScript Decorators

For cleaner code, use swagger-autogen or typescript-openapi:

API Versioning Strategies

Pros: Clear, cache-friendly, easy routing Cons: URL changes with each version

Directory structure:

Example evolution:

2. Header Versioning

Client usage:

3. Query Parameter Versioning

Usage:

Migration Strategies

Dual-Write Pattern

Support both versions during transition:

Deprecation Headers

Changelog

Maintain a changelog to communicate changes:

Key Takeaways

  1. Document from day one using OpenAPI/Swagger

  2. Use URL path versioning for clarity

  3. Never remove endpoints without deprecation period

  4. Communicate changes via changelog and headers

  5. Support old versions for at least 6-12 months

  6. Test both versions in CI/CD

  7. Use semantic versioning (major.minor.patch)

  8. Breaking changes = new major version

  9. Mark deprecated endpoints with headers

  10. Automate documentation generation when possible

Next in series: Testing, Performance, and Production Deploymentβ€”ensuring your API works reliably at scale.


Good documentation saves time. Version carefully to avoid breaking client integrations.

Last updated