---
name: api-route-generator
description: "Produces a complete Express route file you can mount and run: imports, the middleware you chose in the right order, request validation against your schema, and a handler stub that returns the response shape. Use when a new Express.js endpoint needs a route file with middleware, validation, and a handler stub that matches a request and response schema. Do not use for debugging an existing route, generating routes for other frameworks, or writing the business logic inside the handler. Related topics: coding, api."
---

# API Route Generator

Produces a complete Express route file you can mount and run: imports, the middleware you chose in the right order, request validation against your schema, and a handler stub that returns the response shape.

## When to use

Use this skill when a new Express.js endpoint needs a route file with middleware, validation, and a handler stub that matches a request and response schema. Do not use it for debugging an existing route, generating routes for other frameworks, or writing the business logic inside the handler.

## 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. List the imports the chosen middleware and validator need and nothing more, so the file runs without unused dependencies.
3. Order the middleware logging, rate limiting, auth, validation, and write a one-line comment on what each guards.
4. Implement request validation against the schema and return 400 with field-level errors; return 401 before validation when auth is required.
5. Write the async handler with error forwarding to next(err), a TODO for the business logic, and a response literal that matches the response schema field for field.
6. Append a mount example for app.js and confirm the file exports a single router.

## Inputs

- **HTTP method** (choose one: GET, POST, PUT, PATCH, DELETE)
- **Endpoint path** (free text, e.g. "/resources/:id")
- **Request body or query schema (JSON Schema)** (longer free text)
- **Response body schema (JSON Schema)** (longer free text)
- **Middleware to include** (choose any: Auth, Validation, Logging, Rate limiting, CORS)
- **Authentication required** (choose one: Yes, No)
- **Language** (choose one: JavaScript (CommonJS), JavaScript (ESM), TypeScript)

## Output

A single fenced code block with the complete route file (imports, middleware, validation, handler, export), followed by a two-line snippet that mounts it in app.js.

## Prompt template

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

### API Route Generator

Write a ready-to-mount Express.js route from these parameters.

- HTTP method: {{HTTP method}}
- Endpoint path: {{Endpoint path}}
- Request body or query schema (JSON Schema): {{Request body or query schema (JSON Schema)}}
- Response body schema (JSON Schema): {{Response body schema (JSON Schema)}}
- Middleware to include: {{Middleware to include}}
- Authentication required: {{Authentication required}}
- Language: {{Language}}

Produce one file that exports an Express router. Start with the imports it actually needs, then the middleware in the order requests must pass through them: logging first, then rate limiting, then auth, then validation, so an unauthenticated caller never reaches the validator. Implement validation against the request schema with a lightweight validator (zod or express-validator) and return 400 with the field errors; when auth is required, return 401 before any other work. Write the handler as an async function wrapped so thrown errors reach next(err), with a clearly marked TODO where the business logic goes and a response object that matches the response schema exactly. Add a comment above each middleware that says what it guards. Finish with a two-line usage example that mounts the router in app.js.

## Pitfalls

- Running validation before auth, which lets anonymous callers probe the schema and wastes work on requests that will be rejected.
- Returning a response shape that drifts from the schema, such as a bare array where the schema says an object with a data key.
- Forgetting to forward async errors to next(err), so a thrown error leaves the request hanging instead of returning 500.
