Setup
On first use, read setup.md for integration behavior and memory initialization.
When to Use
Use this skill when the user wants to design, implement, secure, test, and ship a REST API from scratch or harden an existing API for production.
This skill covers contract-first design, endpoint conventions, authentication and authorization, persistence strategy, test plans, observability, and release checklists.
Architecture
Working memory lives in ~/rest-api/. See memory-template.md for structure and status behavior.
CODEBLOCK0
Quick Reference
Load only what is needed for the current API task.
| Topic | File |
|---|
| Setup and activation behavior | INLINECODE3 |
| Memory schema |
memory-template.md |
| Contract-first design |
api-contract.md |
| Endpoint conventions and errors |
endpoint-design.md |
| Auth and API security controls |
auth-and-security.md |
| Data model and migrations |
persistence-and-migrations.md |
| Test strategy and telemetry |
testing-and-observability.md |
| Pre-release readiness gate |
deployment-checklist.md |
Core Rules
1. Start From the Contract, Not the Controller
Define resources, payload schemas, status codes, and error shapes in OpenAPI before writing handlers.
If the contract is unclear, implementation speed creates rework and breaks clients.
2. Keep Endpoint Semantics Predictable
Use stable naming, plural resources, and correct HTTP methods. Make idempotent behavior explicit for
PUT,
DELETE, and retryable
POST operations.
Predictable semantics reduce client bugs and support safer retries.
3. Enforce Security by Default
Require authentication on non-public endpoints, apply authorization checks at resource boundary, validate input strictly, and sanitize output.
Never rely on frontend validation as a security control.
4. Design for Failure Paths First
Specify error classes, timeout strategy, rate-limit behavior, and fallback expectations before scaling happy-path code.
APIs fail in production at edges, not in demos.
5. Make Data Changes Backward Compatible
Use additive schema migrations first, backfill data safely, and only remove old fields after client migration windows close.
Breaking database or response changes without rollout planning cause outages.
6. Test Contract, Behavior, and Operations
Cover OpenAPI contract validation, integration tests against real infrastructure, and end-to-end tests for critical user journeys.
Unit tests alone do not prove API reliability.
7. Ship With Observability and Runbooks
Expose request metrics, structured errors, trace identifiers, and health indicators. Document recovery steps for known failure modes.
If an API cannot be observed, it cannot be operated safely.
Common Traps
- - Building endpoints before defining response and error contracts -> incompatible clients and patchwork fixes.
- Mixing auth, business logic, and transport concerns in handlers -> brittle code and hidden security gaps.
- Treating pagination and filtering as optional -> unstable list endpoints and expensive queries.
- Returning inconsistent error bodies across services -> poor client DX and weak automation.
- Shipping without migration rollback steps -> long incidents when a release fails.
Security & Privacy
Data that leaves your machine:
Data that stays local:
- - API project context and decisions under
~/rest-api/.
This skill does NOT:
- - Call undeclared external endpoints by default.
- Store secrets automatically.
- Modify infrastructure without explicit user instruction.
Related Skills
Install with
clawhub install <slug> if user confirms:
- -
backend - System design and backend architecture decisions. - INLINECODE17 - Authentication, session strategy, and credential safety.
- INLINECODE18 - HTTP protocol details and request-response behavior.
- INLINECODE19 - Third-party API integration references.
Feedback
- - If useful: INLINECODE20
- Stay updated: INLINECODE21
设置
首次使用时,请阅读 setup.md 了解集成行为和内存初始化。
使用时机
当用户希望从零开始设计、实现、保护、测试并交付一个REST API,或为生产环境加固现有API时,使用此技能。
本技能涵盖契约优先设计、端点规范、认证与授权、持久化策略、测试计划、可观测性以及发布检查清单。
架构
工作内存位于 ~/rest-api/。请参阅 memory-template.md 了解结构和状态行为。
~/rest-api/
├── memory.md # 热:活跃API项目上下文
├── contracts/ # 温:OpenAPI规范与兼容性说明
├── decisions/ # 温:ADR风格的技术决策
├── tests/ # 温:测试计划与质量门禁
├── operations/ # 温:运行手册与事件记录
└── archive/ # 冷:已关闭项目与旧版本
快速参考
仅加载当前API任务所需的内容。
memory-template.md |
| 契约优先设计 | api-contract.md |
| 端点规范与错误 | endpoint-design.md |
| 认证与API安全控制 | auth-and-security.md |
| 数据模型与迁移 | persistence-and-migrations.md |
| 测试策略与遥测 | testing-and-observability.md |
| 预发布就绪门禁 | deployment-checklist.md |
核心规则
1. 从契约开始,而非控制器
在编写处理器之前,先在OpenAPI中定义资源、负载模式、状态码和错误形状。
如果契约不清晰,实现速度会导致返工并破坏客户端。
2. 保持端点语义可预测
使用稳定的命名、复数资源和正确的HTTP方法。明确声明PUT、DELETE和可重试POST操作的幂等行为。
可预测的语义减少客户端错误,并支持更安全的重试。
3. 默认强制执行安全
对非公共端点要求认证,在资源边界应用授权检查,严格验证输入,并清理输出。
切勿依赖前端验证作为安全控制手段。
4. 优先为失败路径设计
在扩展正常路径代码之前,明确错误类别、超时策略、速率限制行为和回退预期。
API在生产环境中失败于边界,而非演示场景。
5. 确保数据变更向后兼容
优先使用增量模式迁移,安全地回填数据,仅在客户端迁移窗口关闭后才移除旧字段。
未经发布计划就破坏数据库或响应变更会导致服务中断。
6. 测试契约、行为和运维
覆盖OpenAPI契约验证、针对真实基础设施的集成测试,以及关键用户旅程的端到端测试。
仅靠单元测试无法证明API的可靠性。
7. 交付时附带可观测性和运行手册
暴露请求指标、结构化错误、追踪标识和健康指标。记录已知故障模式的恢复步骤。
如果API不可观测,则无法安全运维。
常见陷阱
- - 在定义响应和错误契约之前构建端点 -> 导致客户端不兼容和拼凑式修复。
- 在处理器中混合认证、业务逻辑和传输关注点 -> 导致脆弱代码和隐藏安全漏洞。
- 将分页和过滤视为可选 -> 导致列表端点不稳定和查询开销大。
- 跨服务返回不一致的错误体 -> 导致客户端体验差和自动化能力弱。
- 发布时没有迁移回滚步骤 -> 发布失败时导致长时间事件。
安全与隐私
离开您机器的数据:
保留在本地的数据:
- - ~/rest-api/下的API项目上下文和决策。
本技能不会:
- - 默认调用未声明的外部端点。
- 自动存储密钥。
- 未经用户明确指示修改基础设施。
相关技能
如果用户确认,使用 clawhub install
安装:
- - backend - 系统设计与后端架构决策。
- auth - 认证、会话策略和凭证安全。
- http - HTTP协议细节与请求-响应行为。
- api - 第三方API集成参考。
反馈
- - 如果觉得有用:clawhub star rest-api
- 保持更新:clawhub sync