Add API Endpoint
The OpenAPI spec at openapi/v1.yaml is the single source of truth. Define the endpoint there first, then implement.
New Endpoint Checklist
- Define in OpenAPI spec: Add path, request/response schemas, and
x-required-scopesinopenapi/v1.yaml - Create route handler:
app/api/v1/<resource>/route.ts - Auth: Use
validateApiKey()fromlib/api-keys.ts - Scopes: Use
hasScopes()fromlib/scopes.tsto enforcex-required-scopes - Business logic: Add query helpers in
lib/<resource>.ts, mutations inlib/actions/<resource>.ts - Database: If new tables needed, edit
lib/schema.tsand runbunx drizzle-kit generate(seedb-healthskill) - Tests: Add unit tests in
tests/unit/ - CLI command: Add corresponding command in
cli/cmd/<resource>.go(see docs/cli.md) - Gates: Run
./scripts/gates.sh
New Scope Checklist
If the endpoint needs a new scope:
- Add scope string to
lib/scopes.tsinALL_SCOPESandSCOPE_DESCRIPTIONS - Add to
openapi/v1.yamlunderx-all-scopesandcomponents.schemas.Scope.enum - Add
x-required-scopesto the endpoint inopenapi/v1.yaml - Enforce in route handler via
hasScopes() - Add tests
- Run
./scripts/gates.sh
Route Handler Pattern
import { NextRequest, NextResponse } from "next/server"
import { withApiKeyAuth } from "@/lib/api-v1-auth"
export const GET = withApiKeyAuth(
async (_request: NextRequest, { apiKey, user }) => {
// apiKey and user are already validated
// Business logic here
return NextResponse.json({ data: result })
},
{ requiredScopes: ["resource:read"] }
)
Architecture Layers
openapi/v1.yaml ← spec (source of truth)
app/api/v1/*/route.ts ← thin route handler
lib/api-v1-auth.ts ← auth wrapper
lib/scopes.ts ← scope definitions
lib/<resource>.ts ← queries and helpers
lib/actions/<resource>.ts ← mutations
tests/unit/ ← tests
cli/cmd/<resource>.go ← CLI command
Current Scopes
| Scope | Description |
|---|---|
keys:read | List and inspect API keys |
keys:write | Create and revoke API keys |
usage:read | Read usage metrics |
usage:write | Record usage events |
If You're Lost
- Stripe API patterns — our API design follows similar conventions
- OpenAPI 3.0 spec
- See docs/api.md for full endpoint reference
- See openapi/AGENTS.md for spec editing rules