How to access dynamic API url param using app router
Answered
Common Pochard posted this in #help-forum
Common PochardOP
Seems like a pretty basic question, but I can't find the answer anywhere.
I have an api file
How do I access
On a page I can just do
Looking for the equivalent of
I have an api file
/api/something/[id]/route.ts
How do I access
id
?On a page I can just do
params.id
but how does this work with API handlers? There's no params property on NextRequest
Looking for the equivalent of
req.query.id
from the pages directoryAnswered by Common Pochard
ok, it's the second argument to APIMETHOD(request: NextRequest);
export async function GET(req, { params }) {
const id = params.id
}
7 Replies
Common PochardOP
I know I can do
req.url.split('/').pop();
but that is absolutely heinousChien Français Blanc et Noir
i have the same question!
Common PochardOP
ok, it's the second argument to APIMETHOD(request: NextRequest);
export async function GET(req, { params }) {
const id = params.id
}
Answer
Chien Français Blanc et Noir
thank you!
Common PochardOP
documentation is horrible rn, haha
Chien Français Blanc et Noir
yea, i agree
im completely lost for some topics