Communitygithub.com

API 版本策略

選擇並操作一套不會破壞客戶端的 HTTP API 版本策略——Stripe 的基於日期的固定版本、Deprecation/Sunset 標頭對(RFC 9745 + RFC 8594)、URI 與標頭與媒體類型方法的比較,以及版本轉換器模式。基於 Stripe 公布的架構和 IETF RFC。

API 版本策略 是什麼?

此技能為開發者提供了一套全面的 API 版本策略決策框架,重點關注在不破壞客戶端的前提下進行 API 演進。它詳細介紹了 Stripe 的基於日期的固定版本模型,這是一種按 API 金鑰固定版本的有效方法,允許頻繁的小規模重大變更,而無需強制客戶端進行完整的 v1 到 v2 遷移。技能深入比較了各種策略,包括 URI 區段、自定義標頭(如 Stripe-Version)、Accept 媒體類型和查詢參數,並為每種策略提供了清晰的優缺點和適合場景。它還涵蓋了 Deprecation 和 Sunset HTTP 標頭的標準用法(基於 RFC 9745 和 RFC 8594),以優雅地通知和廢棄舊端點。此外,技能介紹了 Stripe 使用的版本轉換器模式,這是一種架構模式,允許為多個舊版本維護一個現代的核心邏輯,從而降低維護複雜性。最後,它明確區分了內部 API 和公共 API 的不同版本策略,建議內部 API 採用僅添加的演進方式,而公共 API 則需要更嚴格的強制性版本控制流程。該技能適用於任何想要設計健壯且可持續的 API 的團隊,涵蓋從快速原型到大型高規模公共 API 的所有場景。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/curiositech/windags-skills/tree/HEAD/skills/api-versioning-strategy

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

API 版本策略 是做什麼的?

TL;DR: 基於日期、按 API 金鑰固定的版本(Stripe 模型)比 /v1/、/v2/ 更適合長期存在的公共 API,因為它允許進行小規模的重大變更,而無需強制客戶端遷移到新的版本樹。對於內部 API,僅添加的演進加上 Deprecation 和 Sunset 標頭(RFC 9745, RFC 8594)通常就足夠了。務必提前公告,務必在指定日期廢棄,切勿直接刪除。


快速導覽

狀況章節
"需要中斷一個欄位,但有 10 萬個 API 金鑰"基於日期固定的版本
"我該用 /v1/ 還是 Accept: application/vnd.foo.v2+json?"策略比較
"如何告訴客戶端某個端點將被棄用?"Deprecation + Sunset
"在程式碼中維護 6 個版本快把我們搞垮了"版本轉換器
"內部 API —— 我們真的需要版本控制嗎?"內部 API 與公共 API

決策圖

flowchart TD
  A[需要改變 API 行為] --> B{僅添加?<br/>新欄位、新端點、新的可選參數}
  B -->|是| C[直接發布<br/>無需版本控制]
  B -->|否,屬於中斷性變更| D{API 是公共的<br/>+ 客戶端較多且不透明?}
  D -->|否,內部/客戶端較少| E[協調遷移<br/>+ Deprecation/Sunset 標頭]
  D -->|是,公共| F{能否為每個請求<br/>構建兼容層?}
  F -->|可以,有轉換器| G[基於日期固定的版本<br/>Stripe 模型]
  F -->|不行,需要大規模重寫| H[主要版本 URI 提升<br/>/v1 → /v2]
  E --> I[設定 Deprecation: @timestamp<br/>+ Sunset: HTTP-date<br/>+ Link: rel=deprecation]
  G --> I
  H --> I

1. 基於日期固定的版本(Stripe 模型)

Stripe 在 API versioning at Stripe 中發布了其架構:

用戶第一次發出 API 請求時,其帳戶會自動固定到最新的可用版本,之後該用戶進行的每個 API 呼叫都會隱式地分配到該版本。

[版本是] 以發布日期命名的滾動版本(例如 2017-05-24)。

關鍵屬性:

