REST API design patterns. Use when designing endpoints, error responses, pagination, versioning, or API structure. Framework-agnostic principles for building consistent, maintainable APIs.
Ships in typescript. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Inspect the owning package and existing implementation first. Reuse established project types, helpers, errors, lifecycle behavior, and test utilities. The patterns below are options, not an implementation checklist. Introduce one only when the current task requires it.
Use the following patterns only when the task designs or explicitly changes the public API. For internal work, preserve the current routes, payloads, errors, status codes, pagination, and idempotency behavior.
Use consistent, predictable URL patterns:
# Collection resources (plural nouns)
GET /api/v1/users # List users
POST /api/v1/users # Create user
GET /api/v1/users/:id # Get user
PUT /api/v1/users/:id # Update user (full)
PATCH /api/v1/users/:id # Update user (partial)
DELETE /api/v1/users/:id # Delete user
# Nested resources
GET /api/v1/users/:userId/posts # List user's posts
POST /api/v1/users/:userId/posts # Create post for user
GET /api/v1/users/:userId/posts/:postId # Get specific post
# Actions (use verbs sparingly)
POST /api/v1/users/:id/activate # Activate user
POST /api/v1/posts/:id/publish # Publish post
POST /api/v1/invoices/:id/send # Send invoice
/users, not /user)/ledger-accounts)/users/:id/posts/:id)When a public breaking change requires a new version and the project uses path versioning, version APIs in the URL path:
/api/v1/users
/api/v2/users
# Not in headers (harder to test/debug)
# Not in query params (breaks caching)
// v1/routes.ts
export async function v1Routes(app: FastifyInstance) {
app.get('/users', getUsersV1)
app.post('/users', createUserV1)
}
// v2/routes.ts
export async function v2Routes(app: FastifyInstance) {
app.get('/users', getUsersV2) // Breaking change in response structure
app.post('/users', createUserV2)
}
// server.ts
app.register(v1Routes, { prefix: '/api/v1' })
app.register(v2Routes, { prefix: '/api/v2' })
Reuse the current public error format. Use RFC 7807 only when the task explicitly changes the public error contract and selects this format:
interface ProblemDetail {
type: string // Error type identifier
status: number // HTTP status code
title: string // Short, human-readable summary
detail: string // Specific explanation for this occurrence
instance: string // URI reference to specific occurrence
traceId: string // Request trace ID for debugging
}
// Example error response
{
"type": "NOT_FOUND",
"status": 404,
"title": "Not Found",
"detail": "User with ID usr_01h455vb4pex5vsknk084sn02q not found",
"instance": "/api/v1/users/usr_01h455vb4pex5vsknk084sn02q",
"traceId": "req_abc123xyz"
}
// Domain error base class
abstract class AppError extends Error {
abstract readonly status: number
abstract readonly type: string
constructor(message: string, public readonly context?: ErrorContext) {
super(message)
}
toResponse(instance: string, traceId: string): ProblemDetail {
return {
type: this.type,
status: this.status,
title: this.name,
detail: this.message,
instance,
traceId,
...this.context,
}
}
}
// Specific error types
class NotFoundError extends AppError {
readonly status = 404
readonly type = 'NOT_FOUND'
}
class ConflictError extends AppError {
readonly status = 409
readonly type = 'CONFLICT'
constructor(
message: string,
public readonly retryable: boolean = false,
context?: ErrorContext
) {
super(message, context)
}
}
class ServiceUnavailableError extends AppError {
readonly status = 503
readonly type = 'SERVICE_UNAVAILABLE'
constructor(
message: string,
public readonly retryable: boolean = true,
context?: ErrorContext
) {
super(message, context)
}
}
See references/error-responses.md for complete examples.
When the task changes a public collection contract and current query behavior requires cursor pagination, use an opaque cursor that matches the existing data model:
// Request
GET /api/v1/posts?limit=20&cursor=pst_01h455vb4pex5vsknk084sn02q
// Response
{
"items": [
{ "id": "pst_01h455w3x8k5z9y7q1m0n2b3c4", ... },
{ "id": "pst_01h455x2y9l6a0z8r2n1o3c5d6", ... }
],
"nextCursor": "pst_01h455z1a0m7b8y9s3o2p4d6e7",
"hasMore": true
}
interface PaginatedRequest {
limit?: number // Max items to return (default 20, max 100)
cursor?: string // Cursor for next page (opaque to client)
}
interface PaginatedResponse<T> {
items: T[]
nextCursor?: string
hasMore: boolean
}
async function listPosts(req: PaginatedRequest): Promise<PaginatedResponse<Post>> {
const limit = Math.min(req.limit ?? 20, 100)
const queryLimit = limit + 1 // Fetch one extra to check hasMore
const posts = await db.query.posts.findMany({
where: req.cursor ? gt(posts.id, req.cursor) : undefined,
orderBy: desc(posts.createdAt),
limit: queryLimit,
})
const hasMore = posts.length > limit
const items = posts.slice(0, limit)
const nextCursor = hasMore ? items[items.length - 1].id : undefined
return { items, nextCursor, hasMore }
}
❌ Offset-based (/posts?offset=40&limit=20)
- Unstable: Items can shift if new records inserted
- Performance: DB must scan all previous rows
- Inaccurate: Can miss or duplicate items
✅ Cursor-based (/posts?cursor=pst_xyz&limit=20)
- Stable: Cursor points to specific item
- Performant: DB uses index seek
- Accurate: No gaps or duplicates
Use query parameters for filtering and sorting:
# Filtering
GET /api/v1/users?status=active&role=admin
GET /api/v1/posts?author=usr_abc&published=true
# Sorting
GET /api/v1/posts?sort=-createdAt # Descending (- prefix)
GET /api/v1/users?sort=name # Ascending
# Combined
GET /api/v1/posts?author=usr_abc&status=published&sort=-createdAt&limit=20
interface ListPostsQuery {
author?: string
status?: 'draft' | 'published'
sort?: 'createdAt' | '-createdAt' | 'title' | '-title'
limit?: number
cursor?: string
}
async function listPosts(query: ListPostsQuery): Promise<PaginatedResponse<Post>> {
const conditions = []
if (query.author) {
conditions.push(eq(posts.authorId, query.author))
}
if (query.status) {
conditions.push(eq(posts.status, query.status))
}
const orderByColumn = query.sort?.startsWith('-')
? query.sort.slice(1)
: query.sort ?? 'createdAt'
const orderByDirection = query.sort?.startsWith('-') ? desc : asc
return await db.query.posts.findMany({
where: conditions.length > 0 ? and(...conditions) : undefined,
orderBy: orderByDirection(posts[orderByColumn]),
limit: query.limit ?? 20,
})
}
When changing the public API, choose status codes consistently with the existing contract and framework behavior:
# Success
200 OK # Successful GET, PUT, PATCH
201 Created # Successful POST (include Location header)
204 No Content # Successful DELETE, PUT with no response body
# Client Errors
400 Bad Request # Invalid request body/parameters
401 Unauthorized # Missing or invalid authentication
403 Forbidden # Valid auth, but lacks permission
404 Not Found # Resource doesn't exist
409 Conflict # Resource already exists, optimistic lock failure
422 Unprocessable # Validation error (semantic)
429 Too Many Requests # Rate limit exceeded
# Server Errors
500 Internal Server Error # Unexpected error
503 Service Unavailable # Temporary unavailability, retry later
Don't use envelopes for simple CRUD:
// ❌ Unnecessary wrapping
GET /api/v1/users/123
{
"success": true,
"data": { "id": "123", "name": "Alice" }
}
// ✅ Return resource directly
GET /api/v1/users/123
{
"id": "123",
"name": "Alice"
}
Use envelopes for pagination:
// ✅ Envelope needed for metadata
GET /api/v1/users?limit=20
{
"items": [...],
"nextCursor": "usr_xyz",
"hasMore": true
}
Use ISO 8601 format for all timestamps:
{
"createdAt": "2024-01-15T14:30:00.000Z", // ISO 8601 UTC
"updatedAt": "2024-01-16T09:15:30.123Z"
}
// In entities
toResponse(): UserResponse {
return {
...
createdAt: this.createdAt.toISOString(), // Date → ISO string
updatedAt: this.updatedAt.toISOString(),
}
}
Add idempotency keys only when the public API task requires replay-safe writes and the current implementation does not already provide that behavior:
// Request
POST /api/v1/transactions
Headers:
Idempotency-Key: txn_abc123xyz
Body:
{ "amount": 100, "from": "usr_123", "to": "usr_456" }
// Implementation
async function createTransaction(rq: CreateTransactionRequest, idempotencyKey: string) {
// Check if transaction with this key already exists
const existing = await db.query.transactions.findFirst({
where: eq(transactions.idempotencyKey, idempotencyKey),
})
if (existing) {
return TransactionEntity.fromRecord(existing) // Return existing
}
// Create new transaction
const transaction = TransactionEntity.fromRequest(rq, idempotencyKey)
return await transactionRepo.create(transaction)
}
When the task changes the public API:
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
8 skills in this plugin
/typescript-build-toolsTypeScript project tooling with Bun or pnpm, TypeScript 7, Vitest, Biome or Oxc (Oxlint and Oxfmt), and Turborepo. Use when setting up package.json scripts, running builds, typechecking, configuring tests, linting, formatting, or orchestrating monorepo development./typescript-drizzle-ormType-safe SQL with Drizzle ORM in TypeScript. Use when defining database schemas, writing queries, setting up relations, running migrations, working with PostgreSQL/MySQL/SQLite/Cloudflare D1/Durable Objects data layers, or integrating drizzle-orm/effect-postgres with @effect/sql-pg and Effect Layers./typescript-dynamodb-toolboxDynamoDB single-table design using dynamodb-toolbox v2. Use when creating entities, defining key patterns, designing GSIs, writing queries, implementing pagination, or working with any DynamoDB data layer in TypeScript projects.No slash command · from typescript
npx claudepluginhub martinffx/typescript-skills --plugin typescript