Recently in a project, there came a requirement where we had to monitor an Azure Storage queue for the count of messages over a given period. We needed to have some notification/Alert system to inform the team that more than X messages have accumulated in the queue and action needs to be taken. For this reason, we chose to use a timer-based Azure Function and integrate it with the Twilio Send Grid API SaaS offering of Microsoft Azure.

Azure Functions and Twilio SendGrid API

Setup: Twilio SaaS Subscription

First, we go to the Azure Portal and navigate to SaaS options. Once there, we need to click on “Create SaaS Resource” and search for Twilio in the Marketplace. Click on “Subscribe” and then fill out the details.

Twilio SaaS Subscription

Project Details

Once we have entered the essential details about Azure Subscription, Resource groups, name of the SaaS resource, pricing plan (we will use the free tier for now), etc., we click on “Review+Subscribe.” It will ask for a confirmation, and then we hit “Subscribe.” It will take a few minutes to complete the subscription process for the Twilio SaaS application. Once complete, click on the “Configure Account Now” Option.

Configure SaaS Service

This will take us to the Twilio third-party website, basically the console for controlling our subscription. For the first time, it will ask us to fill in some details.

Twilio Third-Party Website

Once we fill out the details and click on Get Started, it will take us to the home page of the Twilio Application.

Get Started Homepage

From here, we expand the “Settings” menu on the left-hand pane, and there will be an API Key option. Clicking on that will open a page where we can create Email API Keys that will be needed later to authorize our requests from the C# Azure Function.

API Key Functions

We click on Create API Key, and then it will give us three options (API Key Permissions) to choose from:

  1. Full Access
  2. Restricted Access
  3. Billing Access

Detailed documentation about these Access options is available on the Twilio Documentation. Still, we will now choose Full Access, give a name to the API and create it. Once we hit create, it will show us the key value we need to copy and save somewhere since it will not be visible again. If we lose it, we will need to create again a new one and use it.

API Key Created

This completes our setup for the Twilio SaaS Application, and now we are ready to create our Azure Function Application.

Setup: Azure Function Application

We are going to create a timer-trigger-based Azure Function that is going to monitor an Azure Storage Queue. Once that queue has more than 50 messages, we will be using the SendGrid API to send a notification to a particular email address. We will use Visual Studio to create an Azure Function and then write some code to monitor the Azure queue.

The Azure Function will look like this:

Azure Functions
This Azure Function will run in a particular frequency, for example, every 2 hours daily. It will use the GetMessageCountInQueue() method to return the number of messages in the queue. The GetMessageCountInQueue() uses the CloudQueueClient to get the reference of the queue using the queue name and then using the queue reference, we get the attributes out of it.

CloudQueueClient

Once this method returns a value greater than 50, we will call our SendGrid API to send an email to a specified Email Address. For that, we need to install the SendGrid Package into our project and then use that to call the SendGrid endpoint. We will set up an email with a body, a from an email address, and a To Email address and then pass it to the SendGrid Client.

SendGrid API

We will use the API key we previously created to authenticate our request to the SendGrid Endpoint and successfully send the email.

Conclusion

We created an Azure SaaS subscription to the Twilio SendGrid API and then created an API key that we can use to authorize our requests to the SendGrid Endpoint for sending Emails. We then used that API key in our Azure Function to call the SendGrid API by passing an email message using the MailHelper. There are many more areas to explore in the SendGrid API console for governance, monitoring, and improving the performance of the overall API, which can be referred to using this link.