Recall & Review
beginner
What is a dynamic API route in Next.js?
A dynamic API route is an API endpoint that uses file names with square brackets (e.g., [id].js) to capture parts of the URL as parameters, allowing the API to respond differently based on the URL segment.
Click to reveal answer
beginner
How do you define a dynamic API route file for a user ID in Next.js?
You create a file named
pages/api/users/[id].js. The [id] part captures the user ID from the URL, which you can access in the handler function.Click to reveal answer
beginner
How do you access the dynamic parameter inside a Next.js API route handler?
Inside the API route handler, you access the parameter via <code>req.query</code>. For example, if the file is <code>[id].js</code>, you get the ID with <code>const { id } = req.query;</code>.Click to reveal answer
beginner
What happens if you request
/api/users/123 when you have a dynamic API route [id].js?Next.js matches the URL segment '123' to the
[id] parameter. The API handler receives req.query.id === '123' and can use it to return data specific to user 123.Click to reveal answer
intermediate
Can you have multiple dynamic parameters in a Next.js API route? How?
Yes. You can create nested dynamic routes like
pages/api/users/[userId]/posts/[postId].js. Both userId and postId are available in req.query.Click to reveal answer
How do you name a file to create a dynamic API route for a product ID in Next.js?
✗ Incorrect
Dynamic API route files use square brackets around the parameter name, like [productId].js.
Where do you find the dynamic parameter value inside a Next.js API route handler?
✗ Incorrect
Next.js provides dynamic route parameters inside req.query in API routes.
What URL matches the API route file
pages/api/posts/[postId].js?✗ Incorrect
The URL /api/posts/42 matches the dynamic route where postId is '42'.
Can you use multiple dynamic segments in one API route in Next.js?
✗ Incorrect
You can nest folders with dynamic names like [userId]/[postId].js to have multiple dynamic segments.
What is the benefit of using dynamic API routes in Next.js?
✗ Incorrect
Dynamic API routes let you handle many URLs that differ by parameters using one route file.
Explain how to create and use a dynamic API route in Next.js to fetch data based on a URL parameter.
Think about how the file name relates to the URL and how you get the parameter inside the code.
You got /4 concepts.
Describe how you can handle multiple dynamic parameters in Next.js API routes and how to access them.
Consider a URL with two variable parts and how the folder structure matches it.
You got /3 concepts.