---
name: nextjs-api-route-generator
description: "Generates a JavaScript Next.js API route that runs your existing middleware functions by name before the handler, validates the body against your schema, and returns the response you specified, with the file path shown. Use when a JavaScript route must run named existing middleware functions in order before the handler and still validate the body and return a set shape. Do not use for TypeScript routes, standalone validation middleware, or debugging a route that already exists. Related topics: coding, next.js."
---

# Next.js API Route Generator

Generates a JavaScript Next.js API route that runs your existing middleware functions by name before the handler, validates the body against your schema, and returns the response you specified, with the file path shown.

## When to use

Use this skill when a JavaScript route must run named existing middleware functions in order before the handler and still validate the body and return a set shape. Do not use it for TypeScript routes, standalone validation middleware, or debugging a route that already exists.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Derive the file path from the route path and put it in the first comment line.
3. Import each named middleware and run them in the given order through a promise-based runMiddleware helper that stops the chain on failure.
4. Add the 405 method guard and the 401 auth check after the chain when authentication is required.
5. Validate presence and type of every body field against the schema and respond 400 with the offending fields.
6. Build the response from the response schema with the status code matched to the method and output one javascript code block.

## Inputs

- **Route path** (free text, e.g. "/api/example")
- **HTTP method** (choose one: GET, POST, PUT, DELETE, PATCH)
- **Request body schema (JSON)** (longer free text)
- **Response schema (JSON)** (longer free text)
- **Authentication required** (choose one: Yes, No)
- **Middleware functions (comma separated)** (free text, e.g. "logger and cors")

## Output

One markdown code block tagged javascript: a file path comment, imports, the runMiddleware helper, the handler with chain, guards, validation, and response, and the default export.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Next.js API Route Generator

Generate a Next.js API route in JavaScript that uses my existing middleware.

- Route path: {{Route path}}
- HTTP method: {{HTTP method}}
- Request body schema (JSON): {{Request body schema (JSON)}}
- Response schema (JSON): {{Response schema (JSON)}}
- Authentication required: {{Authentication required}}
- Middleware functions (comma separated): {{Middleware functions (comma separated)}}

Derive the file path from the route path (pages/api followed by the path, with bracketed segments for dynamic parts) and show it as the first comment line. Treat each named middleware as an existing function of the shape (req, res, next) imported from the project's middleware folder, and run them in the order given through a small runMiddleware helper that turns the callback style into a promise, so one failing middleware stops the chain and sends its own response. Guard the method with 405 and an Allow header. When authentication is required, place the auth check after the middleware chain and before validation, returning 401. Validate the request body against the schema by checking presence and type of each field and answer 400 listing the offending fields. Build the response object field by field from the response schema with the right status code (201 for a POST that creates, 200 otherwise, 204 for a DELETE with no body). Return one markdown code block tagged javascript that starts with the file path comment and contains the full source.

## Pitfalls

- Calling callback-style middleware without awaiting a promise wrapper, so the handler responds before the middleware finishes.
- Continuing the chain after a middleware already sent a response, which raises headers already sent.
- Mapping a dynamic route like /api/users/:id to a flat file instead of the bracketed segment filename.
