Webhooks Micro Service development using Azure Functions

Creating and consuming webhook Micro Service using Azure Functions

Shems Eddine
Sep 5, 2018 · 4 min read

In my previous article, I briefly explained what Webhooks are and why they might be useful to develop. In this technical article, I am going to walk you through how to develop Webhooks using Azure Functions.

The user case

I have a website that allows you to upload, remove and update documents. I am going to create a small Azure Function that will act as the Webhooks Micro Service. We will be listening to an Azure Storage Queue to events coming in from other parts of our systems (not covered in this article).

We will then use data from an Azure Storage Table to figure out if a user has subscribed to this particular event and will try to send the payload to their callback action.

NOTE: I will not be showing you any UI development and all of the data entry will be done through the Azure Storage Explorer.

  • NPM and Node
  • Visual Studio Code
  • Azure Storage Emulator
  • Azure Storage Explorer
  • Knowledge of Azure Functions and developing with JavaScript in a Node Environment

The Webhooks Micro Service

We will creating our Webhooks Micro Service using Azure Functions in NodeJS.

Before we get started, we need to install some Azure Function tools to work with NodeJS.

Open a command prompt and enter the following

npm i -g azure-functions-core-tools

First let’s create a new folder af-webhook-client and within it, let’s run the following commands

func init .

Choose the node worker runtime.

Let’s create our first function

func new

Choose the Queue Trigger, the JavaScript language and choose the name, webhooks-service

Now in our local.settings.json we can define our AzureWebJobsStorage to use the development storage connection string.

We can now set our function.json inputs

In our Azure Function, we are going to define two inputs and no outputs. Our primary input will be a queue trigger and essentially, our Azure Function will fire and forget and will not need to reply back or store anything elsewhere using the output bindings.

Here, our primary input binding is the queueTrigger. We are assuming that the message within the event in our queue are going to have the following schema.

{
"userId": "",
"eventType": "",
"message": ""
}

This in turn will be what we are filtering by for our table input. We can use the filter property to build an expression within the table binding to filter the rows.

We made our partitionKey to be the userId as this will ensure that all of the webhooks for that user will be collated together. our rowId is going to be the eventType as only a single webhook callback action shall be defined for that user.

We want to be able to post to the callback our data so we will use fetch to do so. Let’s install fetch

npm i -S node-fetch

As you can see, we are grabbing our subscription from the context binding. Then we build up our payload which includes the eventId and the createAt timestamp and the message itself.

We can access additional queue item data using context.bindingData

we then try to post the data to the subscription url. If this fails, we write a message to the console.

Let’s give this a try.

Open up Azure Storage Explorer. Let’s create our queue and table storage.

Let’s add our first entry to the webhooksubscriptions table

Run your function using

func host start

We can now test our our first event. In the Azure Storage Explorer, let’s add a single event

{
"userId": "test_user",
"eventType": "document_uploaded",
"message": "my_document1"
}

Voila! You should now see…

It’s still happy news, trust me. We just now need to create our client.

The Webhooks Consumer (our callback API)

We have now created our Webhook Micro Service but we haven’t defined anything to consume it. Let’s look at creating a callback function within our API.

Let’s start off a new project af-webhooks-client

npm init -y af-webhooks-client

Let’s add restify which will be our RESTful API server

npm i -S restify

and let’s define our callback action

Now let’s run our server

node index.js

If we add the same event to our Storage Queue again, this time we should get something like this.

There we have it. You can find the source code on GitHub:

Next up, I will explain how to secure both your Webhooks Micro Service and your consuming Web API.

SHEMSEDDINE on Code

Azure Cloud Platform, Tech Radar, .NET Core, NodeJS and all things Code

Shems Eddine

Written by

Azure Cloud Platform Expert | NodeJS & .NET Senior Consultant | Director of Technology in City of London

SHEMSEDDINE on Code

Azure Cloud Platform, Tech Radar, .NET Core, NodeJS and all things Code

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