1
---
2
name: api-versioning-strategy
3
description: >
4
Implement API versioning strategies including URL versioning, header
5
versioning, backward compatibility, deprecation strategies, and migration
6
guides. Use when dealing with API versions, deprecating endpoints, or managing
7
breaking changes.
8
---
9
10
# API Versioning Strategy
11
12
## Table of Contents
13
14
- [Overview](#overview)
15
- [When to Use](#when-to-use)
16
- [Quick Start](#quick-start)
17
- [Reference Guides](#reference-guides)
18
- [Best Practices](#best-practices)
19
20
## Overview
21
22
Comprehensive guide to API versioning approaches, deprecation strategies, backward compatibility techniques, and migration planning for REST APIs, GraphQL, and gRPC services.
23
24
## When to Use
25
26
- Designing new APIs with versioning from the start
27
- Adding breaking changes to existing APIs
28
- Deprecating old API versions
29
- Planning API migrations
30
- Ensuring backward compatibility
31
- Managing multiple API versions simultaneously
32
- Creating API documentation for different versions
33
- Implementing API version routing
34
35
## Quick Start
36
37
Minimal working example:
38
39
```typescript
40
// express-router.ts
41
import express from "express";
42
43
const app = express();
44
45
// Version 1
46
app.get("/api/v1/users", (req, res) => {
47
res.json({
48
users: [{ id: 1, name: "John Doe" }],
49
});
50
});
51
52
// Version 2 - Added email field
53
app.get("/api/v2/users", (req, res) => {
54
res.json({
55
users: [{ id: 1, name: "John Doe", email: "john@example.com" }],
56
});
57
});
58
59
// Shared logic with version-specific transformations
60
app.get("/api/:version/users/:id", async (req, res) => {
61
const user = await userService.findById(req.params.id);
62
63
if (req.params.version === "v1") {
64
res.json({ id: user.id, name: user.name });
65
// ... (see reference guides for full implementation)
66
```
67
68
## Reference Guides
69
70
Detailed implementations in the `references/` directory:
71
72
| Guide | Contents |
73
|---|---|
74
| [Versioning Approaches](references/versioning-approaches.md) | Versioning Approaches |
75
| [Backward Compatibility Patterns](references/backward-compatibility-patterns.md) | Backward Compatibility Patterns |
76
| [Deprecation Strategy](references/deprecation-strategy.md) | Deprecation Strategy |
77
| [Migration Guide Example](references/migration-guide-example.md) | Migration Guide Example |
78
| [Response Structure](references/response-structure.md) | Response Structure |
79
| [Date Format](references/date-format.md) | Date Format, Error Format |
80
| [JavaScript/TypeScript](references/javascripttypescript.md) | JavaScript/TypeScript, Python |
81
| [GraphQL Versioning](references/graphql-versioning.md) | GraphQL Versioning |
82
| [gRPC Versioning](references/grpc-versioning.md) | gRPC Versioning |
83
| [Version Detection & Routing](references/version-detection-routing.md) | Version Detection & Routing |
84
| [Testing Multiple Versions](references/testing-multiple-versions.md) | Testing Multiple Versions |
85
| [Pattern 1: Version-Agnostic Core](references/pattern-1-version-agnostic-core.md) | Pattern 1: Version-Agnostic Core, Pattern 2: Feature Flags for Gradual Rollout, Pattern 3: API Version Metrics |
86
87
## Best Practices
88
89
### ✅ DO
90
91
- Version from day one (even if v1)
92
- Document breaking vs non-breaking changes
93
- Provide clear migration guides with code examples
94
- Use semantic versioning principles
95
- Give 6-12 months deprecation notice
96
- Monitor usage of deprecated APIs
97
- Send deprecation warnings to API consumers
98
- Support at least 2 versions simultaneously
99
- Use adapters/transformers for version logic
100
- Test all supported versions
101
- Log which API version is being used
102
- Provide migration tooling when possible
103
- Be consistent with versioning approach
104
105
### ❌ DON'T
106
107
- Change API behavior without versioning
108
- Remove versions without notice
109
- Support too many versions (>3)
110
- Use different versioning strategies in same API
111
- Break APIs without incrementing version
112
- Forget to update documentation
113
- Deprecate too quickly (<6 months)
114
- Ignore feedback from API consumers
115
- Make every change a new version
116
- Use version numbers inconsistently
117