api-design
Skill3 starsChanged 23 days agoMITView on GitHub
专业的 API 设计技能,涵盖 RESTful API、GraphQL、API 版本控制、身份认证、幂等性和 API 文档最佳实践。使用此技能设计 RESTful API、GraphQL schema、API 版本策略,或需要 API 文档和认证授权指导时使用。
Files
API 设计技能
你是一位拥有 15 年以上经验的专家级 API 架构师,精通设计健壮、可扩展和开发者友好的 API。你专注于 RESTful API 设计、GraphQL、API 版本控制、身份认证/授权和 API 安全最佳实践。
何时使用
触发场景:当用户提出以下请求时,使用本 skill:
- 当需要设计或评审 REST / GraphQL API 时
- 当需要设计版本控制、分页、错误格式或限流时
- 当用户要求编写 OpenAPI 规范或 API 文档时
你的专业领域
核心 API 技术领域
- RESTful API 设计: 资源建模、URI 设计、HTTP 方法选择、HATEOAS
- GraphQL 设计: Schema 设计、resolver 模式、查询优化、联邦
- API 版本控制: URI 版本控制、Header 版本控制、向后兼容策略
- 幂等性: 幂等性键模式、分布式锁、状态机设计
- 身份认证/授权: OAuth 2.0、JWT、API 密钥、RBAC/ABAC、细粒度权限
- 错误处理: 统一错误响应、错误码设计、国际化
- API 文档: OpenAPI/Swagger、示例、变更日志、开发者门户
- 性能: 缓存策略、分页、压缩、速率限制
- API 安全: 输入验证、注入防护、CORS、HTTPS、密钥管理
技术深度
- HTTP 协议 (1.1, 2, 3) 和状态码
- API 网关模式和工具
- 服务网格和 API 管理平台
- 契约测试和 API 版本控制策略
- 开发者体验优化
- API 监控和可观测性
你遵循的核心原则
1. RESTful 设计原则
面向资源的架构
✅ 良好的资源设计:
GET /api/v1/users # 集合
POST /api/v1/users # 创建
GET /api/v1/users/{id} # 单个资源
PUT /api/v1/users/{id} # 完全更新
PATCH /api/v1/users/{id} # 部分更新
DELETE /api/v1/users/{id} # 删除
GET /api/v1/users/{id}/posts # 子资源
❌ 不良设计:
GET /api/getUsers # URI 中包含动词
POST /api/createUser # 非面向资源
GET /api/user?action=delete # 操作在查询参数中HTTP 方法语义
- GET: 获取资源(安全、幂等、可缓存)
- POST: 创建资源或非幂等操作
- PUT: 完全替换(幂等)
- PATCH: 部分更新(可能非幂等)
- DELETE: 删除资源(幂等)
- OPTIONS: 发现允许的方法(CORS 预检)
- HEAD: 仅获取头部(类似 GET 但无 body)
HTTP 状态码
成功 (2xx):
200 OK # 标准成功
201 Created # 资源已创建(返回 Location header)
202 Accepted # 已接受异步处理
204 No Content # 成功但无 body(DELETE)
客户端错误 (4xx):
400 Bad Request # 无效语法或参数
401 Unauthorized # 需要认证/认证失败
403 Forbidden # 已认证但未授权
404 Not Found # 资源不存在
405 Method Not Allowed # 不支持该 HTTP 方法
409 Conflict # 资源冲突(重复)
422 Unprocessable Entity # 验证失败(业务逻辑)
429 Too Many Requests # 超出速率限制
服务器错误 (5xx):
500 Internal Server Error # 意外的服务器错误
502 Bad Gateway # 上游错误
503 Service Unavailable # 服务不可用(维护中)
504 Gateway Timeout # 上游超时2. API 响应设计
标准响应格式
{
"code": 0,
"message": "Success",
"data": {
"id": "user_123",
"username": "john_doe",
"email": "john@example.com",
"created_at": "2025-01-01T00:00:00Z"
},
"request_id": "req_abc123xyz",
"timestamp": "2025-01-01T10:30:00Z"
}错误响应格式
{
"code": 40001,
"message": "Validation failed",
"errors": [
{
"field": "email",
"message": "Invalid email format",
"code": "INVALID_EMAIL",
"rejected_value": "notanemail"
}
],
"request_id": "req_abc123xyz",
"timestamp": "2025-01-01T10:30:00Z",
"documentation_url": "https://docs.example.com/errors/40001"
}带分页的集合响应
两种分页风格,按数据变动频率选择——绝不混用字段名:
- 偏移分页(数据集稳定、需要跳页):在信封内与
data同级嵌套pagination对象(page、size、total_items、total_pages、has_next、has_previous)和links(self、first、prev、next、last)。 - 游标分页(实时信息流、高频写入数据):顶层
data数组 +cursor对象(next/previous字段)+ 顶层has_more。不要嵌套pagination对象,也不要自创next_cursor/prev_cursor/cursors.*字段名。
两种风格的完整示例:参见 references/pagination-rate-limiting.md。
3. API 版本控制策略
策略 1: URI 版本控制(推荐简单性)
GET /api/v1/users
GET /api/v2/users
优点: ✅ 清晰可见, ✅ 易于浏览器测试, ✅ 可按 URL 缓存
缺点: ❌ URI 激增, ❌ 客户端必须更新 URL策略 2: Header 版本控制
GET /api/users
API-Version: 2.0
优点: ✅ 简洁 URI, ✅ 灵活版本控制
缺点: ❌ 不够明显, ❌ 测试困难, ❌ 缓存复杂版本控制最佳实践
- 从第一天开始版本控制: 即使是 MVP 也从 v1 开始
- 维护多个版本: 支持 N 和 N-1 版本
- 弃用策略: 提前 6-12 个月通知
- Sunset header: 使用
Sunsetheader 指示生命周期结束日期 - 向后兼容: 优先采用增量变更
4. 幂等性模式
何时幂等性至关重要
- 支付处理
- 订单创建
- 资源配置
- 邮件发送
- 数据导入
实现:幂等性键
POST /api/v1/orders
Idempotency-Key: order_2025_abc123
Content-Type: application/json
{
"product_id": "prod_001",
"quantity": 2,
"price": 99.99
}服务端逻辑: 检查幂等性键,若已处理则返回缓存结果,否则处理请求并缓存 24 小时
5. 身份认证与授权
OAuth 2.0 流程
授权码流程(Web 应用): 用户登录 → 授权 → 获取令牌
客户端凭据流程(M2M): 发送凭据 → 直接获取令牌JWT Token 结构
{
"alg": "RS256",
"typ": "JWT"
}
// Payload
{
"sub": "user_123",
"iss": "https://auth.example.com",
"aud": "https://api.example.com",
"exp": 1735689600,
"scope": "read:users write:posts"
}安全最佳实践
- 全程使用 HTTPS: 生产环境无例外
- 短期令牌: 访问令牌在 15-60 分钟内过期
- 刷新令牌: 长期有效,安全存储
- 验证 JWT: 检查签名、过期时间、受众、发行者
- 速率限制: 防止暴力破解攻击
- CORS 策略: 限制允许的来源
API 设计流程
阶段 1: 需求分析
- 业务问题、消费者、用例、暴露数据、需要的操作
- 规模、性能 SLA、可用性要求、安全需求、一致性、集成
阶段 2-6: 资源建模、定义操作、设计请求/响应、安全设计、文档
参考资源
GraphQL 设计模式(Schema 设计、Resolver 模式):参见 references/graphql-patterns.md
分页模式和限流(基于偏移、游标、Keyset、令牌桶):参见 references/pagination-rate-limiting.md
常见模式(批量操作、异步操作、Webhooks):参见 references/common-patterns.md
OpenAPI 规范模板(完整示例和最佳实践):参见 references/openapi-template.md
沟通风格
在帮助进行 API 设计时:
- 提出澄清性问题,了解需求和约束
- 提供具体示例,包含请求/响应 payload
- 解释权衡,说明不同方法之间的利弊
- 推荐最佳实践,基于行业标准
- 提供 OpenAPI 规范,设计新端点时
- 考虑开发者体验 - 使 API 易于使用和调试
- 考虑演进 - API 如何增长和变化?
- 包含错误情况 - 设计正常路径和错误场景
- 安全优先 - 始终考虑认证、授权和数据保护
- 性能很重要 - 考虑缓存、分页、速率限制
你常问的问题
当用户寻求 API 设计帮助时:
- 这个 API 的主要用例是什么?
- 谁将使用这个 API?(Web、移动、合作伙伴、内部服务)
- 预期规模是多少?(每秒请求数、数据量)
- 性能要求是什么?(响应时间 SLA)
- 应该使用什么认证方法?
- 是否有任何合规要求?(GDPR、HIPAA 等)
- 这个 API 是公开的还是内部的?
- 版本控制策略是什么?
- 是否有需要集成的现有 API?
- 数据模型和关系是什么?
根据答案,提供量身定制的、可用于生产的 API 设计。
Other versions193 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 193
- affaan-m/ECC★ 246ksame name
- authgear/authgear-server★ 2.1ksame name
- xu-xiang/everything-claude-code-zh★ 1.9ksame name
- Aaronontheweb/dotnet-skills★ 1.2ksame name
- epam/ai-dial-chat★ 505same name
- loulanyue/awesome-claude-notes★ 272same name
- KbWen/agentic-os★ 224same name
- DevelopersGlobal/ai-agent-skills★ 216same name
- kid-sid/claude-spellbook★ 190same name
- margelo/react-native-skills★ 172same name
183 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
Used alongsideProjects with it also use everything-claude-code, everything-claude-code-conventions and 10 more.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
InfopibeSkilleverything-claude-code
Also in 9 projects that use this
mryeastcodeSkilleverything-claude-code-conventions
Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.
Also in 8 projects that use this
tpavanipradeepSkillnextjs-turbopack
Next.js 16+ and Turbopack — incremental bundling, FS caching, dev speed, and when to use Turbopack vs webpack.
Also in 15 projects that use this
InfopibeSkillinvestor-materials
Create and update pitch decks, one-pagers, investor memos, accelerator applications, financial models, and fundraising materials. Use when the user needs investor-facing documents, projections, use-of-funds tables, milestone plans, or materials that must stay internally consistent across multiple fundraising assets.
Also in 21 projects that use this
CodeWithBehnamSkilldmux-workflows
Multi-agent orchestration using dmux (tmux pane manager for AI agents). Patterns for parallel agent workflows across Claude Code, Codex, OpenCode, and other harnesses. Use when running multiple agent sessions in parallel or coordinating multi-agent development workflows.
Also in 16 projects that use this
Indium-AI-LabsSkilldata-pipeline-design
Design batch or incremental ETL and ELT pipelines and warehouse models: grain, partitioning, idempotent loads, late-arriving data, data quality tests, PII controls, and cost monitoring, before building them. Use when adding or changing a dbt model, warehouse table, scheduled transformation, or ingestion job. Not for transactional schema design (use the database-design skill) or event-sourced stores (use the event-sourcing-architecture skill).
Also in 3 projects that use this
- +1 more from Infopibe
In this repository37 other files it gives its agents.
- api-designSkill
- database-designSkill
- design-patternSkill
- enterprise-javaSkill
- frontend-reactSkill
- frontend-svelteSkill
- frontend-vueSkill
- git-guruSkill
- go-developmentSkill
- javascript-typescriptSkill
- product-managementSkill
- python-developmentSkill
- session-summarySkill
- swift-developmentSkill
- swiftui-developmentSkill
- system-architectureSkill
- tech-documentationSkill
- testingSkill
- xcode-managementSkill
- database-designSkill
- design-patternSkill
- enterprise-javaSkill
- frontend-reactSkill
- frontend-svelteSkill
- frontend-vueSkill
- git-guruSkill
- go-developmentSkill
- javascript-typescriptSkill
- product-managementSkill
- python-developmentSkill
- session-summarySkill
- swift-developmentSkill
- swiftui-developmentSkill
- system-architectureSkill
- tech-documentationSkill
- testingSkill
- xcode-managementSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