屬性為什麼有效
每個帳戶的默認版本(在首次調用時設定)新客戶自動固定到最新版本;既有客戶在您發布變更時不會中斷
Stripe-Version 標頭可覆蓋每個請求的固定版本允許逐步遷移客戶端:SDK 可以在帳戶升級前,先選擇使用新版本
基於日期的命名(2024-04-10)傳達時效性;無需爭論 "v3" 的含義;允許任意多次的小規模中斷,而非累積到一次大規模的 v3 發布
儀表板升級路徑客戶可以預覽差異,然後升級其帳戶的默認版本

這是唯一能擴展到真正長期存在的公共 API 的方法(Stripe 的版本可追溯到十年前)。它會增加您的內部複雜度(第 4 節中的轉換器模式),但能讓您的客戶免於永無止境的 /v1/ → /v2/ 遷移週期。


2. 版本策略比較

策略版本位置優點缺點最佳適用場景
URI 區段(/v1/, /v2/)路徑快取友好(不同 URL = 不同快取條目);無需客戶端配置無法進行小規模中斷;強制進行版本樹分支;URL 不再是 "穩定的資源標識符"(根據 Fielding 的說法)內部 API,很少中斷的公共 API
自定義標頭(Stripe-Version: 2024-04-10)請求標頭大量版本開銷低;每個請求可細粒度控制無法共享內嵌版本的 URL;在瀏覽器網址列中測試較困難大規模公共 API
Accept 媒體類型(Accept: application/vnd.foo.v2+json)標準 Accept 標頭符合 HTTP "規範";重用內容協商機制設定麻煩;工具支援不一;透過 curl 進行除錯的指令冗長超媒體 API,深度 RESTful 設計
查詢參數(?version=2)URL易於測試;在日誌中可見污染 URL;語義上不正確(版本不是資源屬性)僅限快速原型開發
無版本控制,僅添加無零開銷;無需客戶端協調若不建立新端點,則永遠無法進行中斷性變更具有共同部署的內部微服務

根據您的限制條件選擇:

  • "我們頻繁進行小規模中斷" → 基於日期的標頭(Stripe)
  • "我們每 5 年進行一次大規模重寫" → URI 區段(/v1/, /v2/)
  • "我們從不中斷,只添加" → 無版本控制 + 僅添加演進

3. Deprecation/Sunset 標頭對

兩個 IETF RFC 涵蓋了生命週期信號:

Sunset 標頭 — RFC 8594

Sunset 的值是一個 HTTP-date 時間戳,如 [RFC7231] 第 7.1.1.1 節所定義,並且 SHOULD 是未來的時間戳。

Sunset: Sat, 31 Dec 2026 23:59:59 GMT

表示 "該資源預計在未來某個特定時間點變得無響應"。客戶端 SHOULD 將該時間戳視為提示,而非硬性合約。

Deprecation 標頭 — RFC 9745

Deprecation: @1735689599
Sunset: Sun, 31 Dec 2026 23:59:59 GMT
Link: <https://api.example.com/docs/migrate-v1-to-v2>; rel="deprecation"

Deprecation 的值是一個 Unix 時間戳(秒,根據 Structured Fields 規範前綴 @)。它可以是過去的("已棄用")或未來的("將要棄用")。必須遵守的事項:

  • MUST 使用 RFC 9651 中的結構化欄位日期格式
  • Sunset MUST NOT 早於 Deprecation——規範非常明確;這是一個時間順序約束
  • SHOULD 包含一個 Link,其 rel="deprecation" 指向遷移文件

發送 Deprecation 的行為並不會改變資源的行為——它是一個信號,而非降級。伺服器 SHOULD 在 Sunset 日期之前保持資源的功能正常(緊急情況除外)。

建議的時間線

T+0      首次在生產環境發送 Deprecation 標頭
T+30d    公開公告(更新日誌、電子郵件、文件)
T+90d    在回應日誌/指標中添加對已棄用路徑使用情況的記錄
T+180d   設定 Sunset 日期,提前 90 天
T+270d   Sunset 日期到來——端點返回 410 Gone 並附帶遷移連結

