Automating API Requests with C# and Azure Functions using HttpTrigger

Introduction:

As more and more businesses move towards automation, developers are increasingly required to build systems that automate tasks that would otherwise require manual intervention. One such task is making requests to APIs on a regular basis. In this blog post, we will show you how to create a C# function in Azure Functions that can automatically make requests to a specified API every 10 minutes.

Automating API Requests with C# and Azure Functions using HttpTrigger

Steps to create Azure Function in Portal:

Demo

Step 1:

Create an Azure Function App The first step is to create an Azure Function App in the Azure portal. Go to the Azure portal, click on Create a resource, and search for Function App. Follow the prompts to create your Function App.

Step 2:

Create a New Function Once your Function App is created, you can create a new function within it. Click on the Functions section in the left-hand menu and click the Add button to create a new function. Select the C# language and the HttpTrigger template.

Step 3:

Add Code to the Function In the function editor, you can add your C# code to make the API request. You can use the HttpClient class to make the request. Here’s an example:

public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
{
    var client = new HttpClient();
    var response = await client.GetAsync("https://api.example.com/");
    var responseString = await response.Content.ReadAsStringAsync();
    log.LogInformation(responseString);
    return new OkObjectResult(responseString);
}

Step 4:

Schedule the Function To schedule the function to run every 10 minutes, you can use the built-in TimerTrigger. Add the following code to the function:

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

private static readonly HttpClient httpClient = new HttpClient();

public static async Task Run([TimerTrigger("0 */10 * * * *")]TimerInfo myTimer, ILogger log)
{
       log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

    var url = "https://essentialproducts-api.azurewebsites.net/api/Category/All";
        var response = await httpClient.GetAsync(url);
        var responseBody = await response.Content.ReadAsStringAsync();

        log.LogInformation($"API response: {responseBody}");
}

This code uses a Cron expression to schedule the function to run every 10 minutes. You can customize the expression to suit your needs.

CRON expression Explained

"0 */10 * * * *" is a CRON expression that specifies the schedule for the timer trigger. The * character is a wildcard that matches any value, and the / character specifies a step value. So in this case, "0 */10 * * * *" means:

  • The function should trigger every minute at the 0th second (0).
  • The function should trigger every 10 minutes (*/10), i.e. at 0:10, 0:20, 0:30, etc.
  • The function should trigger every hour (*) of every day of the month (*).
  • The function should trigger every day (*) of every month (*).
  • The function should trigger every day of the week (*), where Sunday is represented as either 0 or 7.

To change the timer interval, you can modify the CRON expression. For example, if you want the function to trigger every 5 minutes, you can use the expression "0 */5 * * * *". If you want the function to trigger every hour at the 30th minute, you can use the expression "0 30 * * * *".

You can use an online CRON expression generator to help you create the desired schedule.

Step 5:

Test the Function Once you have created and scheduled your function, you can test it by clicking the Run button in the function editor. You can also view the function’s logs to see the results of the API request.

Application Source Code @ LearnSmartCoding GitHub

Check out other topics that might interest you.

Conclusion:

Automating API requests with C# and Azure Functions can save time and reduce the risk of errors. By following the steps outlined in this blog post, you can create a C# function in Azure Functions that can make requests to a specified API every 10 minutes.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights