Next.js Discord

Discord Forum

How to get ID from /api/something/[id]

Answered
Rhinelander posted this in #help-forum
Open in Discord
RhinelanderOP
I want to access /api/something/[id] the id part.

export default async function GET(req: Request, res: Response) {

}
Answered by B33fb0n3
you can get it via the params like:
app/items/[slug]/route.ts
export async function GET(
  request: Request,
  { params }: { params: { slug: string } }
) {
  const slug = params.slug // 'a', 'b', or 'c'
}
View full answer

8 Replies

RhinelanderOP
Is req.url the way?
you can get it via the params like:
app/items/[slug]/route.ts
export async function GET(
  request: Request,
  { params }: { params: { slug: string } }
) {
  const slug = params.slug // 'a', 'b', or 'c'
}
Answer
So in your case params.id
RhinelanderOP
Thank you once again!
happy to help once again 🙂
RhinelanderOP
I don't want to create new post but where would you suggest me to learn next.js validation. Like the proper way to handle let say trycatch and how to display that data in toast.