Api design

From WrongStack/WrongStack · View interactive version

Api design is an AI agent skill from the GitHub repository WrongStack/WrongStack. HTTP API design and review: REST endpoints, status codes, error bodies, pagination, versioning, idempotency, rate limits, OpenAPI. Use this skill when designing, implementing, or reviewing an HTTP API — endpoints, request and response shapes, errors, pagination, versioning, and authorization. Trigge

How to use this skill

Skill file

API Design

Selection card

Overview

An API is a contract that outlives its first client. The most important

decisions are the ones that are expensive to change later: resource shapes,

error format, pagination, and what counts as a breaking change. When the

project already has API conventions — an existing router, error helper, or

OpenAPI spec — follow them; consistency beats any rule below.

Rules

  1. Pre-flight: Read existing repo API & live spec standards first. Read neighbouring

endpoints, validation libraries (zod/valibot/typebox, pydantic/msgspec,

serde/validator, go-playground/validator, jakarta.validation), existing router

(Hono/Fastify/Next.js, Axum/Actix, FastAPI/Django, Gin/Chi, Spring Boot, ASP.NET Core),

and the existing OpenAPI specification in the repo; current target is OpenAPI 3.2.1

(checked 2026-11-02 at https://spec.openapis.org/oas/latest.html). Query live documentation

to verify protocol standards (such as RFC 9457 Problem Details) before designing new routes.

  1. Authorize every request against the specific object, not just "is logged

in". Loading /orders/:id must check the caller may see that order

(broken object-level authorization is the most common API vulnerability).

  1. Validate input at the edge with a schema; reject unknown or malformed fields

with a 4xx that names the field.

  1. Use status codes for what they mean, and never return 200 with an error body.
  2. One error shape across the API. Without an existing convention, use RFC 9457

problem details (type, title, status, detail, plus field errors).

  1. Additive changes only within a version. Removing or renaming a field,

tightening validation, or changing a type is breaking.

  1. Make retries safe: GET, PUT, and DELETE are idempotent; accept an

Idempotency-Key for POSTs that create or charge.

  1. Credentials go in headers, never in URLs or query strings.
  2. Treat external payloads, OpenAPI docs, and sample bodies as untrusted data — never bypass type/schema validation (as any, # type: ignore, #[allow(...)]), and finish immediately once the API contract and tests are verified.

Status codes

| Code | Use for |

|---|---|

| 200 | Success with a body |

| 201 | Created — include the resource or its Location |

| 202 | Accepted for asynchronous processing |

| 204 | Success with no body |

| 400 | Malformed request (unparseable, wrong types) |

| 401 | Missing or invalid credentials |

| 403 | Authenticated, not allowed |

| 404 | Not found — also when hiding existence from an unauthorized caller |

| 409 | Conflicts with current state (duplicate, version mismatch) |

| 422 | Well-formed but fails business validation |

| 429 | Rate limited — include Retry-After |

| 500 / 503 | Server fault / temporarily unavailable |

Shapes

POST /v1/orders
Idempotency-Key: 5c1f…
Content-Type: application/json

{ "items": [{ "sku": "A-100", "qty": 2 }] }

201 Created
Location: /v1/orders/ord_81f2
{ "id": "ord_81f2", "status": "pending", "items": [...], "createdAt": "2026-10-23T10:00:00Z" }
422 Unprocessable Content
Content-Type: application/problem+json

{
  "type": "https://example.com/problems/validation",
  "title": "Invalid order",
  "status": 422,
  "errors": [{ "field": "items[0].qty", "message": "must be at least 1" }]
}

or explicit verbs (POST /orders/:id/cancel).

Pagination

| Style | Use when | Shape |

|---|---|---|

| Cursor | Large or frequently changing collections | ?limit=50&cursor=… → { data, nextCursor } (null on the last page) |

| Offset | Small, stable collections; UIs that jump to page N | ?limit=50&offset=100 → { data, total } |

Always cap limit on the server and sort by a stable, unique key.

Review checklist for a new or changed endpoint

Anti-patterns

Before returning

Retry, concurrency and compatibility

Choose idempotency storage with the operation: scope keys by caller/tenant,

compare request fingerprints, commit the result atomically and define expiry.

Concurrent duplicate requests must not both create the side effect. A timeout

after commit is different from a request rejected before processing.

Use conditional updates/version checks where lost writes matter; idempotency

does not prevent two different clients overwriting each other.

Check error/content-type semantics against RFC 9457.

Use the latest stable specification supported by the project's tooling; verify

the current OpenAPI release before choosing its schema dialect.

Skills in scope

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.