REST-style APIs in Azure Functions with JavaScript

This post is a follow-up to the Azure Functions CRUD with Table Storage & JavaScript / NodeJS article. We’ll now look at how to access these HTTP-triggered Create, Read, Update & Delete functions using REST-style URLs.

What do I mean by this? Let’s compare the URL structure we have and where we want to end up:

Default URL style with function name in the path
REST-style URLs

Notice that all calls are expected at /api/items and the correct function is being chosen only by the supplied HTTP verb (POST, DELETE, GET & PUT).

Luckily this requires very little code changes. The easiest way to figure this out is by going into the Azure Portal and looking at the Integrate configuration page for the function we want to change.

Here we can make sure only the correct HTTP method is selected and set up the route (in our case items) and. For those functions where we need to pass an ID in the URL (Update & Delete) we’ll add /{id} to indicate that we’re expecting an additional parameter.

For the Read-Function we’ll mark this parameter as optional with a * like this: items/{*id}

Integrate configuration page

Afterwards we can check how these changes are reflected in the functions.json file as this is where all these configurations are stored.

Updated function.json with route & optional ID

From there we can copy the JSON code back into our local source files to have them persisted.

The only thing left to do now is to update how we read the ID passed to the ItemRead, ItemPut & ItemDelete functions. As it’s not being passed as a query string anymore we’ll change from

const id = req.query.id;to const id = req.params.id;

That’s all there is to it. The updated code can be found here in the GitHub repo.

medialesson

We help our customers design, architect, develop and operate modern, intelligent, beautiful and usable apps on any platform powered by the Cloud, IoT and AI.

Thomas Pentenrieder

Written by

Dev, Windows Insider MVP, windowsarea.de, Consultant @medialesson

medialesson

We help our customers design, architect, develop and operate modern, intelligent, beautiful and usable apps on any platform powered by the Cloud, IoT and AI.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade