You can define dynamic parameters in your route paths by wrapping the parameter name in square brackets. These parameters are then accessible via req.params.
Example: A file at /api/products/[productId]/route.ts allows you to capture a productId from the URL.
Note: If using multiple parameters (e.g., /api/products/[productId]/variants/[variantId]/route.ts), each parameter must have a unique name. Duplicate parameter names will result in an error.
import type {
MedusaRequest,
MedusaResponse,
ProductService,
} from "@medusajs/medusa"
export async function GET(req: MedusaRequest, res: MedusaResponse) {
const { productId } = req.params
const productService: ProductService = req.scope.resolve("productService")
const product = await productService.retrieve(productId)
res.json({
product,
})
}