對於主要版本固定的 API 上的小規模中斷性變更:中位數為 6 個月。對於完整的 v1 → v2 廢棄:12-24 個月。


4. 版本轉換器模式

Stripe 的內部架構解決了 "我們在生產環境中有 50 個版本" 的問題:

[該系統使用] API 資源類別來定義當前的 API 回應結構,並結合版本變更模組來封裝向後不相容的轉換。在處理回應時,系統向後回溯時間,並套用沿途找到的每個版本變更模組,直到達到目標版本。

其形式:

// resource: 規範(最新)表示
const charge = {
  id: 'ch_123',
  amount: 1000,
  payment_method_details: { card: { brand: 'visa', last4: '4242' } },
}

// 每個中斷性變更都是一個轉換器模組:
const v_2024_03_01 = {
  // 移除 payment_method_details 下的 'card' 巢狀,展平欄位
  apply(resource) {
    return {
      ...resource,
      card_brand: resource.payment_method_details?.card?.brand,
      card_last4: resource.payment_method_details?.card?.last4,
    }
  },
}

const v_2023_10_15 = {
  // 將 'amount' 重新命名為 'amount_cents'
  apply(resource) {
    const { amount, ...rest } = resource
    return { ...rest, amount_cents: amount }
  },
}

// 管線按時間倒序套用轉換器,直到達到請求的版本
function transform(resource, requestedVersion) {
  const chain = transformers.filter(t => t.date > requestedVersion)
  return chain.reduceRight((r, t) => t.apply(r), resource)
}

優勢:

  • 核心程式碼保持現代化——每個端點都針對最新的 schema 編寫;業務邏輯中不會散落 if (version < ...)。
  • 每個中斷性變更都是一個檔案——易於審查、易於隔離測試、易於在最後一個用戶固定版本越過該變更後刪除。
  • 遙測——您可以統計每天觸發哪些轉換器,以便根據實際使用情況規劃廢棄。

代價:構建轉換器框架。如果您每年有 >3 次中斷性變更且 >10k 個 API 消費者,那麼這值得投入;對於 <50 個客戶的內部 API 則過於複雜。


5. 內部 API 與公共 API

其不對稱性很重要:

內部公共
是否可以協調部署?可以——原子交換不行——客戶端獨立部署
Schema 演進僅添加 + Slack 訊息嚴格的版本控制 + 電子郵件 + 儀表板 + 部落格文章
廢棄窗口數天到數週6-24 個月
版本控制需求通常不需要——只需 Deprecation 標頭強制
最佳策略僅添加演進;僅在重寫時使用 URI 版本基於日期的標頭(如果規模允許),主要重寫使用 URI

陷阱:公司將所有 API 都視為 "公共" API,並為僅由 3 個內部服務使用的 API 構建了笨重的版本控制基礎設施。相反的陷阱:一個洩漏的內部 API 有了外部消費者,而您實際上無法中斷它。

防禦措施:將 "公共" 作為服務上的一個二進位標籤。公共服務獲得完整的版本控制儀式;內部服務則沒有,但會對誰在呼叫它們進行積極監控。


反模式

反模式為什麼會出問題修正方案
為了單一中斷性變更將 /v1/ 提升到 /v2/強制客戶端為了一個欄位遷移所有東西基於日期的版本或僅添加演進
移除端點時沒有 Sunset 標頭客戶端在沒有任何警告的情況下中斷;支援工單爆炸務必在移除前 90 天以上配對 Deprecation 標頭
發送了 Deprecation 但沒有 Link客戶端知道它已棄用,但不知道該怎麼辦務必包含 Link: <migration-doc>; rel="deprecation"
為每個客戶硬編碼版本例外變得無法維護;無法推斷行為使用轉換器模式;"例外" 變為版本固定
像對待公共 API 一樣對內部 API 進行版本控制流程開銷大但沒有回報僅添加演進 + 原子部署 + Slack
在 REST API 上使用 semver(v1.2.3)暗示了不適用的含義(對於 JSON schema 來說什麼是 "修補"?)基於日期的命名或僅主版本(v1)
設定了 Sunset 日期但未強制執行端點永遠存在,程式碼腐爛日曆提醒;在 Sunset 前 7 天失敗的 CI 測試以強制行動

新手 / 專家 / 時間線

新手專家
第一次中斷性變更將 /v1 提升到 /v2,複製貼上整個版本樹考慮基於日期的固定,評估變更是否確實是中斷性的
告知客戶端有關棄用的訊息發送大量電子郵件Deprecation + Sunset 標頭 + Link + 電子郵件 + 儀表板橫幅
維護 5 個版本在每個控制器中使用 5 個 if/else 分支轉換器模組,僅保留最新版本的核心程式碼
強制執行 Sunset"我們會處理的"在 CI 中設定 Sunset 日期;端點在該日期變為 410 Gone
內部 API 變更與公共 API 相同的儀式——緩慢僅添加演進 + 原子部署;為公共 API 保留繁重流程

時間線測試:從 "我們想要移除欄位 X" 到 "欄位 X 在生產環境中消失" 需要多長時間?一個專家團隊會有一個可重複的答案(例如,90 天)。一個新手團隊則沒有答案,因為沒有流程。


品質關卡

當滿足以下條件時,版本控制變更才能發布:

  • 測試: 移除或重新命名任何外部可見的欄位,必須以至少 N 天前發布的棄用公告為前提(其中 N 是記錄在案的廢棄窗口)。
  • 測試: 每個已棄用的端點都發送 Deprecation: @<unix-ts> 和 Sunset: <HTTP-date> 標頭,並在整合測試中驗證。
  • 測試: Sunset 值永遠不早於 Deprecation 值(根據 RFC 9745 的 MUST 要求)。
  • 測試: 每個棄用公告都包含一個 Link: <docs-url>; rel="deprecation" 標頭,並且該 URL 存在一個公共的遷移文件(在 CI 中進行 HEAD 檢查)。
  • 測試: 存在每個版本的使用情況遙測——在刪除 v2 之前,您可以回答 "昨天 v2 路徑上有多少請求?"。
  • 測試(僅限日期固定): 新的 API 金鑰預設使用最新版本;既有的金鑰在部署期間保持其固定版本。
  • 手動: Sunset 強制執行計劃:一個日曆提醒、一個 CI 檢查或一個自動化的 410 Gone——總有某個機制 在日期到來時觸發。

不屬於本技能的內容

  • GraphQL schema 演進(請使用 graphql-schema-evolution)
  • gRPC / Protobuf 線路兼容性(請使用 protobuf-evolution-rules)
  • 資料庫 schema 版本控制(請使用 zero-downtime-database-migration)
  • 函式庫 / SDK semver(請使用 npm-package-versioning)
  • 事件流 schema 演進(請使用 kafka-schema-registry-design)

資料來源

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

curiositech/windags-skills

Expert in 2000s-era music visualization (Milkdrop, AVS, Geiss) and modern WebGL implementations. Specializes in Butterchurn integration, Web Audio API AnalyserNode FFT data, GLSL shaders for audio-reactive visuals, and psychedelic generative art. Activate on "Milkdrop", "music visualization", "WebGL visualizer", "Butterchurn", "audio reactive", "FFT visualization", "spectrum analyzer". NOT for simple bar charts/waveforms (use basic canvas), video editing, or non-audio visuals.

curiositech/windags-skills

Expert legal research agent for finding and scraping expungement data state by state. Knows authoritative sources, URL patterns, Firecrawl configuration, and 2026 legal landscape.

curiositech/windags-skills

