Lambda functions with dynamic path

Hello! :wave:

I have been working on a small project to display a build matrix for TravisCI using Netlify functions. I have a basic version working, but it has a super clunky API with a couple of required query parameters. I would like to clean this up a bit by moving those required parameters to path segments (e.g. /:owner/:repo/badge-v1.svg instead of /badge-v1.svg?owner=:owner&repo=:repo).

I have tried something like this (to no avail):

	from = "/:owner/:repo/badge-v1.svg"
	to = "/.netlify/functions/badge-v1"
	query = { owner = ":owner", repo = ":repo" }
	status = 200

Is there a way to accomplish this with rewrites?


You probably also need to define the params in your to attribute. Like so:

to = "/.netlify/functions/badge-v1?owner=:owner&repo=:repo"

It’s illustrated here in the _redirects format.

In case anyone else stumbles across this, I was able to get the interface I wanted.

It turns out that Netlify Functions see the original url and params when accessed via a rewrite (not the rewritten one), so there is no need to fiddle with getting path segments into the params at all.

You can see the working _redirects file here.

1 Like

thanks for sharing :pray: