← ClaudeAtlas

api-patternslisted

Scaffold and review API routes following project conventions. Use when creating new API endpoints, modifying existing routes, or reviewing API code for correctness.
bean232323/elite-claude-playbook · ★ 0 · API & Backend · score 72
Install: claude install-skill bean232323/elite-claude-playbook
# API Route Patterns ## Before Creating a New Route 1. Check existing routes in your API directory for established patterns 2. Identify which auth method is needed (session-based, bearer token, API key) 3. Determine the validation schema needed for the request body ## Standard Route Structure Every API route should follow this pattern: ``` 1. Authenticate — verify the user/caller 2. Validate — parse and validate request body with schema 3. Business Logic — do the actual work 4. Response — return structured response with proper status code ``` Example (Next.js App Router): ```typescript import { NextRequest, NextResponse } from 'next/server'; import { z } from 'zod'; const requestSchema = z.object({ // define your fields }); export async function POST(req: NextRequest) { try { // 1. Authenticate const user = await getAuthenticatedUser(req); if (!user) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } // 2. Validate const body = await req.json(); const parsed = requestSchema.safeParse(body); if (!parsed.success) { return NextResponse.json( { error: 'Invalid request', details: parsed.error.flatten() }, { status: 400 } ); } // 3. Business logic const result = await performAction(parsed.data, user.id); // 4. Response return NextResponse.json(result, { status: 200 }); } catch (error) { console.error('[ROUTE_NAME] Error:', error); return NextRespons