Next.js Discord

Discord Forum

Can't get URL parameters in API route

Answered
Chinese Alligator posted this in #help-forum
Open in Discord
Chinese AlligatorOP
Next version 14.2.5

/app/api/item/[itemId]/message/[messageId]/route.ts

req.nextUrl -> Property 'nextUrl' does not exist on type 'NextApiRequest'. (One suggested method).
I have looked at the req. object, and there are no properties which have the itemId or messageId on.

Other options suggested have been to manually parse the URL and extract parameters through matching, but this doesn't seem the correct way?
I would assume NextJS API has built in URL parameter extraction based on the the folderisation of the routes.

This is my code:
export async function POST(req: NextApiRequest, res: NextApiResponse) {

}


I was using this format on another route, which worked to get the params, but this then didn't work with Clerk middleware:
export async function GET(req: Request, { params }: any) {
// params worked here
}
Answered by joulev
NextApiRequest and Request are two entirely different types, incompatible with each other
View full answer

4 Replies