<< back to Guides
<< 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:
- π Preserve backward compatibility
- π Enable iterative improvements
- π§ͺ Test new features without breaking production
π© When Do You Need Versioning?
β When introducing:
- Breaking changes to response structure
- Removed or renamed fields
- Behavior modifications (e.g., different sorting, pagination)
- Deprecation of endpoints or functionality
β Not always necessary for:
- Internal APIs
- Non-breaking changes (e.g., adding optional fields)
π― 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:
- Clear and explicit
- Easy to cache and route
β Cons:
- Breaks RESTful URL purity
- Requires duplicate documentation/routes
2. Query Parameter Versioning
Specify the version via a query string.
// Example
GET /users/123?version=2
β Pros:
- Non-invasive URL change
- Useful for quick testing or experimentation
β Cons:
- Harder to cache
- Less common in public APIs
3. Header Versioning
Send version info via custom headers.
// Example
GET /users/123
Headers:
Accept: application/vnd.myapp.v2+json
β Pros:
- Clean URLs
- RESTful principles preserved
β Cons:
- Hidden from browsers and some tools (like cURL)
- Harder to debug or document
4. Content Negotiation (Media Type Versioning)
Use MIME types in the Accept
header.
// Example
Accept: application/vnd.example.v1+json
β Pros:
- Fine-grained control
- Works well for APIs with multiple formats
β Cons:
- Requires custom logic on server
- Not always intuitive for consumers
π§ Best Practices
- π’ Communicate changes clearly in changelogs and documentation.
- π¨ Deprecate gradually: support multiple versions with sunset notices.
- βοΈ Keep old versions stable and free of breaking changes.
- π¦ Group related changes into a new version; donβt version every minor tweak.
- β Default to latest only if safe, or require versioning explicitly.
- π§ͺ Use automated tests to ensure old versions still function as expected.
π 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:
- Adding new fields instead of replacing
- Deprecating fields with
@deprecated
π§° Tooling & Support
- OpenAPI (Swagger) supports version tagging and documentation per version.
- Postman supports versioned collections.
- API Gateways (e.g., Kong, AWS API Gateway) allow version-based routing.
β 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
- REST API Versioning Best Practices β Microsoft
- Google Cloud API Design Guide
- API Evolution in GraphQL
<< back to Guides