<< back to Guides

🚦 API Versioning Crash Course

API versioning is essential for evolving an API without breaking existing clients. This crash course covers why versioning matters, strategies, and best practices.


πŸ“Œ Why API Versioning?

APIs evolve. Changes can break existing users if not managed correctly. Versioning provides a contract between providers and consumers:


🚩 When Do You Need Versioning?

βœ… When introducing:

❌ Not always necessary for:


🎯 Common API Versioning Strategies

Here are the most common ways to version your APIs, each with pros and cons:


1. URI Path Versioning

Put the version directly in the URL path.

// Example
GET /v1/users/123
GET /v2/users/123

βœ… Pros:

❌ Cons:


2. Query Parameter Versioning

Specify the version via a query string.

// Example
GET /users/123?version=2

βœ… Pros:

❌ Cons:


3. Header Versioning

Send version info via custom headers.

// Example
GET /users/123
Headers:
  Accept: application/vnd.myapp.v2+json

βœ… Pros:

❌ Cons:


4. Content Negotiation (Media Type Versioning)

Use MIME types in the Accept header.

// Example
Accept: application/vnd.example.v1+json

βœ… Pros:

❌ Cons:


🧠 Best Practices


πŸ” Versioning Approaches in REST vs GraphQL

Style Versioning Style Notes
REST URI, header, or param Most popular styles use URI paths
GraphQL Schema evolution / field deprecation Avoids versioning whole schema

GraphQL typically avoids versioning entire schemas by:


🧰 Tooling & Support


βœ… TL;DR Cheat Sheet

Strategy URL Example Good For Drawbacks
URI Path /v1/resource Simplicity, public APIs Clutters endpoint hierarchy
Query Param /resource?version=1 Quick tests Caching issues
Header Accept: vnd.app.v1+json Clean URL, advanced users Harder to debug/document
GraphQL (no ver) schema.graphql + @deprecated Evolution without breaking Requires planning

πŸ“š Further Reading


<< back to Guides