Next.js Discord

Discord Forum

How do you get Query params in Next13 route.ts file?

Answered
American Wirehair posted this in #help-forum
Open in Discord
American WirehairOP
https://nextjs.org/docs/app/api-reference/file-conventions/route#parameters

I am unsure of how you are meant to the get queryParams on the server site /api/route.ts file? I can find no mention of it? I am using the /app router
Answered by joulev
async function GET(request: NextRequest) {
  console.log(request.nextUrl.searchParams);
}
View full answer

4 Replies

Answer
@joulev ts async function GET(request: NextRequest) { console.log(request.nextUrl.searchParams); }
American WirehairOP
Okay a follow up question is if I am using zod to validate the query param input, the searchParams is a URLSearchParams. so how would I then convert that to somthing zod can use?
@joulev https://stackoverflow.com/q/8648892
American WirehairOP
Thanks alot, I really appreciate it