Dynamic API Routes in Next.js
API routes support dynamic routes, and follow the same file naming rules used for pages.
For example, the API route pages/api/post/[pid].js has the following code:
export default function handler(req, res) {
const { pid } = req.query
res.end(`Post: ${pid}`)
}
Now, a request to /api/post/abc will respond with the text: Post: abc.
