GreenLoop IT Solutions : Articles
Automation Course Part 7 – Webhooks and HTTP triggered Azure functions
Welcome to the next knowledge-base in our API development series! In this knowledge-base, we will discuss webhooks, what they are, and how to build dynamic, serverless apps using HTTP triggered Azure Functions.
Webhooks are a way for applications to send real-time notifications to other applications. When an event occurs in an application, such as a new user registering or a new order being placed, the application can send a notification to a specified URL as a POST message. The body of the webhook call is typically a JSON object.
Azure Functions is a serverless compute service that enables you to run code on-demand without having to provision or manage infrastructure. We’ve previously discussed Timer-Triggered Azure Functions. But an important capability of Azure Functions for automation is support for HTTP-triggered functions, which can be used to build serverless apps that respond to webhooks.
Here are the steps to build a serverless app using HTTP triggered Azure Functions:
Step 1: Create an Azure Function App
The first step is to create an Azure Function App. This can be done in the Azure Portal or using PowerShell. For example, to create an Azure Function App in PowerShell, you can use the following command:
New-AzFunctionApp -ResourceGroupName <resource-group-name> -Name <function-app-name> -StorageAccountName <storage-account-name> -ConsumptionPlanLocation <location>
Step 2: Create an HTTP Triggered Function
Next, create an HTTP Triggered Function in the Azure Function App. This can be done in the Azure Portal or using PowerShell. For example, to create an HTTP Triggered Function in PowerShell, you can use the following command:
New-AzFunction -Name <function-name> -ResourceGroupName <resource-group-name> -FunctionAppName <function-app-name> -TemplateFile HttpTrigger -Language
This will create an HTTP Triggered Function that can respond to webhook requests.
Step 3: Add Webhook Response to Function
Now, add the response that you want the Function to send when it receives a webhook request. This can be done using PowerShell. For example, to send a response that includes the payload of the webhook request, you can use the following command:
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$response = @{
status = 200
body = "Received payload: $($requestBody | ConvertTo-Json -Depth 100)"
headers = @{ "Content-Type" = "application/json" }
}
Out-File -Encoding Ascii -FilePath $res -InputObject ($response | ConvertTo-Json -Depth 100)
This code will send a response with a 200 status code and the payload of the webhook request.
Step 4: Register Webhook with Other Application
Finally, register the webhook with the other application that will be sending notifications. This will typically involve providing a URL for the Azure Function App and any necessary authentication credentials.
To get the URL of your function:
Make sure to use an authenticated function if there’s any chance the URL could be abused!