How to create Azure function Timer Trigger that calls API endpoint

Today our focus will be on how to create Azure function timer trigger that calls an API endpoint based on the cronjob time expression.

Serverless and Cloud computing is taking over the tech world by storm, and companies are moving to the cloud because of the many benefits. Azure function Timer Trigger is one of them.

Serverless, such as Microsoft Azure Timer Trigger Function, helps to focus on development and business instead managing servers. Write your functions, deploy, and Microsoft takes care of the rest.

Today we are going to be talking about the create Azure function timer trigger. We will create azure functions that trigger a specific time and call an endpoint.

I prefer to use the azure function to trigger APIs on remote servers or for long-running tasks. Microsoft Azure function can be written with Nodejs, Java, Python, C#, F#, TypeScript, etc.

Without much time, let's dive right in.

I am using Visual Studio 2022 for development.

Click on "Create a new project" button at the right corner of your Visual Studio.

In the search box, search for functions as seen in the image below and click the next button on the right.

Azure function Timer Trigger

Now name your function as you wish. Mine is FunctionApp1. Then click create.

There is a pop to select the kind of function that you want. I will scroll down to "Timer trigger" and then click create. Leave other things as default.

If everything is rightly done well, you will see a code similar to the image below.

On line 10, you can see the function name annotation, which defines the function name. You can rename the name as you wish. I will rename mine to "TestFunction."

On line 11, the method name is Run and has a void return type. You can also change to return anything you want and probably change the name to represent what you what to achieve. You can also see the TimerTriiger that accepts cron expression, which triggers every 5 minutes "0 */5 * * * *". To change your cron expression to suit your needs. You can configure the cronjob expression here.

You can also search google for the cronjob type you want. Eg, cron job for every second day of the month.

Now let us add more code to the application.

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using RestSharp;

namespace FunctionApp1
    public class Function1
        public const string baseurl = "https://localhost:5101/api/v1";
        public const string ApiKey = "remoteAPiKey";

        public void Run([TimerTrigger("*/5 * * * *")]TimerInfo myTimer, ILogger log)
            string url = $"{baseurl}/Values";
            var client = new RestClient(url);
            var request = new RestRequest();
            request.Method = Method.Get;
            request.AddHeader("ApiKey", ApiKey);
            RestResponse response = client.Execute(request);
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

The image above shows the application triggers every five minutes and calls an endpoint. This implementation will get you started as you can extend your application to whatever you need.

In the next post, we will write about how to easily deploy to azure.