Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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?
AproductId[].js
BproductId.js
Cproduct-[id].js
D[productId].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?
Areq.body
Breq.query
Creq.params
Dreq.headers
✗ 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?
A/api/posts
B/api/posts/postId
C/api/posts/42
D/api/posts/[postId]
✗ 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?
AYes, by nesting folders with dynamic names
BNo, only one dynamic segment is allowed
CYes, but only separated by dashes
DNo, dynamic routes are not supported in API
✗ 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?
AThey allow handling many similar URLs with one file
BThey improve CSS styling
CThey speed up image loading
DThey disable server-side rendering
✗ 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.
Practice
(1/5)
1. What is the purpose of using square brackets in Next.js API route filenames like [id].js?
easy
A. To create dynamic API routes that capture parts of the URL
B. To mark the file as a static API route
C. To import external modules dynamically
D. To define middleware for the API route
Solution
Step 1: Understand file naming in Next.js API routes
Square brackets in filenames like [id].js indicate a dynamic segment in the URL path.
Step 2: Recognize the effect on routing
This allows the API route to capture the value in that part of the URL and use it inside the handler.
Final Answer:
To create dynamic API routes that capture parts of the URL -> Option A
Quick Check:
Dynamic routes use brackets = D [OK]
Hint: Square brackets in filenames mean dynamic URL parts [OK]
Common Mistakes:
Thinking brackets mark static routes
Confusing with dynamic imports
Assuming brackets define middleware
2. Which of the following is the correct way to access the dynamic parameter id inside a Next.js API route handler in [id].js?
easy
A. const id = req.params.id;
B. const id = req.body.id;
C. const id = req.query.id;
D. const id = req.route.id;
Solution
Step 1: Recall how Next.js passes dynamic route params
Next.js provides dynamic route parameters inside req.query in API routes.
Step 2: Identify the correct syntax
Accessing id is done by req.query.id, not req.params or others.
Final Answer:
const id = req.query.id; -> Option C
Quick Check:
Dynamic params in API routes = req.query [OK]
Hint: Use req.query to get dynamic route params in Next.js API [OK]
Common Mistakes:
Using req.params instead of req.query
Trying to get params from req.body
Using incorrect property like req.route
3. Given the API route file pages/api/user/[userId].js with this handler:
export default function handler(req, res) {
const { userId } = req.query;
res.status(200).json({ message: `User ID is ${userId}` });
}
What will be the JSON response when a client requests /api/user/42?
medium
A. 404 Not Found
B. {"message":"User ID is userId"}
C. {"message":"User ID is undefined"}
D. {"message":"User ID is 42"}
Solution
Step 1: Extract dynamic parameter from URL
The URL /api/user/42 matches the dynamic route [userId].js, so userId is "42".
Step 2: Check the handler response
The handler reads userId from req.query and returns JSON with message including that value.
Final Answer:
{"message":"User ID is 42"} -> Option D
Quick Check:
Dynamic param value used in response = A [OK]
Hint: Dynamic param in URL becomes req.query value [OK]
Common Mistakes:
Expecting literal string 'userId' instead of value
Assuming undefined if param missing
Thinking route returns 404 for dynamic routes
4. Consider this Next.js API route file named pages/api/product/[pid].js with the handler:
export default function handler(req, res) {
const pid = req.query.pid;
if (!pid) {
res.status(400).json({ error: "Product ID missing" });
}
res.status(200).json({ productId: pid });
}
What is the bug in this code?
medium
A. It should use res.send instead of res.json
B. It does not return after sending 400 response, causing headers to be sent twice
C. It uses req.query.pid instead of req.params.pid
D. The file name should be [pid].ts instead of .js
Solution
Step 1: Analyze the conditional response
If pid is missing, the code sends a 400 response but does not stop execution.
Step 2: Understand HTTP response behavior
Without a return after sending 400, the code continues and tries to send a 200 response, causing an error.
Final Answer:
It does not return after sending 400 response, causing headers to be sent twice -> Option B
Quick Check:
Return after error response to avoid double send [OK]
Hint: Always return after sending error response in API handlers [OK]
Common Mistakes:
Ignoring missing return after res.status(400).json()
Confusing req.query with req.params
Thinking res.send is required over res.json
5. You want to create a Next.js API route that handles multiple dynamic segments like /api/order/[orderId]/item/[itemId].js. How should you structure the files and access both orderId and itemId inside the handler?
hard
A. Create nested folders: pages/api/order/[orderId]/item/[itemId].js and access via req.query.orderId and req.query.itemId
B. Create a single file pages/api/order-item.js and parse URL manually
C. Use query parameters like /api/order?orderId=1&itemId=2 and access req.query
D. Create a file pages/api/order/[orderId]-[itemId].js and access req.query as an array
Solution
Step 1: Understand nested dynamic routes in Next.js
Next.js supports nested folders with dynamic segments using square brackets for each segment.
Step 2: Access multiple dynamic params in handler
Both orderId and itemId appear in req.query as separate keys.
Final Answer:
Create nested folders: pages/api/order/[orderId]/item/[itemId].js and access via req.query.orderId and req.query.itemId -> Option A
Quick Check:
Nested folders with brackets = multiple params in req.query [OK]
Hint: Use nested folders with brackets for multiple dynamic params [OK]
Common Mistakes:
Trying to parse multiple params in one filename
Using query string instead of dynamic routes
Assuming req.query returns array for multiple params