Home
Support Forums

How to use URL parameters with on-demand builders

I’m trying to leverage on-demand builders by turning a simple serverless function into a “builder”.

The problem is that, as the documentation assess:

They don’t provide access to HTTP headers or query parameters from incoming requests.

Currently my function works with event.queryStringParameters.name retrieved from the function’s URL: /.netlify/function/photo?name=xxx

I could easily make the name param as part of the url .netlify/functions/photo/xxx but even then how could I retrieve that part of the URL? The doc is not really clear on what’s available from a “builder function”'s context.

Thanks!

1 Like

Hi,
First you’ll want to rewrite the URL to point at the function. e.g. add this to _redirects

/photos/* /.netlify/functions/photo 200

Then in the function you can access the URL on event.path. You can get the parts by doing something like:

const handler = async (event) => {
   const urlparts = event.path.split("/")
}

This would send requests to /photos/name to your function, and you could get name in something like urlparts[1] (though do check this).

Here’s how we do it in the Next plugin: netlify-plugin-nextjs/imageFunction.js at main · netlify/netlify-plugin-nextjs · GitHub

4 Likes

Thanks a ton! I was able to retrieve the parameter from the URL using your suggestion.

1 Like