Expert in 3D computer vision labeling tools, workflows, and AI-assisted annotation for LiDAR, point clouds, and sensor fusion. Covers SAM4D/Point-SAM, human-in-the-loop architectures, and vertical-specific training strategies. Activate on '3D labeling', 'point cloud annotation', 'LiDAR labeling', 'SAM 3D', 'SAM4D', 'sensor fusion annotation', '3D bounding box', 'semantic segmentation point cloud'. NOT for 2D image labeling (use clip-aware-embeddings), general ML training (use ml-engineer), video annotation without 3D (use computer-vision-pipeline), or VLM prompt engineering (use prompt-engineer).

curiositech/windags-skills

Implement WCAG 2.2 AA/AAA compliance with automated testing, keyboard navigation, screen reader support, and focus management. Activate on: accessibility audit, WCAG compliance, keyboard navigation, screen reader, aria attributes, axe-core, focus trap. NOT for: design-level accessibility review (use design-accessibility-auditor), color contrast only (use css-in-js-architect).

curiositech/windags-skills

Time-blind friendly planning, executive function support, and daily structure for ADHD brains. Specializes in realistic time estimation, dopamine-aware task design, and building systems that actually work for neurodivergent minds.

curiositech/windags-skills

Designs digital experiences for ADHD brains using neuroscience research and UX principles. Expert in reducing cognitive load, time blindness solutions, dopamine-driven engagement, and compassionate design patterns. Activate on 'ADHD design', 'cognitive load', 'accessibility', 'neurodivergent UX', 'time blindness', 'dopamine-driven', 'executive function'. NOT for general accessibility (WCAG only), neurotypical UX design, or simple UI styling without ADHD context.

curiositech/windags-skills

>- Apply crisis decision-making research to agent routing, uncertainty triage, and coordination failure analysis in time-pressured systems. Use when diagnosing handoff failures, analytical paralysis, or expert judgment under incomplete information. NOT for routine coding, simple CRUD design, or static single-agent tasks with complete information.

curiositech/windags-skills

Extend and modify the admin dashboard, developer portal, and operations console. Use when adding new admin tabs, metrics, monitoring features, or internal tools. Activates for dashboard development, analytics, user management, and internal tooling.

curiositech/windags-skills

Conversation patterns and interaction protocols for multi-agent systems. Covers request/response, pub/sub, blackboard, delegation chains, debate, critique, consensus, fan-out/fan-in, supervisor-worker, and peer negotiation. Deep analysis of AutoGen conversation patterns, CrewAI delegation, LangGraph state passing, and FIPA-ACL performatives. Teaches how to design what agents say to each other and in what order. Activate on: "agent conversation", "agent protocol", "multi-agent debate", "agent delegation", "supervisor worker pattern", "agent voting", "consensus protocol", "fan-out fan-in", "agent negotiation", "blackboard pattern", "agent dialogue", "conversation topology", "agent handoff". NOT for: wire format or serialization (use agent-interchange-formats), orchestration infrastructure (use agentic-infrastructure-2026), single agent behavior (use agentic-patterns).

curiositech/windags-skills

Meta-agent for creating new custom agents, skills, and MCP integrations. Expert in agent design, MCP development, skill architecture, and rapid prototyping. Activate on 'create agent', 'new skill', 'MCP server', 'custom tool', 'agent design'. NOT for using existing agents (invoke them directly), general coding (use language-specific skills), or infrastructure setup (use deployment-engineer).

curiositech/windags-skills

AI-powered calendar management and agent-based scheduling coordination. Covers calendar APIs (Google Calendar, CalDAV/iCal), AI scheduling assistants (Reclaim, Clockwise, Motion, Cal.com), building custom calendar agents with MCP, multi-calendar merging, timezone management, focus block protection, meeting fatigue detection, and agent-to-agent meeting negotiation protocols. Activate on: "calendar agent", "AI scheduling", "calendar coordination", "meeting scheduling", "calendar API", "focus time protection", "calendar optimization", "Google Calendar MCP", "Reclaim", "Clockwise", "Motion", "Cal.com", "smart scheduling", "calendar-aware agent", "timezone scheduling", "agent negotiation meetings". NOT for: manual calendar UI component design (use form-validation-architect), project management scheduling or Gantt charts (use project-management-guru-adhd), general time-tracking or pomodoro apps (use adhd-daily-planner for time-awareness), building the agent itself from scratch (use agent-creator).

