Introduction: Making Your API Dynamic

Welcome back! In the previous lesson, you learned how to create a basic API route in Next.js and return a simple JSON response. That was a great first step. Now, let’s take things further by making your API more dynamic and interactive.

In real-world applications, APIs often need to respond differently based on the data sent by the client. This is where reading request data comes in. By learning how to read information from the request — such as query parameters in the URL — you can personalize your API’s responses and make your endpoints much more useful.

In this lesson, you will learn how to read query parameters from incoming requests and use them to customize your API’s output.


How Client Data Reaches Your API (GET vs POST)

When a client (like a browser, mobile app, or frontend page) sends a request to your API, it can include data in different ways depending on the request type:

  • Query Parameters (used in GET requests): These appear in the URL (like /api/user?id=123). They’re visible in the address bar and are often used for filtering or requesting specific items.
  • Request Body (used in POST, PUT, or PATCH requests): This is a more private and flexible way to send data — like login forms or file uploads. The body isn’t visible in the URL and allows you to send structured data like JSON.
  • Headers: These send metadata about the request, like what format you expect in the response, or which user is making the request (via tokens).

In this lesson, we’re working with GET requests, so we’ll focus on query parameters — but keep in mind that there are other ways your API can receive data depending on the method used.


What are Query Parameters

The word "query" comes from the idea that you’re asking something from the server — you're making a request with specific parameters that define your "query."

Query parameters are placed in the URL after a question mark (?) and are often used with GET requests to filter, sort, or customize the response from the API. Each key-value pair is separated by an ampersand (&), like this:

In this example, category=shoes and limit=10 tell the server to only return 10 items in the "shoes" category. Query parameters are easy to use for simple input data that doesn't require authentication or large payloads.


Quick Recap: Basic API Route Structure

Before we dive into reading request data, let’s quickly remind ourselves how a basic API route is set up in Next.js. Here’s a simple example, similar to what you saw in the last lesson:

This code creates a GET endpoint at /api/hello that always returns the same message. In this lesson, we’ll build on this by making the response change based on the request.


Accessing Query Parameters With NextRequest

Now, let’s talk about query parameters. Query parameters are extra bits of information you can add to a URL. For example:

Here, name and language are query parameters. They allow the client to send information to your API.

In Next.js, you can use the NextRequest object to access these parameters. Let’s look at how to do this:

Explanation:

  • request.nextUrl.searchParams is a convenience provided by Next.js. It converts the incoming request URL into a URL object automatically. It gives you access to all the query parameters in the URL.
  • searchParams.get('name') tries to get the value of the name parameter. If it’s not there, it returns null.
  • The same goes for language.

⚠️ Note: Parameter keys are case-sensitive. That means /api/hello?Language=es will not return the expected greeting, because 'Language' is not the same as 'language'.

Personalizing The API Response

Now that you know how to read query parameters, let’s use them to make your API response more personal and interesting. You can use the values from the request to change the message you send back.

Here’s a more complete example, based on the outcome for this lesson:

searchParams behaves like a special map of all the query parameters in the URL. It comes from the URLSearchParams interface, which provides helpful methods like:

  • .get('key') – returns the value for a key (or null if not found)
  • .has('key') – returns true if the key exists
  • .keys() – returns all keys

You can think of it like a JavaScript Map, but for the query part of a URL. For example:

Summary And Next Steps

In this lesson, you learned how to read query parameters from incoming requests using the NextRequest object in Next.js. You also saw how to use these parameters to personalize your API’s response, making it more dynamic and useful.

You are now ready to practice these skills! In the next set of exercises, you’ll get hands-on experience reading request data and customizing your API responses. This is a key step in building real-world APIs, so take your time and experiment with different parameters. Good luck!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal