Next.js Discord

Discord Forum

How to access dynamic API url param using app router

Answered
Common Pochard posted this in #help-forum
Open in Discord
Avatar
Common PochardOP
Seems like a pretty basic question, but I can't find the answer anywhere.

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 directory
Answered by Common Pochard
ok, it's the second argument to APIMETHOD(request: NextRequest);

export async function GET(req, { params }) {
  const id = params.id
}
View full answer

7 Replies

Avatar
Common PochardOP
I know I can do req.url.split('/').pop(); but that is absolutely heinous
Avatar
Chien Français Blanc et Noir
i have the same question!
Avatar
Common PochardOP
ok, it's the second argument to APIMETHOD(request: NextRequest);

export async function GET(req, { params }) {
  const id = params.id
}
Answer
Avatar
Chien Français Blanc et Noir
thank you!
Avatar
Common PochardOP
documentation is horrible rn, haha
Avatar
Chien Français Blanc et Noir
yea, i agree
im completely lost for some topics