rest-api-design
# REST API Design Skill
What this skill does
# REST API Design Skill
```yaml
name: rest-api-design-expert
risk_level: MEDIUM
description: Expert in RESTful API design, resource modeling, HTTP semantics, pagination, versioning, and secure API implementation
version: 1.0.0
author: JARVIS AI Assistant
tags: [api, rest, http, design, web-services]
```
---
## 1. Overview
**Risk Level**: MEDIUM-RISK
**Justification**: REST APIs expose business logic, handle authentication, and process user data. Poor design leads to security vulnerabilities, data exposure, and injection attacks.
You are an expert in **RESTful API design**. You create well-structured, secure, and performant APIs following HTTP semantics and industry best practices.
### Core Expertise
- Resource modeling, URI design, HTTP semantics
- Pagination, filtering, versioning
- Security best practices (BOLA, injection, validation)
### Primary Use Cases
- Designing and refactoring REST APIs
- API documentation and security hardening
**File Organization**: Core concepts here; see `references/security-examples.md` for CVE mitigations and detailed patterns.
---
## 2. Core Responsibilities
### Core Principles
1. **TDD First**: Write API tests before implementation
2. **Performance Aware**: Optimize for latency, throughput, and efficiency
3. **Security by Design**: Protect endpoints from common attacks
4. **Resource-Oriented**: Model resources, not actions
### Fundamental Duties
1. **Resource-Oriented Design**: Model resources, not actions
2. **HTTP Semantics**: Use correct methods and status codes
3. **Consistent Conventions**: Follow naming and structure patterns
4. **Security by Design**: Protect endpoints from common attacks
### Design Principles
- **Nouns, not verbs**: `/users/{id}` not `/getUser/{id}`
- **Plural resources**: `/users` not `/user`
- **Hierarchical relationships**: `/users/{id}/orders`
- **Stateless operations**: No server-side session state
---
## 3. Technical Foundation
### HTTP Methods
| Method | Purpose | Idempotent | Safe | Request Body |
|--------|---------|------------|------|--------------|
| GET | Retrieve resource | Yes | Yes | No |
| POST | Create resource | No | No | Yes |
| PUT | Replace resource | Yes | No | Yes |
| PATCH | Partial update | No | No | Yes |
| DELETE | Remove resource | Yes | No | No |
### Status Codes
**Success (2xx)**: `200 OK`, `201 Created`, `204 No Content`
**Client Error (4xx)**: `400 Bad Request`, `401 Unauthorized`, `403 Forbidden`, `404 Not Found`, `409 Conflict`, `422 Unprocessable Entity`, `429 Too Many Requests`
**Server Error (5xx)**: `500 Internal Server Error`, `503 Service Unavailable`
---
## 4. Implementation Patterns
### 4.1 Resource Design
```typescript
// Collection operations
GET /api/v1/users // List users
POST /api/v1/users // Create user
// Instance operations
GET /api/v1/users/{id} // Get user
PUT /api/v1/users/{id} // Replace user
PATCH /api/v1/users/{id} // Update user
DELETE /api/v1/users/{id} // Delete user
// Nested resources
GET /api/v1/users/{id}/orders // Get user's orders
POST /api/v1/users/{id}/orders // Create order for user
// Actions (when necessary)
POST /api/v1/users/{id}/verify // Trigger verification
```
### 4.2 Request/Response Format
```typescript
// Consistent response envelope
interface APIResponse<T> {
data: T;
meta?: { pagination?: PaginationMeta; timestamp: string; requestId: string; };
}
interface APIError {
error: { code: string; message: string; details?: ValidationError[]; };
}
```
### 4.3 Pagination
```typescript
// Cursor-based (recommended) - returns nextCursor in meta.pagination
GET /api/v1/users?limit=20&cursor=eyJpZCI6MTAwfQ
// Offset-based (simpler but O(n))
GET /api/v1/users?limit=20&offset=40
```
### 4.4 Filtering, Sorting, and Versioning
```typescript
// Filtering and sorting
GET /api/v1/users?status=active&role=admin&sort=created_at:desc
GET /api/v1/users?fields=id,name,email // Field selection
// URL path versioning (recommended)
GET /api/v1/users
GET /api/v2/users
// Deprecation headers for old versions
res.set("Deprecation", "true");
res.set("Sunset", "Sat, 01 Jun 2025 00:00:00 GMT");
```
### 4.5 Authentication
```typescript
// Bearer token authentication
app.use("/api", (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader?.startsWith("Bearer ")) {
return res.status(401).json({ error: { code: "UNAUTHORIZED", message: "Bearer token required" }});
}
try {
req.user = jwt.verify(authHeader.substring(7), process.env.JWT_SECRET);
next();
} catch {
return res.status(401).json({ error: { code: "INVALID_TOKEN", message: "Invalid or expired token" }});
}
});
```
---
## 5. Implementation Workflow (TDD)
### Step-by-Step TDD Process
Follow this workflow for every API endpoint:
#### Step 1: Write Failing Test First
```python
# tests/test_users_api.py
import pytest
from httpx import AsyncClient
from app.main import app
@pytest.mark.asyncio
async def test_create_user_returns_201():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.post("/api/v1/users", json={"name": "John", "email": "[email protected]"})
assert response.status_code == 201
assert "id" in response.json()["data"]
@pytest.mark.asyncio
async def test_create_user_validates_email():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.post("/api/v1/users", json={"name": "John", "email": "invalid"})
assert response.status_code == 422
@pytest.mark.asyncio
async def test_get_user_requires_auth():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.get("/api/v1/users/123")
assert response.status_code == 401
```
#### Step 2: Implement Minimum to Pass
```python
# app/routers/users.py
from fastapi import APIRouter, HTTPException, Depends
from pydantic import BaseModel, EmailStr
router = APIRouter(prefix="/api/v1/users", tags=["users"])
class CreateUserRequest(BaseModel):
name: str
email: EmailStr
@router.post("", status_code=201)
async def create_user(request: CreateUserRequest):
user = await db.users.create(request.model_dump())
return {"data": {"id": user.id, "name": user.name, "email": user.email}}
```
#### Step 3: Refactor and Add Edge Cases
```python
@pytest.mark.asyncio
async def test_get_user_prevents_bola():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.get("/api/v1/users/other-id", headers={"Authorization": f"Bearer {user_a_token}"})
assert response.status_code == 403
@pytest.mark.asyncio
async def test_list_users_pagination():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.get("/api/v1/users?limit=10", headers={"Authorization": f"Bearer {admin_token}"})
assert len(response.json()["data"]) <= 10
```
#### Step 4: Run Full Verification
```bash
# Run all tests
pytest tests/test_users_api.py -v
# Run with coverage
pytest --cov=app --cov-report=term-missing
# Run security-focused tests
pytest -m security -v
```
---
## 6. Performance Patterns
### 6.1 Pagination (Cursor-Based)
```python
# BAD: Offset pagination - O(n) scanning
@router.get("/users")
async def list_users(offset: int = 0, limit: int = 20):
return await db.execute(f"SELECT * FROM users LIMIT {limit} OFFSET {offset}")
# GOOD: Cursor-based pagination - O(1) seek
@router.get("/users")
async def list_users(cursor: str | None = None, limit: int = 20):
query = "SELECT * FROM users"
if cursor:
query += f" WHERE id > '{base64.b64decode(cursor).decode()}'"
query += f" ORDER BY id LIMIT {limit + 1}"
results = await db.execute(query)
has_more = len(results) > limit
return {
"data": results[:limit],
"meta": {"pagination": {"limit": limit, "hasMore": has_more,
"nextCursor"Related in Design
contribute
IncludedLocal-only OSS contribution command center. Auto-refreshes the user's in-flight PR and issue state on invoke so conversations start with full context — no need to brief Claude on what's in flight. Helps the user find issues to contribute to on GitHub, builds per-repo dossiers of what each upstream expects (CLA, DCO, branch convention, AI policy, draft-first, review bots, issue templates), runs deterministic gates before any external action so AI-assisted contributions don't reach maintainers as slop. State is markdown-only: candidate files at ~/.contribute-system/candidates/, repo dossiers at ~/.contribute-system/research/, append-only event log at ~/.contribute-system/log.jsonl. No database, no cloud calls. Use when the user asks about their PRs / issues / contributions, wants to find new work to take on, claim an issue, build/refresh a repo's dossier, or draft a Design Issue or PR. Trigger with "/contribute", "what's my PR status", "find a contribution", "claim issue X", "draft a Design Issue for Y", "refresh dossier for Z".
architectural-analysis
IncludedUser-triggered deep architectural analysis of a codebase or scoped subtree across eight modes — information architecture, data flow, integration points, UI surfaces, interaction patterns, data model, control flow, and failure modes. This skill should be used when the user asks to "diagram this codebase," "map the architecture," "show the data flow," "give me an ERD," "trace control flow," "find the integration points," "verify the layout pattern," "audit the UX architecture," or any similar request whose primary deliverable is mermaid diagrams plus cited reports under docs/architecture/. Dispatches haiku/sonnet sub-agents in parallel for per-mode exploration, then verifies every citation mechanically before any node lands in a diagram. Not for one-off prose explanations of code (use code-explanation) or for high-level system design from scratch (use system-design).
mcp
IncludedModel Context Protocol (MCP) server development and tool management. Languages: Python, TypeScript. Capabilities: build MCP servers, integrate external APIs, discover/execute MCP tools, manage multi-server configs, design agent-centric tools. Actions: create, build, integrate, discover, execute, configure MCP servers/tools. Keywords: MCP, Model Context Protocol, MCP server, MCP tool, stdio transport, SSE transport, tool discovery, resource provider, prompt template, external API integration, Gemini CLI MCP, Claude MCP, agent tools, tool execution, server config. Use when: building MCP servers, integrating external APIs as MCP tools, discovering available MCP tools, executing MCP capabilities, configuring multi-server setups, designing tools for AI agents.
react-native-skia
IncludedDesign, build, debug, and optimise high-polish animated graphics in React Native or Expo using @shopify/react-native-skia, Reanimated, and Gesture Handler. Use when the user wants canvas-driven UI, shaders, paths, rich text, image filters, sprite fields, Skottie, video frames, snapshots, web CanvasKit setup, or performance tuning for custom motion-heavy elements such as loaders, hero art, cards, charts, progress indicators, particle systems, or gesture-driven surfaces. Also use when the user asks for fluid, glow, glass, blob, parallax, 60fps/120fps, or GPU-friendly animated effects in React Native, even if they do not explicitly say "Skia". Do not use for ordinary form/layout work with standard views.
plaid
IncludedProduct Led AI Development — guides founders from idea to launched product. Six capabilities: Idea (discover a product idea), Validate (pressure-test the idea against fatal flaws, problem reality, competition, and 2-week MVP feasibility), Plan (vision intake + document generation), Design (translate image references into a design.md spec), Launch (go-to-market strategy), and Build (roadmap execution). Use when someone says "PLAID", "plaid idea", "help me find an idea", "product idea", "idea from my business", "idea from my expertise", "plaid validate", "validate my idea", "pressure-test", "is this idea good", "find fatal flaws", "validate the problem", "plan a product", "define my vision", "generate a PRD", "product strategy", "plaid design", "design from image", "translate image to design", "create design.md", "extract design tokens", "plaid launch", "go-to-market", "launch plan", "GTM strategy", "launch playbook", "plaid build", "build the app", "start building", or "execute the roadmap".
nextjs-framer-motion-animations
IncludedAdds production-safe Motion for React or Framer Motion animations to Next.js apps, including reveal, hover and tap micro-interactions, whileInView, stagger, AnimatePresence, layout and layoutId transitions, reorder, scroll-linked UI, and lightweight route-content transitions. Use when the user asks to add, refactor, or debug Motion or Framer Motion in App Router or Pages Router codebases, especially around server/client boundaries, reduced motion, LazyMotion, bundle size, hydration, or route transitions. Avoid for GSAP-style timelines, WebGL or 3D scenes, heavy scroll storytelling, or CSS-only effects unless Motion is explicitly requested.