Get the FREE Ultimate OpenClaw Setup Guide →

nextjs-route-generator

npx machina-cli add skill Nembie/claude-code-skills/nextjs-route-generator --openclaw
Files (1)
SKILL.md
6.7 KB

Next.js Route Generator

Before generating any output, read config/defaults.md and adapt all patterns, imports, and code examples to the user's configured stack.

Generation Process

  1. Determine the route path and HTTP methods needed
  2. Generate route handler with proper exports (GET, POST, PUT, PATCH, DELETE)
  3. Add Zod schemas for request validation
  4. Include structured error handling
  5. Export TypeScript types for frontend consumption

Route Structure

Place routes in app/api/ following Next.js App Router conventions:

  • app/api/users/route.ts/api/users
  • app/api/users/[id]/route.ts/api/users/:id
  • app/api/posts/[postId]/comments/route.ts/api/posts/:postId/comments

Standard Route Template

import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';

// ============ Schemas ============

const createSchema = z.object({
  name: z.string().min(1).max(100),
  email: z.string().email(),
});

const updateSchema = createSchema.partial();

const querySchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().min(1).max(100).default(20),
  search: z.string().optional(),
});

// ============ Types ============

export type CreateInput = z.infer<typeof createSchema>;
export type UpdateInput = z.infer<typeof updateSchema>;
export type QueryParams = z.infer<typeof querySchema>;

// ============ Helpers ============

function errorResponse(message: string, status: number, details?: unknown) {
  return NextResponse.json(
    { error: message, details },
    { status }
  );
}

function parseSearchParams(request: NextRequest, schema: z.ZodSchema) {
  const params = Object.fromEntries(request.nextUrl.searchParams);
  return schema.safeParse(params);
}

// ============ Handlers ============

export async function GET(request: NextRequest) {
  const parsed = parseSearchParams(request, querySchema);

  if (!parsed.success) {
    return errorResponse('Invalid query parameters', 400, parsed.error.flatten());
  }

  const { page, limit, search } = parsed.data;

  // TODO: Implement data fetching
  const items = [];
  const total = 0;

  return NextResponse.json({
    data: items,
    pagination: {
      page,
      limit,
      total,
      totalPages: Math.ceil(total / limit),
    },
  });
}

export async function POST(request: NextRequest) {
  let body: unknown;

  try {
    body = await request.json();
  } catch {
    return errorResponse('Invalid JSON body', 400);
  }

  const parsed = createSchema.safeParse(body);

  if (!parsed.success) {
    return errorResponse('Validation failed', 400, parsed.error.flatten());
  }

  // TODO: Implement creation logic
  const created = { id: 1, ...parsed.data };

  return NextResponse.json({ data: created }, { status: 201 });
}

Dynamic Route Template

For [id] routes (app/api/resource/[id]/route.ts):

import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';

const paramsSchema = z.object({
  id: z.coerce.number().int().positive(),
});

const updateSchema = z.object({
  name: z.string().min(1).max(100).optional(),
  email: z.string().email().optional(),
});

type RouteContext = {
  params: Promise<{ id: string }>;
};

export async function GET(request: NextRequest, context: RouteContext) {
  const params = await context.params;
  const parsed = paramsSchema.safeParse(params);

  if (!parsed.success) {
    return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
  }

  // TODO: Fetch by ID
  const item = null;

  if (!item) {
    return NextResponse.json({ error: 'Not found' }, { status: 404 });
  }

  return NextResponse.json({ data: item });
}

export async function PUT(request: NextRequest, context: RouteContext) {
  const params = await context.params;
  const parsedParams = paramsSchema.safeParse(params);

  if (!parsedParams.success) {
    return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
  }

  let body: unknown;
  try {
    body = await request.json();
  } catch {
    return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 });
  }

  const parsedBody = updateSchema.safeParse(body);

  if (!parsedBody.success) {
    return NextResponse.json(
      { error: 'Validation failed', details: parsedBody.error.flatten() },
      { status: 400 }
    );
  }

  // TODO: Update logic
  const updated = { id: parsedParams.data.id, ...parsedBody.data };

  return NextResponse.json({ data: updated });
}

