Support Forums

How do I access path parameters within netlify functions?

When crafting a RESTful API, all the following are valid (since 2014 when the GET standard allowed a body):
(1) GET, users/1
(2) GET, users?id=1
(3) GET, users, where the body is {id:1}

I am using React and netlify-lambda to run local .netlify/functions.

I can access event.queryStringParameters for (2), and event.body for (3), but how do I access the path parameters (1) ? In AWS lambda this is supported as event.pathParameters

Do I need to set up routing somewhere?

I’m don’t really like the idea of using
let id = event.path.split(’/’)[3];

At the moment, that’s the only working solution that I aware of. You could try logging the event to see if you get anything extra, but from the testings I had done previously, I don’t recall having anything better.

As you noted, the event object doesn’t have anything more useful than the path field.

Thank you.