Build a typed Next.js route.ts handler with Zod validation, correct status codes, async params, caching behaviour and a fetch example.
You are a Next.js developer. Write an App Router Route Handler (`app/**/route.ts`) for the endpoint below. Follow the conventions of the installed Next.js version; for a framework-neutral implementation from an API contract, use a general endpoint implementation prompt instead.
## Inputs
Next.js version and runtime (Node.js or Edge): [NEXT_VERSION]
Route path and dynamic segments (for example app/api/projects/[id]/route.ts): [ROUTE_PATH]
HTTP method(s): [METHODS]
Purpose and business rules: [PURPOSE]
Request schema (params, query, body): [REQUEST_SCHEMA]
Response contract and error format used in the project: [RESPONSE_FORMAT]
Authentication requirement and the existing session helper: [AUTH]
Data access layer to call (ORM client, service function): [DATA_ACCESS]
## Instructions
1. Export only the requested method functions. In Next.js 15 and later, `context.params` is a Promise; await it.
2. Validate params, query and body with Zod using `safeParse`, and return 400 (or the project's validation status) with field errors.
3. Check authentication and authorization with [AUTH] inside the handler before data access.
4. Use the project's response format; if none is given, use `{ success: true, data }` and `{ success: false, error: { code, message } }`.
5. Return explicit statuses: 201 for creation, 404 for missing resources, 409 for conflicts, 500 with a generic message for unexpected errors (log the details server-side).
6. State the caching behaviour: GET handlers are dynamic by default since Next.js 15; opt into caching only if the data allows it.
7. Add CORS headers only if cross-origin browser clients are listed, including an OPTIONS handler if needed. Note where rate limiting would sit (proxy/middleware, platform, or the handler).
## Return
1. `route.ts` content.
2. Shared types or Zod schemas file if reused elsewhere.
3. Usage example with `fetch`, including error handling.
4. Test cases for valid input, invalid input, unauthenticated and not found.
## Acceptance checks
- Handler signatures match the stated Next.js version (async params where applicable).
- Validation happens before any database call.
- No secrets or stack traces in responses.
- The response shape matches [RESPONSE_FORMAT].Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Next.js: 15.5, Node.js runtime
Route: app/api/projects/[id]/tasks/route.ts
Methods: POST
Purpose: create a task in a project; title required (1-120 chars), dueDate optional ISO date not in the past
Response format: { success, data | error }
Auth: required; getSession() from @/lib/auth returns { userId } or null; user must be a project member
Data access: prisma.task.create, prisma.projectMember.findUniqueA route.ts exporting POST(request, { params }) that awaits params to read id, parses the JSON body with a Zod schema (title length 1-120, dueDate as an optional date that is not in the past), returns 401 without a session, 404 (or 403 per project policy) when the user is not a member, 201 with the created task, and a generic 500 on unexpected errors; no CORS headers; and a fetch example that reads the error object. Illustrative expectation, not a recorded model run.Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
IDEs
Models