#How do I get req query params in a GET route handler?

1 messages · Page 1 of 1 (latest)

valid iron
#

@thorny finch How do I get req query params in a GET route handler?

thorny finchBOT
#

Hi @valid iron. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To get query parameters in a GET route handler, you can use the NextRequest object from next/server. Here's an example of how to access query parameters in a route handler:

import type { NextRequest } from 'next/server';

export const config = {
  runtime: 'edge',
};

export default async function handler(req: NextRequest) {
  const { searchParams } = new URL(req.url);
  const email = searchParams.get('email');
  return new Response(email);
}

In this example, the searchParams object is used to get the value of the email query parameter from the request URL. You can replace email with the name of the query parameter you want to access.

Source: Edge API Routes - Query Parameters (https://nextjs.org/docs/api-routes/edge-api-routes)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Next.js supports API Routes, which allow you to build your API without leaving your Next.js app. Learn how it works here.

#

@valid iron kapa.ai is still learning and improving, please let us how it did by reacting below

valid warren