tezvyn:

How do you create a dynamic API route and access its parameter?

AI-drafted, machine-checkedSource: nextjs.orgintermediate

This tests Next.js routing conventions and dynamic parameter extraction. Create /api/products/[productId]/route.js, export a GET handler, and read productId from params. Red flag: mixing App Router params with Pages Router req.query.

WHAT THIS TESTS: This question tests your knowledge of Next.js file-system routing for API endpoints and how the framework passes dynamic path segments to route handlers. The interviewer wants to see if you know the App Router convention, understand the difference between route parameters and query strings, and can describe the handler signature accurately.

A GOOD ANSWER COVERS: First, explain the file naming convention: create a folder structure like api/products/[productId] with a route.js or route.ts file inside it. Second, describe the handler export: you export async functions for HTTP verbs like GET, POST, PUT, or DELETE. Third, explain parameter access: the handler receives a request object as the first argument and a context object as the second argument, and the context object contains params which holds the dynamic values. Fourth, mention version awareness: in Next.js 15 and later, params is a Promise that must be awaited, while in earlier versions it is a plain object, so a senior candidate should note the difference. Fifth, add validation: a strong answer notes that you should validate or sanitize productId before using it in a database query.

COMMON WRONG ANSWERS: A major red flag is describing the Pages Router pattern for API routes, such as exporting a default handler and accessing req.query.productId, because the App Router uses a completely different file convention and handler signature. Another mistake is saying you extract the value from the request URL manually or from searchParams when the question specifically asks about dynamic route segments. Confusing the route file name page.js with route.js is also a signal that the candidate has not built App Router APIs.

LIKELY FOLLOW-UPS: The interviewer may ask how you would handle nested dynamic routes like /api/products/[productId]/reviews/[reviewId]. They might also ask how to generate static responses for dynamic API routes at build time, or how to validate the productId using a schema library like Zod inside the handler. Another common follow-up is asking about the difference between Route Handlers and Server Actions in the App Router.

ONE CONCRETE EXAMPLE: Imagine an e-commerce product API. You create app/api/products/[productId]/route.ts and export an async function GET that takes request and context. Inside the function, you await context.params if using Next.js 15 to get the productId, then validate that it is a non-empty string or matches a UUID pattern before fetching the product from your database and returning a JSON response.

Read the original → nextjs.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.