api-design-rest
Design a REST endpoint or resource — URL, method, status codes, request/response shape, errors, idempotency, versioning.
- License 许可
- MIT
- 最近更新
- 2026-10-29
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- api-design-rest
- description
- Design a REST endpoint or resource — URL, method, status codes, request/response shape, errors, idempotency, versioning.
03 SKILL.md 正文
api-design-rest
Specify a REST endpoint before implementing it. Output is a one-page design that surfaces the trade-offs.
Mode: analytical — judgment inside a fixed frame. Keep the output shape; every verdict cites its evidence; unknowns are marked [TBD] or [VERIFY], never filled in.
When to use
- Adding a new endpoint.
- An existing endpoint's contract is ambiguous and a caller asks "what does this do?".
- A workflow invokes
polymath-backend:api-design-rest.
Inputs
- Resource and operation in plain English.
- Existing API conventions in the repo (read the most recent endpoint).
Project localization
Before the procedure, resolve the project snapshot —
${POLYMATH_DATA_DIR:-$HOME/.polymath/data}/polymath-core/project-context.json
(ignore it if its _meta.source belongs to another repo; absent → read
.polymath/project.yaml directly; neither → built-in defaults). Then apply
(contract: polymath-core:project-context):
conventions_docs: read rolesapi-styleandbackend-stack; followconventions.api_style_guidewhen set. Project rules win over the generic defaults below.
Procedure
-
Resource shape — name the resource (noun, plural) and its identifier scheme. URLs operate on resources, not actions:
POST /orders/:id/refunds≫POST /refundOrder. -
AuthN + AuthZ — the scope/role required, and the object-level check: the caller may act on this
:id(broken object-level authorization is the most common API vulnerability). Decide 403 vs 404 for "exists but not yours" — 404 avoids confirming the resource exists. -
Method — choose by semantics, not by accident:
- GET — safe, idempotent, cacheable.
- POST — create / non-idempotent action.
- PUT — full replace (idempotent).
- PATCH — partial update (idempotent if the patch is value-based; not if it's "append").
- DELETE — idempotent.
-
Status codes — pick from this short list and stick to it:
- 200 OK (response has body), 201 Created (with Location), 202 Accepted (async work), 204 No Content (no body).
- 400 Bad Request (validation), 401 Unauthorized (no/invalid creds), 403 Forbidden (creds OK but not allowed), 404 Not Found, 409 Conflict (concurrent edit / unique constraint), 410 Gone, 422 Unprocessable Entity (semantic validation), 429 Too Many Requests.
- 500 Internal Server Error, 503 Service Unavailable (retry hint via Retry-After).
-
Request/response shape — JSON. snake_case OR camelCase, pick one for the whole service. Show one happy-path example.
-
Errors — one error envelope for the whole service (RFC 9457
application/problem+jsonis the standard option if the service has none yet):{ "error": { "code": "RATE_LIMIT_EXCEEDED", "message": "…", "details": { "retry_after_seconds": 60 } } }codeis machine-readable,messageis human-readable,detailsis structured context. Don't surface stack traces. -
Idempotency — for
POST(and non-idempotentPATCH) writes that may be retried: require anIdempotency-Keyheader. (PUTandDELETEare idempotent by definition.) Store the request fingerprint and response against the key for ≥ 24h; a reused key with a different payload returns422, and a request arriving while the first is still in flight returns409. -
Versioning — pick one and stick to it: URL (
/v1/orders) or header (Accept: application/vnd.example+json; version=1). URL is simpler and works with caches. -
Pagination — cursor-based for collections that can grow. Page size capped server-side; expose
next_cursorin the response. -
Rate limit signaling —
RateLimit-Limit,RateLimit-Remaining,RateLimit-Resetheaders (or the legacyX-variants if the service already uses them).
Output
Endpoint: POST /v1/orders/:order_id/refunds
Purpose: Initiate a refund for an order.
Auth: Bearer (scope=orders:refund).
Idempotency: required (Idempotency-Key header).
Request body:
{ "amount_cents": int, "reason": string, "refund_to": "original"|"credit" }
Responses:
201 Created { "refund": { "id": "...", "status": "processing", ... } }
400 Bad Request amount_cents not int / missing reason
404 Not Found order_id unknown
409 Conflict order already fully refunded
422 Unprocessable amount_cents exceeds order total
429 Too Many Requests (Retry-After: 60)
Errors envelope: standard. Codes used: VALIDATION, ORDER_NOT_FOUND,
ALREADY_REFUNDED, EXCEEDS_TOTAL, RATE_LIMIT.
Anti-patterns to avoid
- Verbs in URLs (
/getOrder). - 200 OK for everything (use 201/204 where appropriate).
- "Errors" returned as 200 with an
errorfield in body. - Surfacing internal IDs / stack traces in error responses.
- Skipping idempotency on a payment / refund / send-email endpoint.
- Checking that the caller is logged in but not that they own the
:idthey're acting on.
04 同仓库更多 Skills
runbook
Write or refresh an operational runbook for an alert/symptom; structured as TL;DR + Pre-checks + Steps + Verification + Rollback + Escalation.
rfc
Draft a lightweight design RFC to docs/rfcs/<slug>.md; Summary, Motivation, Detailed design, Drawbacks, Alternatives, Unresolved questions. A proposal to debate, not a recorded decision (adr).
editorial-pass
Review and tighten prose in a Markdown artifact for a target tone (terse, formal, friendly) without dropping information; preserve quotations and load-bearing constraints.
architecture-review-facilitation
Run an architecture review as a process — entry criteria, reviewer checklist, decision log, and follow-up actions — pairing the design artifact with a red-team pass.
architecture-doc
Write a C4-style architecture document; System Context + Containers (+ Components when interesting) with mermaid diagrams. Describes structure, not a decision (adr) or proposal (rfc).
adr
Write an Architecture Decision Record (Michael Nygard format) to docs/adrs/NNNN-<slug>.md; frontmatter validated by the ADR artifact schema. Records a made decision, not a proposal to debate (rfc).