# API Patterns

API design, versioning, testing, schema validation, and contract testing patterns for...

- Repository: https://github.com/vibeeval/vibecosystem (541 stars)
- Category: Design
- License: MIT
- Last updated: 2026-08-08
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add vibeeval/vibecosystem --skill api-patterns -g -a claude-code -y`
Codex: `npx skills add vibeeval/vibecosystem --skill api-patterns -g -a codex -y`
Cursor: `npx skills add vibeeval/vibecosystem --skill api-patterns -g -a cursor -y`
Gemini CLI: `npx skills add vibeeval/vibecosystem --skill api-patterns -g -a gemini-cli -y`

## Description

API design, versioning, testing, schema validation, and contract testing patterns for REST and GraphQL APIs.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# API Patterns

REST and GraphQL API design patterns for consistent, versioned, and well-tested interfaces.

## API Versioning

### URL-Based Versioning

```typescript
// routes/v1/markets.ts
// routes/v2/markets.ts
// URL: /api/v1/markets, /api/v2/markets

// Express router setup
import { Router } from 'express'

const v1Router = Router()
const v2Router = Router()

app.use('/api/v1', v1Router)
app.use('/api/v2', v2Router)

// Deprecation header middleware
function deprecationWarning(version: string, sunsetDate: string) {
  return (_req: Request, res: Response, next: NextFunction) => {
    res.setHeader('Deprecation', 'true')
    res.setHeader('Sunset', sunsetDate)
    res.setHeader('Link', `</api/v${parseInt(version) + 1}>; rel="successor-version"`)
    next()
  }
}

v1Router.use(deprecationWarning('1', 'Sat, 01 Jan 2027 00:00:00 GMT'))
```

### Header-Based Versioning

```typescript
// Accept: application/vnd.api+json;version=2
function versionMiddleware(req: Request, res: Response, next: NextFunction) {
  const accept = req.headers['accept'] || ''
  const match = accept.match(/version=(\d+)/)
  req.apiVersion = match ? parseInt(match[1]) : 1
  next()
}
```

## Schema Validation with Zod

### Request + Response Validation

```typescript
import { z } from 'zod'

// Request schema
const CreateMarketSchema = z.object({
  name: z.string().min(1).max(200),
  description: z.string().max(2000).optional(),
  category: z.enum(['sports', 'politics', 'crypto', 'tech']),
  closeAt: z.string().datetime(),
  initialLiquidity: z.number().positive().max(1_000_000)
})

// Response schema - strip internal fields
const MarketResponseSchema = z.object({
  id: z.string().uuid(),
  name: z.string(),
  category: z.string(),
  status: z.enum(['open', 'closed', 'resolved']),
  volume: z.number(),
  createdAt: z.string().datetime()
})

type CreateMarketDto = z.infer<typeof CreateMarketSchema>
type MarketResponse = z.infer<typeof MarketResponseSchema>

The rest: https://github.com/vibeeval/vibecosystem/blob/HEAD/skills/api-patterns/SKILL.md
