Next.js › Route Handlers (API Route.js)
How do you access dynamic route parameters inside a route handler defined with a dynamic segment?
IntermediateAnswer
The second argument passed to your route handler function contains a params object, which you await to access the dynamic segment values captured from the URL, letting you build endpoints like fetching a specific item by its id.
// app/api/products/[id]/route.js
export async function GET(request, { params }) {
const { id } = await params;
const product = await getProduct(id);
return Response.json(product);
}Real-world example A product API endpoint reads the product id directly from the dynamic route segment, letting it fetch and return the correct product details for any given id in the URL.