简介
开源社区出品的 API 版本控制指南,提供 4 种版本策略与弃用迁移方案,帮助团队实现 API 平滑演进与生命周期管理。
原始描述
API versioning strategies — URL path, header, query param, content negotiation — with breaking change classification, deprecation timelines, migration patterns, and multi-version support. Use when evolving APIs, planning breaking changes, or managing version lifecycles.
通过你的 Agent 添加
安装目录:api-versioning/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
中文使用说明 来自收录源
核心用法
本 Skill 是一个纯文档型(T-MD)的 API 版本管理参考指南,系统性地阐述了 API 演进的完整生命周期管理。它提供了四种主流版本管理策略(URL 路径、查询参数、请求头、内容协商)的详细对比与实施示例,定义了重大变更与非重大变更的明确边界,并给出了包含公告、日落期和移除三阶段的废弃流程。文档还涵盖了适配器模式、外观模式等迁移模式,以及多版本并发支持的架构原则。
显著优点
- 体系化知识:完整覆盖了从版本策略选择、语义化版本应用到客户端通信、SDK 版本对齐的全流程,提供了可操作的实践清单。
- 工程化指导:包含可直接参考的代码示例(Python/FastAPI、JavaScript/Express)和反模式警告,易于集成到实际开发流程中。
- 低风险:作为零依赖、零可执行代码的纯文档,不会引入任何依赖项漏洞或运行时性能隐患。
- 安全意识强:强调了不破坏现有消费者、渐进废弃和使用 RFC 8594
Sunset Header 等现代 Web 最佳实践。
潜在缺点或局限性
- 纯文档无自动化:作为静态参考文档,不提供自动化的版本检查、代码生成或 CI 集成工具,所有实践均需开发者手动实施。
- T3 来源可信度有限:作者
wpank 为个人开发者,缺乏大型科技公司或权威标准组织的背书,内容的全面性和准确性未经过广泛的第三方验证。
- 缺少许可证声明:未包含开源许可证文件,可能限制在企业或合规要求严格的环境中的采纳与分发。
- 示例未测试:所有代码块均为教学示例,可能未涵盖复杂的生产环境边界情况。
适合的目标群体
- 后端/API 架构师:负责设计或重构 API 版本策略的技术决策者。
- 全栈开发者:需要理解何时以及如何安全地推出 API 新版本的一线开发人员。
- API 产品经理:需要把握版本兼容性对第三方消费者影响的产品或平台运营者。
- DevOps 工程师:负责通过 API 网关实现版本路由和废弃版本服务下架的基础设施人员。
使用风险
- 内容准确性与时效性风险:来自个人维护的 T3 来源,文档可能未及时随技术趋势更新,或包含主观性较强的观点。使用前建议与其他权威源(如 Microsoft REST API Guidelines、Google API Improvement Proposals)交叉验证。
- 实施复杂性风险:文档中提出的多版本支持、适配器模式等架构,若在小型项目中过度实施,可能造成不必要的工程开销与维护负担。
- 无运行时风险:由于 Skill 本身是纯文档,不存在注入攻击、恶意依赖、数据泄露等直接安全风险。唯一的合规风险是缺失许可证,可能导致后续的法律用途不明确。
Sunset Header 等现代 Web 最佳实践。- 纯文档无自动化:作为静态参考文档,不提供自动化的版本检查、代码生成或 CI 集成工具,所有实践均需开发者手动实施。
- T3 来源可信度有限:作者
wpank为个人开发者,缺乏大型科技公司或权威标准组织的背书,内容的全面性和准确性未经过广泛的第三方验证。 - 缺少许可证声明:未包含开源许可证文件,可能限制在企业或合规要求严格的环境中的采纳与分发。
- 示例未测试:所有代码块均为教学示例,可能未涵盖复杂的生产环境边界情况。
适合的目标群体
- 后端/API 架构师:负责设计或重构 API 版本策略的技术决策者。
- 全栈开发者:需要理解何时以及如何安全地推出 API 新版本的一线开发人员。
- API 产品经理:需要把握版本兼容性对第三方消费者影响的产品或平台运营者。
- DevOps 工程师:负责通过 API 网关实现版本路由和废弃版本服务下架的基础设施人员。
使用风险
- 内容准确性与时效性风险:来自个人维护的 T3 来源,文档可能未及时随技术趋势更新,或包含主观性较强的观点。使用前建议与其他权威源(如 Microsoft REST API Guidelines、Google API Improvement Proposals)交叉验证。
- 实施复杂性风险:文档中提出的多版本支持、适配器模式等架构,若在小型项目中过度实施,可能造成不必要的工程开销与维护负担。
- 无运行时风险:由于 Skill 本身是纯文档,不存在注入攻击、恶意依赖、数据泄露等直接安全风险。唯一的合规风险是缺失许可证,可能导致后续的法律用途不明确。
- 内容准确性与时效性风险:来自个人维护的 T3 来源,文档可能未及时随技术趋势更新,或包含主观性较强的观点。使用前建议与其他权威源(如 Microsoft REST API Guidelines、Google API Improvement Proposals)交叉验证。
- 实施复杂性风险:文档中提出的多版本支持、适配器模式等架构,若在小型项目中过度实施,可能造成不必要的工程开销与维护负担。
- 无运行时风险:由于 Skill 本身是纯文档,不存在注入攻击、恶意依赖、数据泄露等直接安全风险。唯一的合规风险是缺失许可证,可能导致后续的法律用途不明确。
安全性检查报告将此 Skill 评为 A 级(T3 来源上限),安全分 97 分,确认其无任何恶意行为或敏感信息泄露。建议在使用前由数据负责人进行一次人工审查以确认内容与团队实践匹配,并补充开源许可证。
技能正文
name: api-versioning
description: >-
Version REST and GraphQL APIs. Use when a user asks to version an API,
handle breaking changes, implement API deprecation, manage multiple API
versions, or design an API evolution strategy.
license: Apache-2.0
compatibility: 'Any language/framework'
metadata:
author: terminal-skills
version: 1.1.0
category: development
tags: "api, versioning, rest, deprecation, backwards-compatibility"
API Versioning
Overview
Overview
APIs evolve, but breaking changes break clients. This skill covers versioning strategies (URL path, headers, query params), deprecation workflows, backwards-compatible changes, and migration patterns for REST and GraphQL APIs.
Instructions
Step 1: URL Path Versioning (Recommended)
// routes/v1/projects.ts — Version 1 routes
import { Router } from 'express'
const v1Router = Router()
v1Router.get('/projects', async (req, res) => {
const projects = await db.project.findMany()
// V1 returns flat array
res.json(projects)
})
// routes/v2/projects.ts — Version 2 with pagination
const v2Router = Router()
v2Router.get('/projects', async (req, res) => {
const { cursor, limit = 20 } = req.query
const projects = await db.project.findMany({
take: Number(limit) + 1,
orderBy: { id: 'asc' }, // cursor paging needs a stable order
...(cursor ? { cursor: { id: String(cursor) }, skip: 1 } : {}), // skip the cursor row itself
})
const hasMore = projects.length > Number(limit)
if (hasMore) projects.pop()
// V2 returns paginated envelope
res.json({
data: projects,
pagination: {
nextCursor: hasMore ? projects[projects.length - 1].id : null,
hasMore,
},
})
})
// app.ts — Mount versions
app.use('/v1', v1Router)
app.use('/v2', v2Router)
Step 2: Backwards-Compatible Changes
These changes are SAFE (no version bump needed):
- Adding new optional fields to responses
- Adding new endpoints
- Adding new optional query parameters
- Adding new enum values (if clients handle unknown values)
These changes REQUIRE a new version:
- Removing or renaming fields
- Changing field types
- Making optional fields required
- Changing response structure (array → object)
- Changing authentication scheme
// Adding a field is backwards-compatible
// V1 response: { id, name, status }
// V1.1 response: { id, name, status, taskCount } ← safe, old clients ignore new field
// Changing structure is BREAKING
// V1 response: [{ id, name }]
// V2 response: { data: [{ id, name }], pagination: {} } ← new version required
Step 3: Deprecation Headers
// middleware/deprecation.ts — Warn clients about deprecated versions
import type { Request, Response, NextFunction } from 'express'
// Deprecation (RFC 9745): "@" + Unix seconds of the date it was deprecated
// Sunset (RFC 8594): HTTP-date after which the endpoint may stop working
export function deprecationMiddleware(version: string, deprecatedAtUnix: number, sunsetDate: string) {
return (req: Request, res: Response, next: NextFunction) => {
res.set('Deprecation', `@${deprecatedAtUnix}`)
res.set('Sunset', sunsetDate)
res.set('Link', [
`<https://api.shopline.dev/docs/migrate-v1-to-v2>; rel="deprecation"; type="text/html"`,
`</v2${req.path}>; rel="successor-version"`,
].join(', '))
console.log(`[DEPRECATION] ${req.method} /v${version}${req.path} from ${req.ip}`)
next()
}
}
// v1 deprecated 2026-09-01 00:00 UTC, sunset 2027-03-01
app.use('/v1', deprecationMiddleware('1', 1788220800, 'Mon, 01 Mar 2027 00:00:00 GMT'), v1Router)
Step 4: API Changelog
# API Changelog
## v2.0.0 (2026-09-01)
### Breaking Changes
- `GET /projects` now returns paginated response `{ data: [], pagination: {} }`
- Removed `GET /projects/all` (use pagination instead)
### Migration Guide
- Update response parsing to read `response.data` instead of `response` directly
- Implement cursor-based pagination for large datasets
- v1 sunset date: March 1, 2027
## v1.3.0 (2026-08-15)
### Added
- `taskCount` field in project responses
- `GET /projects/{id}/activity` endpoint
Step 5: GraphQL
// middleware/deprecation.ts — Warn clients about deprecated versions
import type { Request, Response, NextFunction } from 'express'
// Deprecation (RFC 9745): "@" + Unix seconds of the date it was deprecated
// Sunset (RFC 8594): HTTP-date after which the endpoint may stop working
export function deprecationMiddleware(version: string, deprecatedAtUnix: number, sunsetDate: string) {
return (req: Request, res: Response, next: NextFunction) => {
res.set('Deprecation', `@${deprecatedAtUnix}`)
res.set('Sunset', sunsetDate)
res.set('Link', [
`<https://api.shopline.dev/docs/migrate-v1-to-v2>; rel="deprecation"; type="text/html"`,
`</v2${req.path}>; rel="successor-version"`,
].join(', '))
console.log(`[DEPRECATION] ${req.method} /v${version}${req.path} from ${req.ip}`)
next()
}
}
// v1 deprecated 2026-09-01 00:00 UTC, sunset 2027-03-01
app.use('/v1', deprecationMiddleware('1', 1788220800, 'Mon, 01 Mar 2027 00:00:00 GMT'), v1Router)
# API Changelog
## v2.0.0 (2026-09-01)
### Breaking Changes
- `GET /projects` now returns paginated response `{ data: [], pagination: {} }`
- Removed `GET /projects/all` (use pagination instead)
### Migration Guide
- Update response parsing to read `response.data` instead of `response` directly
- Implement cursor-based pagination for large datasets
- v1 sunset date: March 1, 2027
## v1.3.0 (2026-08-15)
### Added
- `taskCount` field in project responses
- `GET /projects/{id}/activity` endpoint
Step 5: GraphQL
GraphQL APIs usually evolve one schema without version numbers: add fields and types freely, mark old ones with the @deprecated(reason: "...") directive (valid on fields and enum values, and on arguments and input fields), watch field-level usage in your server's metrics, and remove a field only when it is no longer queried.
type Project {
id: ID!
name: String!
status: String @deprecated(reason: "Use state, which has a fixed set of values.")
state: ProjectState!
}
Examples
Example 1: Add pagination without breaking existing clients
User request: "Our GET /projects returns a bare array and we need pagination. Mobile apps still use it."
The agent keeps /v1/projects untouched, adds /v2/projects with the { data, pagination } envelope from Step 1, and mounts the deprecation middleware on /v1. A request such as curl -i https://api.shopline.dev/v1/projects then returns the old body plus Deprecation: @1788220800, Sunset: Mon, 01 Mar 2027 00:00:00 GMT and a Link header pointing to the v2 path.
Example 2: Rename a GraphQL field
User request: "Rename Project.status to Project.state in our GraphQL schema."
The agent adds state, marks status with @deprecated(reason: "Use state"), keeps both resolvers returning the same value, and checks field usage in the server's metrics. It removes status only after no client queries it.
Guidelines
- URL path versioning (
/v1/, /v2/) is simplest and most widely adopted.
- Only create new major versions for breaking changes — everything else is additive.
- Keep old versions running for 6-12 months minimum after deprecation.
- Monitor old version usage — don't sunset until traffic is near zero.
- Document every breaking change with a migration guide.
- Consider API gateways (Kong, AWS API Gateway) for routing versions independently.
- Set
Deprecation as @ followed by Unix seconds, such as @1788220800 (RFC 9745); the old Deprecation: true form came from a draft and is not the standard.
- Header-based versioning (
Accept: application/vnd.shopline.v2+json or a custom API-Version header) keeps URLs stable but is harder to test in a browser and to cache; set Vary on the header you use.
/v1/, /v2/) is simplest and most widely adopted.Deprecation as @ followed by Unix seconds, such as @1788220800 (RFC 9745); the old Deprecation: true form came from a draft and is not the standard.Accept: application/vnd.shopline.v2+json or a custom API-Version header) keeps URLs stable but is harder to test in a browser and to cache; set Vary on the header you use.