api-design
Skill2k starsChanged 9 months agoMITView on GitHub
生产级 API 的 REST API 设计模式,包括资源命名、状态码、分页、过滤、错误响应、版本控制和速率限制。
125 other projects use a version of this, 25 of them edited.
Trending
Quiet since October.thin dataabout the whole repository
Most recently taken up by
- AllThingsSmitty/proven-skillstheir own version, first seen Oct 6, 2026
- Lukk17/agent-standardstheir own version, first seen Oct 5, 2026
- SkillMedev/api-integration-engineeringtheir own version, first seen Oct 5, 2026
- SkillMedev/full-stack-web-devtheir own version, first seen Oct 5, 2026
- cellrishi-code/claude-skills-atlastheir own version, first seen Oct 4, 2026
- mnzralee/claude-multi-agent-architecturetheir own version, first seen Oct 3, 2026
Who's talking about xu-xiang/everything-claude-code-zh
The biggest 1 of 1 mention linking to the repository that our crawl found. Each line is their own headline.
Used with
Projects with it also use data-modeling, ci-pipeline and 8 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.
mmondoraSkilldata-modeling
Schema design, multi-tenant data isolation, and migration management. Firestore and PostgreSQL patterns, RLS, UUID v7 conventions. Use when designing database schemas, writing migrations, or implementing multi-tenant data access.
Also in 7 projects that use this
vndeeSkillci-pipeline
Use when creating or updating CI/CD pipelines with GitHub Actions or GitLab CI for Go, Python, or React projects
Also in 4 projects that use this
pluginagentmarketplaceSkillmicroservices
Microservices architecture patterns and best practices. Service decomposition, inter-service communication, and distributed data management.
Also in 4 projects that use this
kevinaimonsterSkilltypescript-strict
TypeScript strict mode and advanced type system assistant. Helps write strictly-typed TypeScript code with generics, type guards, utility types, conditional types, mapped types, and template literal types. Trigger when user says 'TypeScript types' 'type gymnastics' 'TypeScript generics' 'strict TypeScript' 'type guard' 'TS类型' 'TypeScript严格模式' '类型体操' '泛型怎么写' 'TS type help'. Keywords: TypeScript, strict mode, generics, type guard, utility types, conditional types, mapped types, template literal types, infer, extends, keyof, typeof, type narrowing, discriminated unions, type assertion, type predicate, type gymnastics, TS, 类型体操, 泛型, 类型守卫, 严格类型
Also in 4 projects that use this
DatabricksSkillload-testing
Load test a Databricks App to find its maximum QPS. Use when: (1) User says 'load test', 'benchmark', 'QPS', 'throughput', or 'performance test', (2) User wants to find how many queries per second their app can handle, (3) User wants to set up load testing scripts for their agent, (4) User wants to view load test results/dashboard, (5) User wants to validate results or observe latency/error rate with MLflow tracing.
Also in 4 projects that use this
CloudAI-XSkilldatabase-design
Designs database schemas, indexing strategies, query optimization, and migration patterns for SQL and NoSQL databases. Use when designing tables, optimizing queries, fixing N+1 problems, planning migrations, or when asked about database performance, normalization, ORMs, or data modeling.
Also in 15 projects that use this
Other versions125 projects use a version of this.
Word for word · 0
None found.
Edited · 25
- loulanyue/awesome-claude-notes★ 27242% same
- tpavanipradeep/everything-claude-code★ 13442% same
- templetongroup/radiant★ 11342% same
- Jamkris/everything-gemini-code★ 8742% same
- skateddu/claude-code-python-setup★ 2842% same
- woxiangyangzhimao/skills★ 1441% same
- Infopibe/everything-claude-code★ 842% same
- zero-click/zero-ratchet★ 842% same
- yevhens-hue/claude-skills-starter-kit★ 442% same
- Yog-Sotho/claude-skills★ 316% same
15 more not listed
Wrote their own version · 100
- Aaronontheweb/dotnet-skills★ 1.2ksame name
- nth5693/gemini-kit★ 373same name
- KbWen/agentic-os★ 206same name
- margelo/react-native-skills★ 173same name
- cohen-liel/hivemind★ 110same name
- microsoft/haste★ 106same name
- s-morgan-jeffries/apple-mail-fast-mcp★ 104same name
- travisjneuman/.claude★ 101same name
- proyecto26/system-design-skills★ 85same name
- wshaddix/dotnet-skills★ 79same name
90 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.
In this repository114 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .codex/AGENTS.mdAGENTS.md
- rules/agents.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- examples/CLAUDE.mdCLAUDE.md
- .cursor/rules/common-agents.mdCursor rule
- .cursor/rules/common-coding-style.mdCursor rule
- .cursor/rules/common-development-workflow.mdCursor rule
- .cursor/rules/common-git-workflow.mdCursor rule
- .cursor/rules/common-hooks.mdCursor rule
- .cursor/rules/common-patterns.mdCursor rule
- .cursor/rules/common-performance.mdCursor rule
- .cursor/rules/common-security.mdCursor rule
- .cursor/rules/common-testing.mdCursor rule
- .cursor/rules/golang-coding-style.mdCursor rule
- .cursor/rules/golang-hooks.mdCursor rule
- .cursor/rules/golang-patterns.mdCursor rule
- .cursor/rules/golang-security.mdCursor rule
- .cursor/rules/golang-testing.mdCursor rule
- .cursor/rules/python-coding-style.mdCursor rule
- .cursor/rules/python-hooks.mdCursor rule
- .cursor/rules/python-patterns.mdCursor rule
- .cursor/rules/python-security.mdCursor rule
- .cursor/rules/python-testing.mdCursor rule
- .cursor/rules/swift-coding-style.mdCursor rule
- .cursor/rules/swift-hooks.mdCursor rule
- .cursor/rules/swift-patterns.mdCursor rule
- .cursor/rules/swift-security.mdCursor rule
- .cursor/rules/swift-testing.mdCursor rule
- .cursor/rules/typescript-coding-style.mdCursor rule
- .cursor/rules/typescript-hooks.mdCursor rule
- .cursor/rules/typescript-patterns.mdCursor rule
- .cursor/rules/typescript-security.mdCursor rule
- .cursor/rules/typescript-testing.mdCursor rule
- article-writingSkill
- backend-patternsSkill
- coding-standardsSkill
- content-engineSkill
- e2e-testingSkill
- eval-harnessSkill
- frontend-patternsSkill
- frontend-slidesSkill
- investor-materialsSkill
- investor-outreachSkill
- market-researchSkill
- security-reviewSkill
- strategic-compactSkill
- tdd-workflowSkill
- verification-loopSkill
- oneskillSkill
- article-writingSkill
- content-engineSkill
- frontend-slidesSkill
- investor-materialsSkill
- investor-outreachSkill
- market-researchSkill
- api-designSkill
- article-writingSkill
- autonomous-loopsSkill
- backend-patternsSkill
- The first 60 shown.
The file
Files
API 设计模式(API Design Patterns)
用于设计一致且开发者友好的 REST API 的约定与最佳实践。
何时激活(When to Activate)
- 设计新的 API 端点(Endpoint)
- 审查现有的 API 合约
- 添加分页、过滤或排序功能
- 为 API 实现错误处理
- 规划 API 版本化策略
- 构建公开或面向合作伙伴的 API
资源设计(Resource Design)
URL 结构
# 资源应为名词、复数、小写、短横线命名法(kebab-case)
GET /api/v1/users
GET /api/v1/users/:id
POST /api/v1/users
PUT /api/v1/users/:id
PATCH /api/v1/users/:id
DELETE /api/v1/users/:id
# 关系的子资源
GET /api/v1/users/:id/orders
POST /api/v1/users/:id/orders
# 不符合 CRUD 的操作(谨慎使用动词)
POST /api/v1/orders/:id/cancel
POST /api/v1/auth/login
POST /api/v1/auth/refresh命名规则
# 推荐(GOOD)
/api/v1/team-members # 多词资源使用短横线命名法(kebab-case)
/api/v1/orders?status=active # 使用查询参数进行过滤
/api/v1/users/123/orders # 嵌套资源表示归属关系
# 错误(BAD)
/api/v1/getUsers # URL 中包含动词
/api/v1/user # 使用单数(应使用复数)
/api/v1/team_members # URL 中使用蛇形命名法(snake_case)
/api/v1/users/123/getOrders # 嵌套资源中包含动词HTTP 方法与状态码(HTTP Methods and Status Codes)
方法语义
| 方法 | 幂等性(Idempotent) | 安全性(Safe) | 用途 |
| GET | 是 | 是 | 获取资源 |
| POST | 否 | 否 | 创建资源,触发操作 |
| PUT | 是 | 否 | 完整替换资源 |
| PATCH | 否* | 否 | 部分更新资源 |
| DELETE | 是 | 否 | 删除资源 |
*通过正确的实现,PATCH 也可以设计为幂等。
状态码参考
# 成功(Success)
200 OK — GET, PUT, PATCH(包含响应体)
201 Created — POST(需包含 Location 响应头)
204 No Content — DELETE, PUT(不含响应体)
# 客户端错误(Client Errors)
400 Bad Request — 校验失败,JSON 格式错误
401 Unauthorized — 缺失或无效的身份验证
403 Forbidden — 已验证身份但未获得授权
404 Not Found — 资源不存在
409 Conflict — 重复条目,状态冲突
422 Unprocessable Entity — 语义错误(JSON 正确但数据非法)
429 Too Many Requests — 超出速率限制
# 服务端错误(Server Errors)
500 Internal Server Error — 意外错误(绝不要暴露详细堆栈)
502 Bad Gateway — 上游服务失败
503 Service Unavailable — 暂时性过载,需包含 Retry-After常见错误
# 错误:所有响应都返回 200
{ "status": 200, "success": false, "error": "Not found" }
# 推荐:语义化地使用 HTTP 状态码
HTTP/1.1 404 Not Found
{ "error": { "code": "not_found", "message": "User not found" } }
# 错误:校验错误返回 500
# 推荐:返回 400 或 422 并提供字段级详情
# 错误:创建资源返回 200
# 推荐:返回 201 并附带 Location 响应头
HTTP/1.1 201 Created
Location: /api/v1/users/abc-123响应格式(Response Format)
成功响应
{
"data": {
"id": "abc-123",
"email": "alice@example.com",
"name": "Alice",
"created_at": "2025-01-15T10:30:00Z"
}
}集合响应(含分页)
{
"data": [
{ "id": "abc-123", "name": "Alice" },
{ "id": "def-456", "name": "Bob" }
],
"meta": {
"total": 142,
"page": 1,
"per_page": 20,
"total_pages": 8
},
"links": {
"self": "/api/v1/users?page=1&per_page=20",
"next": "/api/v1/users?page=2&per_page=20",
"last": "/api/v1/users?page=8&per_page=20"
}
}错误响应
{
"error": {
"code": "validation_error",
"message": "Request validation failed",
"details": [
{
"field": "email",
"message": "Must be a valid email address",
"code": "invalid_format"
},
{
"field": "age",
"message": "Must be between 0 and 150",
"code": "out_of_range"
}
]
}
}响应封装变体
// 方案 A:包含 data 包装器的信封模式(推荐用于公开 API)
interface ApiResponse<T> {
data: T;
meta?: PaginationMeta;
links?: PaginationLinks;
}
interface ApiError {
error: {
code: string;
message: string;
details?: FieldError[];
};
}
// 方案 B:扁平化响应(更简单,常用于内部 API)
// 成功:直接返回资源对象
// 失败:返回错误对象
// 通过 HTTP 状态码进行区分分页(Pagination)
基于偏移量(Offset-Based,简单)
GET /api/v1/users?page=2&per_page=20
# 实现
SELECT * FROM users
ORDER BY created_at DESC
LIMIT 20 OFFSET 20;优点: 易于实现,支持“跳转到第 N 页”。 缺点: 在大偏移量时速度慢(OFFSET 100000),且在并发插入时可能出现数据重复或遗漏。
基于游标(Cursor-Based,可扩展)
GET /api/v1/users?cursor=eyJpZCI6MTIzfQ&limit=20
# 实现
SELECT * FROM users
WHERE id > :cursor_id
ORDER BY id ASC
LIMIT 21; -- 多获取一个以确定是否有下一页(has_next){
"data": [...],
"meta": {
"has_next": true,
"next_cursor": "eyJpZCI6MTQzfQ"
}
}优点: 无论在什么位置性能都保持一致,在并发插入时保持稳定。 缺点: 无法跳转到任意页,游标是不透明的(不可读)。
选型建议
| 使用场景 | 分页类型 |
| 管理后台、小型数据集 (<10K) | 偏移量(Offset) |
| 无限滚动、Feed 流、大型数据集 | 游标(Cursor) |
| 公开 API | 默认游标,可选支持偏移量 |
| 搜索结果 | 偏移量(用户通常期望看到页码) |
过滤、排序与搜索(Filtering, Sorting, and Search)
过滤
# 简单相等
GET /api/v1/orders?status=active&customer_id=abc-123
# 比较操作符(使用括号标记法)
GET /api/v1/products?price[gte]=10&price[lte]=100
GET /api/v1/orders?created_at[after]=2025-01-01
# 多个值(逗号分隔)
GET /api/v1/products?category=electronics,clothing
# 嵌套字段(点标记法)
GET /api/v1/orders?customer.country=US排序
# 单字段(前缀 - 表示降序)
GET /api/v1/products?sort=-created_at
# 多字段(逗号分隔)
GET /api/v1/products?sort=-featured,price,-created_at全文搜索
# 搜索查询参数
GET /api/v1/products?q=wireless+headphones
# 字段特定搜索
GET /api/v1/users?email=alice稀疏字段集(Sparse Fieldsets)
# 仅返回指定字段(减小负载)
GET /api/v1/users?fields=id,name,email
GET /api/v1/orders?fields=id,total,status&include=customer.name身份验证与授权(Authentication and Authorization)
基于令牌的认证
# Authorization 标头中的 Bearer 令牌
GET /api/v1/users
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
# API 密钥(用于服务间调用)
GET /api/v1/data
X-API-Key: sk_live_abc123授权模式
// 资源级:检查所有权
app.get("/api/v1/orders/:id", async (req, res) => {
const order = await Order.findById(req.params.id);
if (!order) return res.status(404).json({ error: { code: "not_found" } });
if (order.userId !== req.user.id) return res.status(403).json({ error: { code: "forbidden" } });
return res.json({ data: order });
});
// 基于角色:检查权限
app.delete("/api/v1/users/:id", requireRole("admin"), async (req, res) => {
await User.delete(req.params.id);
return res.status(204).send();
});速率限制(Rate Limiting)
响应头
HTTP/1.1 200 OK
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1640000000
# 超过限制时
HTTP/1.1 429 Too Many Requests
Retry-After: 60
{
"error": {
"code": "rate_limit_exceeded",
"message": "Rate limit exceeded. Try again in 60 seconds."
}
}速率限制分层
| 层级 | 限制 | 窗口(Window) | 使用场景 |
| 匿名用户 | 30/min | 按 IP | 公开端点 |
| 已验证用户 | 100/min | 按用户 | 标准 API 访问 |
| 高级用户 | 1000/min | 按 API 密钥 | 付费 API 套餐 |
| 内部调用 | 10000/min | 按服务 | 服务间通信 |
版本控制(Versioning)
URL 路径版本化(推荐)
/api/v1/users
/api/v2/users优点: 显式、易于路由、可缓存。 缺点: 版本间 URL 发生变化。
Header 版本化
GET /api/users
Accept: application/vnd.myapp.v2+json优点: URL 保持整洁。 缺点: 难以测试,容易遗忘。
版本化策略
1. 从 /api/v1/ 开始 —— 在确实需要前不要进行版本化
2. 最多维护 2 个活跃版本(当前版本 + 上一版本)
3. 弃用时间表:
- 宣布弃用(对公开 API 提前 6 个月通知)
- 添加 Sunset 标头:Sunset: Sat, 01 Jan 2026 00:00:00 GMT
- 在 Sunset 日期后返回 410 Gone
4. 非破坏性变更不需要新版本:
- 在响应中添加新字段
- 添加新的可选查询参数
- 添加新的端点
5. 破坏性变更需要新版本:
- 移除或重命名字段
- 更改字段类型
- 更改 URL 结构
- 更改身份验证方式实现模式(Implementation Patterns)
TypeScript (Next.js API 路由)
import { z } from "zod";
import { NextRequest, NextResponse } from "next/server";
const createUserSchema = z.object({
email: z.string().email(),
name: z.string().min(1).max(100),
});
export async function POST(req: NextRequest) {
const body = await req.json();
const parsed = createUserSchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json({
error: {
code: "validation_error",
message: "Request validation failed",
details: parsed.error.issues.map(i => ({
field: i.path.join("."),
message: i.message,
code: i.code,
})),
},
}, { status: 422 });
}
const user = await createUser(parsed.data);
return NextResponse.json(
{ data: user },
{
status: 201,
headers: { Location: `/api/v1/users/${user.id}` },
},
);
}Python (Django REST Framework)
from rest_framework import serializers, viewsets, status
from rest_framework.response import Response
class CreateUserSerializer(serializers.Serializer):
email = serializers.EmailField()
name = serializers.CharField(max_length=100)
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["id", "email", "name", "created_at"]
class UserViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer
permission_classes = [IsAuthenticated]
def get_serializer_class(self):
if self.action == "create":
return CreateUserSerializer
return UserSerializer
def create(self, request):
serializer = CreateUserSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = UserService.create(**serializer.validated_data)
return Response(
{"data": UserSerializer(user).data},
status=status.HTTP_201_CREATED,
headers={"Location": f"/api/v1/users/{user.id}"},
)Go (net/http)
func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
var req CreateUserRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid_json", "Invalid request body")
return
}
if err := req.Validate(); err != nil {
writeError(w, http.StatusUnprocessableEntity, "validation_error", err.Error())
return
}
user, err := h.service.Create(r.Context(), req)
if err != nil {
switch {
case errors.Is(err, domain.ErrEmailTaken):
writeError(w, http.StatusConflict, "email_taken", "Email already registered")
default:
writeError(w, http.StatusInternalServerError, "internal_error", "Internal error")
}
return
}
w.Header().Set("Location", fmt.Sprintf("/api/v1/users/%s", user.ID))
writeJSON(w, http.StatusCreated, map[string]any{"data": user})
}API 设计自检表(API Design Checklist)
在发布新端点之前:
- 资源 URL 遵循命名约定(复数、短横线命名法、无动词)
- 使用了正确的 HTTP 方法(读取用 GET,创建用 POST 等)
- 返回了适当的状态码(不要所有响应都返回 200)
- 使用模式校验输入数据(如 Zod, Pydantic, Bean Validation)
- 错误响应遵循标准格式,包含代码(Code)和消息(Message)
- 为列表端点实现了分页(游标或偏移量)
- 要求身份验证(或显式标记为公开)
- 已检查授权(用户只能访问其自身的资源)
- 配置了速率限制
- 响应不会泄露内部详情(如堆栈追踪、SQL 错误)
- 与现有端点的命名保持一致(小驼峰 camelCase vs 蛇形 snake_case)
- 已编写文档(更新了 OpenAPI/Swagger 规范)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
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.