export async function DELETE(request: NextRequest, context: RouteContext) {
  const params = await context.params;
  const parsed = paramsSchema.safeParse(params);

  if (!parsed.success) {
    return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
  }

  // TODO: Delete logic

  return new NextResponse(null, { status: 204 });
}

Middleware Pattern

For routes requiring authentication:

import { auth } from '@/lib/auth';

export async function GET(request: NextRequest) {
  const session = await auth();

  if (!session) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  // Proceed with authenticated request
}

Error Handling Patterns

Database Errors

import { Prisma } from '@prisma/client';

try {
  // Database operation
} catch (error) {
  if (error instanceof Prisma.PrismaClientKnownRequestError) {
    if (error.code === 'P2002') {
      return errorResponse('Resource already exists', 409);
    }
    if (error.code === 'P2025') {
      return errorResponse('Resource not found', 404);
    }
  }
  throw error; // Re-throw unexpected errors
}

Global Error Boundary

Unexpected errors are caught by Next.js and return 500. Log them in production:

console.error('Unhandled API error:', error);

Response Conventions

  • 200: Successful GET/PUT/PATCH
  • 201: Successful POST (resource created)
  • 204: Successful DELETE (no content)
  • 400: Bad request / validation error
  • 401: Unauthorized
  • 403: Forbidden
  • 404: Not found
  • 409: Conflict (duplicate)
  • 500: Internal server error

Integration Check

After generating a route, verify that: the Zod schema matches the expected request body, the response type is explicitly defined, error responses use consistent format across all generated routes, and the route handles all specified HTTP methods. If generating multiple routes, ensure shared types are extracted to a common types file.

Asset

See assets/route-template/route.ts for a minimal starter template.

Source

git clone https://github.com/Nembie/claude-code-skills/blob/main/skills/nextjs-route-generator/SKILL.mdView on GitHub

Overview

Scaffold Next.js App Router API routes with Zod validation, error handling, and TypeScript types. This skill helps you quickly create REST endpoints or CRUD backends by generating route handlers, validation schemas, and typed exports for frontend use.

How This Skill Works

It analyzes the needed route path and HTTP methods, then generates a route.ts with GET, POST, PUT, PATCH, and DELETE exports. It adds Zod schemas for request validation, a structured errorResponse, and exports TypeScript types for frontend consumption.

When to Use It

  • When asked to create a new API route in the Next.js App Router under app/api
  • When scaffolding REST endpoints or CRUD operations (create, read, update, delete)
  • When you need robust input validation using Zod schemas
  • When you require centralized, structured error handling across routes
  • When you want to export TypeScript types for frontend integration

Quick Start

  1. Step 1: Review config/defaults.md and adapt patterns/imports to your stack
  2. Step 2: Create app/api/users/route.ts and implement GET/POST with Zod schemas
  3. Step 3: Export types (CreateInput, UpdateInput, QueryParams) and reuse in frontend

Best Practices

  • Follow the Next.js App Router route.ts export pattern (GET, POST, PUT, PATCH, DELETE)
  • Use Zod schemas for all request validation (createSchema, updateSchema, querySchema)
  • Implement a centralized errorResponse helper for consistent errors
  • Export TypeScript types (CreateInput, UpdateInput, QueryParams) for frontend use
  • Structure dynamic routes with [id] patterns and parse route params safely

Example Use Cases

  • GET /api/users with pagination and optional search using querySchema
  • POST /api/users to create a new user with createSchema validation
  • GET /api/users/[id] to fetch a specific user by ID
  • PUT /api/users/[id] to update a user with updateSchema validation
  • GET /api/posts/[postId]/comments to list comments for a post

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers