How to access query parameters in Netlify functions

How to access query parameters in Netlify functions

Perttu Lähteenlahti
3 min read
netlify-functionsquery-parametersserverlesstypescript
Share:

Why query parameters are useful in Netlify Functions

Query parameters are one of the simplest ways to pass data to a backend endpoint. You have probably used them many times without thinking about it.

For example, a URL like this:

/api/signup?email=test@example.com

contains extra information that the server can read and act on.

When you build Netlify Functions, accessing query parameters is very common. You might use them for filtering data, passing IDs, or handling small actions without needing a full request body.

Where query parameters live in Netlify Functions

In a Netlify Function, all request data is passed into the handler function as an event object.

Query parameters are available on:

event.queryStringParameters

This object contains all query values from the URL.

Basic example in TypeScript

Here is a simple Netlify Function written in TypeScript that reads a query parameter called email:

import { Handler } from "@netlify/functions"

export const handler: Handler = async (event) => {
  const email = event.queryStringParameters?.email

  return {
    statusCode: 200,
    body: JSON.stringify({
      receivedEmail: email ?? "no email provided"
    })
  }
}

If you call this function using a URL like:

https://your-site.netlify.app/.netlify/functions/example?email=test@example.com

The function will receive test@example.com as the value of email.

Handling missing query parameters safely

Query parameters are optional by nature. A user can easily call your function without them.

That is why it is important to handle missing values gracefully.

Using optional chaining (?.) helps avoid runtime errors:

const email = event.queryStringParameters?.email

You can then decide what to do if the value is missing, such as returning an error or a default response.

Working with multiple query parameters

If your URL contains more than one query parameter, they all show up in the same object.

For example:

/api/search?term=netlify&page=2

You can read them like this:

const term = event.queryStringParameters?.term
const page = event.queryStringParameters?.page

Each value is a string, so remember to convert it if you need a number or boolean.

When to use query parameters in Netlify

Query parameters work best for:

  • Simple filters
  • Optional values
  • Small bits of data
  • GET requests

For larger or sensitive data, a POST request with a request body is usually a better choice.

Conclusion

Accessing query parameters in Netlify Functions is straightforward once you know where to look.

To summarize:

  • Query parameters live on event.queryStringParameters
  • Each value is a string
  • Always handle missing values safely
  • Use TypeScript for better clarity and safety

With this pattern, your Netlify Functions can easily react to data passed in the URL and handle a wide range of simple use cases without extra complexity.

Manage Netlify on the go

Download Netli.fyi and monitor your sites, check deploys, and manage your projects from anywhere.

Related articles