Source: https://github.com/aipoch/medical-research-skills
API Design Principles
When to Use
- Designing a new REST API contract for CRUD-style resources and you need consistent resource modeling, naming, and HTTP semantics.
- Designing a new GraphQL schema for multiple clients with different data shapes and you need clear type/field ownership and safe evolution.
- Reviewing an existing API (REST or GraphQL) to identify inconsistencies in naming, error handling, pagination/filtering, or versioning/deprecation.
- Deciding between REST vs. GraphQL (or defining boundaries when mixing both) and documenting trade-offs and constraints.
- Standardizing cross-cutting concerns (authn/authz, rate limiting, observability, long-running operations, idempotency) across multiple services.
Key Features
- End-to-end workflow for API design/review: requirements → style choice → domain modeling → operations → cross-cutting concerns → deliverables.
- REST guidance: resource-oriented modeling, stable identifiers, relationship patterns, and correct HTTP verb usage.
- GraphQL guidance: schema/type modeling, Query vs. Mutation separation, input types for writes, and explicit side-effect handling.
- Cross-cutting design patterns: consistent error model, pagination/filtering/sorting, versioning and deprecation strategy, and operational concerns.
- Review checklist to validate completeness, highlight risks/gaps, and produce actionable follow-ups.
Dependencies
- None (documentation-only skill).
- Reference documents:
references/rest.md
references/graphql.md
references/review-checklist.md
Example Usage
Goal
Design (or review) an API for managing Projects and Tasks, and produce a contract with examples, error model, pagination, and a checklist summary.
Step 1: Clarify requirements and constraints
- Consumers: Web app + mobile app + internal admin.
- Constraints: p95 latency < 200ms for list endpoints; PII present; audit logging required.
- Core use cases: list projects, view project, create task, update task status, search tasks by status/assignee.
Step 2: Choose API style and boundaries
- Choose REST for resource-oriented CRUD with cacheable reads and straightforward endpoints.
- If GraphQL is later introduced for client-specific views, define boundaries (e.g., GraphQL for read aggregation; REST remains source-of-truth for writes).
Step 3: Produce a REST contract skeleton (runnable examples)
Base URL
https://api.example.com/v1
Resources
projects
tasks (scoped under a project)
Endpoints
GET /v1/projects
POST /v1/projects
GET /v1/projects/{projectId}
GET /v1/projects/{projectId}/tasks
POST /v1/projects/{projectId}/tasks
PATCH /v1/projects/{projectId}/tasks/{taskId}
List projects (pagination + filtering)
Request
curl -sS -X GET "https://api.example.com/v1/projects?limit=20&cursor=eyJpZCI6IjEwMCJ9&sort=createdAt:desc" \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json"
Response (200)
{
"data": [
{
"id": "proj_123",
"name": "Roadmap 2026",
"createdAt": "2026-01-10T12:00:00Z"
}
],
"page": {
"limit": 20,
"nextCursor": "eyJpZCI6InByb2pfMTIzIn0="
}
}
Create a task (idempotency)
Request
curl -sS -X POST "https://api.example.com/v1/projects/proj_123/tasks" \
-H "Authorization: Bearer $TOKEN" \
-H "Idempotency-Key: 2b7b1a2e-7f2b-4c2a-9c2b-0b3b7c9d1a11" \
-H "Content-Type: application/json" \
-d '{
"title": "Draft API spec",
"assigneeId": "user_42",
"dueAt": "2026-03-01T00:00:00Z"
}'
Response (201)
{
"data": {
"id": "task_999",
"projectId": "proj_123",
"title": "Draft API spec",
"status": "OPEN",
"assigneeId": "user_42",
"dueAt": "2026-03-01T00:00:00Z",
"createdAt": "2026-02-25T09:00:00Z"
}
}
Error model example
Response (409)
{
"error": {
"code": "CONFLICT",
"message": "A task with the same title already exists in this project.",
"details": {
"field": "title",
"reason": "DUPLICATE"
},
"requestId": "req_01HTZQ8K7Y9M2A3B4C5D6E7F8G"
}
}
Step 4: Run the review checklist
Use references/review-checklist.md to validate:
- Naming consistency (resources, fields, enums)
- HTTP semantics and status codes
- Pagination/filtering/sorting rules
- Error model completeness and stability
- Versioning/deprecation plan
- Security and observability requirements
Expected deliverable format (save to outputs/)
- API style choice + trade-offs
- Contract skeleton (endpoints or schema)
- Request/response (or query/mutation) examples
- Error model + pagination strategy
- Checklist results + risks/gaps
Implementation Details
Recommended workflow (design/review)
Clarify requirements and constraints
- Identify domain, core use cases, and consumer types (web/mobile/partners/internal).
- Capture constraints: latency, throughput, consistency, compliance, data sensitivity.
Choose API style and boundaries
- REST: best for resource-oriented APIs, cacheable reads, and simple CRUD.
- GraphQL: best for multiple clients with varying data shapes and frequent iteration.
- If mixing, define boundaries to avoid overlapping responsibilities.
Domain modeling
- REST: model stable resources (nouns), stable identifiers, and relationships.
- GraphQL: define types and field ownership; use input types for writes.
Operation and behavior design
- REST: map operations to HTTP verbs; represent actions via sub-resources or noun-based endpoints when needed.
- GraphQL: separate
Query vs. Mutation; document side effects explicitly.
- Define idempotency (especially for creates) and patterns for long-running tasks when applicable.
Cross-cutting concerns
- Authentication/authorization
- Error model (stable codes, actionable messages, request correlation IDs)
- Pagination, filtering, sorting (document defaults and limits)
- Versioning and deprecation strategy
- Observability (logging/metrics/tracing), rate limiting
Reference guides
- REST Principles and Patterns:
references/rest.md
- GraphQL Principles and Patterns:
references/graphql.md
- Review Checklist:
references/review-checklist.md
1---2name: api-design-principles3description: Principles and checklists for designing and reviewing REST and GraphQL APIs; use when defining or evaluating API contracts (endpoints/schemas), naming, error models, pagination, versioning, and REST vs. GraphQL trade-offs.4license: MIT5---6> **Source**: [https://github.com/aipoch/medical-research-skills](https://github.com/aipoch/medical-research-skills)
7
8# API Design Principles
9
10## When to Use
11
12- Designing a new REST API contract for CRUD-style resources and you need consistent resource modeling, naming, and HTTP semantics.
13- Designing a new GraphQL schema for multiple clients with different data shapes and you need clear type/field ownership and safe evolution.
14- Reviewing an existing API (REST or GraphQL) to identify inconsistencies in naming, error handling, pagination/filtering, or versioning/deprecation.
15- Deciding between REST vs. GraphQL (or defining boundaries when mixing both) and documenting trade-offs and constraints.
16- Standardizing cross-cutting concerns (authn/authz, rate limiting, observability, long-running operations, idempotency) across multiple services.
17
18## Key Features
19
20- End-to-end workflow for API design/review: requirements → style choice → domain modeling → operations → cross-cutting concerns → deliverables.
21- REST guidance: resource-oriented modeling, stable identifiers, relationship patterns, and correct HTTP verb usage.
22- GraphQL guidance: schema/type modeling, Query vs. Mutation separation, input types for writes, and explicit side-effect handling.
23- Cross-cutting design patterns: consistent error model, pagination/filtering/sorting, versioning and deprecation strategy, and operational concerns.
24- Review checklist to validate completeness, highlight risks/gaps, and produce actionable follow-ups.
25
26## Dependencies
27
28- None (documentation-only skill).
29- Reference documents:
30 - `references/rest.md`
31 - `references/graphql.md`
32 - `references/review-checklist.md`
33
34## Example Usage
35
36### Goal
37
38Design (or review) an API for managing `Projects` and `Tasks`, and produce a contract with examples, error model, pagination, and a checklist summary.
39
40### Step 1: Clarify requirements and constraints
41
42- Consumers: Web app + mobile app + internal admin.
43- Constraints: p95 latency < 200ms for list endpoints; PII present; audit logging required.
44- Core use cases: list projects, view project, create task, update task status, search tasks by status/assignee.
45
46### Step 2: Choose API style and boundaries
47
48- Choose **REST** for resource-oriented CRUD with cacheable reads and straightforward endpoints.
49- If GraphQL is later introduced for client-specific views, define boundaries (e.g., GraphQL for read aggregation; REST remains source-of-truth for writes).
50
51### Step 3: Produce a REST contract skeleton (runnable examples)
52
53**Base URL**
54- `https://api.example.com/v1`
55
56**Resources**
57- `projects`
58- `tasks` (scoped under a project)
59
60**Endpoints**
61- `GET /v1/projects`
62- `POST /v1/projects`
63- `GET /v1/projects/{projectId}`
64- `GET /v1/projects/{projectId}/tasks`
65- `POST /v1/projects/{projectId}/tasks`
66- `PATCH /v1/projects/{projectId}/tasks/{taskId}`
67
68#### List projects (pagination + filtering)
69
70**Request**
71```bash
72curl -sS -X GET "https://api.example.com/v1/projects?limit=20&cursor=eyJpZCI6IjEwMCJ9&sort=createdAt:desc" \
73 -H "Authorization: Bearer $TOKEN" \
74 -H "Accept: application/json"
75```
76
77**Response (200)**
78```json
79{
80 "data": [
81 {
82 "id": "proj_123",
83 "name": "Roadmap 2026",
84 "createdAt": "2026-01-10T12:00:00Z"
85 }
86 ],
87 "page": {
88 "limit": 20,
89 "nextCursor": "eyJpZCI6InByb2pfMTIzIn0="
90 }
91}
92```
93
94#### Create a task (idempotency)
95
96**Request**
97```bash
98curl -sS -X POST "https://api.example.com/v1/projects/proj_123/tasks" \
99 -H "Authorization: Bearer $TOKEN" \
100 -H "Idempotency-Key: 2b7b1a2e-7f2b-4c2a-9c2b-0b3b7c9d1a11" \
101 -H "Content-Type: application/json" \
102 -d '{
103 "title": "Draft API spec",
104 "assigneeId": "user_42",
105 "dueAt": "2026-03-01T00:00:00Z"
106 }'
107```
108
109**Response (201)**
110```json
111{
112 "data": {
113 "id": "task_999",
114 "projectId": "proj_123",
115 "title": "Draft API spec",
116 "status": "OPEN",
117 "assigneeId": "user_42",
118 "dueAt": "2026-03-01T00:00:00Z",
119 "createdAt": "2026-02-25T09:00:00Z"
120 }
121}
122```
123
124#### Error model example
125
126**Response (409)**
127```json
128{
129 "error": {
130 "code": "CONFLICT",
131 "message": "A task with the same title already exists in this project.",
132 "details": {
133 "field": "title",
134 "reason": "DUPLICATE"
135 },
136 "requestId": "req_01HTZQ8K7Y9M2A3B4C5D6E7F8G"
137 }
138}
139```
140
141### Step 4: Run the review checklist
142
143Use `references/review-checklist.md` to validate:
144- Naming consistency (resources, fields, enums)
145- HTTP semantics and status codes
146- Pagination/filtering/sorting rules
147- Error model completeness and stability
148- Versioning/deprecation plan
149- Security and observability requirements
150
151### Expected deliverable format (save to `outputs/`)
152
153- API style choice + trade-offs
154- Contract skeleton (endpoints or schema)
155- Request/response (or query/mutation) examples
156- Error model + pagination strategy
157- Checklist results + risks/gaps
158
159## Implementation Details
160
161### Recommended workflow (design/review)
162
1631. **Clarify requirements and constraints**
164 - Identify domain, core use cases, and consumer types (web/mobile/partners/internal).
165 - Capture constraints: latency, throughput, consistency, compliance, data sensitivity.
166
1672. **Choose API style and boundaries**
168 - **REST**: best for resource-oriented APIs, cacheable reads, and simple CRUD.
169 - **GraphQL**: best for multiple clients with varying data shapes and frequent iteration.
170 - If mixing, define boundaries to avoid overlapping responsibilities.
171
1723. **Domain modeling**
173 - REST: model stable resources (nouns), stable identifiers, and relationships.
174 - GraphQL: define types and field ownership; use input types for writes.
175
1764. **Operation and behavior design**
177 - REST: map operations to HTTP verbs; represent actions via sub-resources or noun-based endpoints when needed.
178 - GraphQL: separate `Query` vs. `Mutation`; document side effects explicitly.
179 - Define **idempotency** (especially for creates) and patterns for **long-running tasks** when applicable.
180
1815. **Cross-cutting concerns**
182 - Authentication/authorization
183 - Error model (stable codes, actionable messages, request correlation IDs)
184 - Pagination, filtering, sorting (document defaults and limits)
185 - Versioning and deprecation strategy
186 - Observability (logging/metrics/tracing), rate limiting
187
188### Reference guides
189
190- REST Principles and Patterns: `references/rest.md`
191- GraphQL Principles and Patterns: `references/graphql.md`
192- Review Checklist: `references/review-checklist.md`