Create a Catch-all API Route in Next.js
📖 Scenario: You are building a Next.js app that needs to handle API requests for multiple dynamic paths under one route.For example, you want to catch requests like /api/products/123, /api/products/123/reviews, or /api/products/123/reviews/456 with a single API route file.
🎯 Goal: Build a catch-all API route in Next.js that captures all sub-paths under /api/products and returns the path segments as JSON.
📋 What You'll Learn
Create a catch-all API route file named
[...slug].ts inside app/api/products/Extract the path segments from the request parameters
Return a JSON response with the captured path segments
Use Next.js 14+ App Router conventions with a
GET handler💡 Why This Matters
🌍 Real World
Catch-all API routes let you handle many dynamic URL paths with one file, useful for building flexible APIs in Next.js apps.
💼 Career
Understanding catch-all routes is important for Next.js developers building scalable backend APIs that respond to various dynamic paths.
Progress0 / 4 steps