curiositech/windags-skills

Build and adopt production AI agent infrastructure in 2026. Covers framework selection (LangGraph, CrewAI, AutoGen, MCP), orchestration patterns, evaluation, observability, memory systems, and tool use. Also covers the SOCIAL dimension: how to sell agent infrastructure internally, change management, measuring ROI, building trust in autonomous systems, and scaling adoption across teams. Activate on: "agent infrastructure", "agent framework comparison", "which agent framework", "sell AI tools internally", "agent adoption", "agent observability", "agent evaluation", "MCP architecture", "agentic mesh", "enterprise AI agents", "AI change management", "agent ROI". NOT for: building specific agents (use ai-engineer), designing agent behavior patterns (use agentic-patterns), prompt tuning (use prompt-engineer).

curiositech/windags-skills

Fundamental patterns for effective agentic behavior. Teaches decomposition, tool orchestration, error recovery, context management, quality self-assessment, and knowing when to stop. Model-agnostic principles that make any agent more effective regardless of domain. Activate on: "how should I structure this agent", "agentic workflow", "agent patterns", "multi-step task", "tool orchestration", "/agentic-patterns", "decompose this", "agent best practices", "chain of actions", "when should the agent stop", "agent loop design". NOT for: creating agent infrastructure (use agent-creator), building DAGs (use windags-architect), specific tool implementation.

curiositech/windags-skills

Automated discovery and matching of agent skills for dynamic task routing and capability assessment

curiositech/windags-skills

Cryptographic security for agentic systems — zero-trust agent networking, signed message envelopes (JWS/JWE), capability-based security (ocaps), Merkle tree audit trails, WASM sandboxing, and formal verification. Covers CLI dev tool security, mTLS between agents, permission boundaries (least privilege for AI agents), and supply chain security for skills/plugins. Activate on: "agent security", "zero trust agents", "secure agent communication", "capability-based security", "ocap", "signed messages between agents", "agent audit trail", "sandbox agent execution", "agent permissions", "mTLS agents", "cryptographic verification", "agent supply chain", "OWASP agentic", "prove agent did X", "tamper-proof agent logs". NOT for: application-level SAST scanning (use security-auditor), network firewall rules (use infrastructure), SOC2/HIPAA compliance (organizational), or prompt injection defense (use prompt-engineer).

curiositech/windags-skills

Data structures and serialization formats for agent-to-agent communication. Covers message envelopes, structured output schemas, capability declarations, task handoff payloads, error/retry signaling, and context windows as data structures. Deep comparison of A2A protocol, MCP, OpenAI function calling, and LangChain message types. Teaches when to use rigid schemas vs free-form with validation, typed vs untyped, streaming vs batch. Activate on: "agent message format", "agent communication schema", "agent-to-agent protocol", "A2A protocol", "MCP message format", "structured output for agents", "agent interop", "interchange format", "agent serialization", "task handoff format", "capability declaration". NOT for: what agents say to each other (use agent-conversation-protocols), orchestration topology (use multi-agent-coordination), building agent infrastructure (use agentic-infrastructure-2026).

curiositech/windags-skills

Logic-based agent programming language implementing BDI architecture for practical autonomous agent development

curiositech/windags-skills

>- Design AgentSpeak(L)-style BDI agents with context-guarded plans, selection functions, and intention stacks. Use for interruptible autonomy, agent policy, and multi-agent orchestration in dynamic environments. NOT for simple rule engines, static planners, or centralized workflows.

curiositech/windags-skills

Foundational concurrent computation model where actors communicate exclusively through asynchronous message passing

curiositech/windags-skills

license: Apache-2.0 NOT for unrelated tasks outside this domain.

相關技能