How to make an API request inside of an Azure function that runs on a schedule using Node.js

What is the Azure Function

Azure Functions is a cloud service available on-demand that provides all the continually updated infrastructure and resources needed to run your applications. You focus on the pieces of code that matter most to you, and Functions handles the rest. Functions provides serverless compute for Azure.

https://docs.microsoft.com/en-us/azure/azure-functions/

What is a function trigger

A Function trigger is an event which is responsible for the execution of Azure functions.

How to create an Azure timer trigger function to call API request

Prerequisites

You must have an Azure paid account to continue this tutorial

Create Function App

STEP 01: From the Azure portal search and select Function App and click create

Azure Function App (https://pbs.twimg.com/media/E3Bu9LwX0AI7hAB.png)

STEP 02: On the Basic page, enter the following settings

Azure Function App settings
Basic page

STEP 03: On the Hosting, Networking, Monitoring and Tags pages, if you want to change the settings, change them or else keep them as it is

Hosting Page
Networking page
Monitoring page
Tags page

STEP 04: On the Review + Create page, review your settings and create the function

Review + Create page

STEP 05: After deployment succeeded, Go to resource to see your function app

Deployment completion notification
Function App dashboard

Create Timer Trigger function

STEP 01: In your function app, select Functions, and then select + Create

STEP 02: Select Timer trigger template from the template list and add the function name and the schedule

STEP 03: After creating the Timer trigger function, select Code+Test and add the following code to index.js and save

const httpMod = require(‘https’);module.exports = function (context, myTimer) { const options = {   host: ‘api.coindesk.com’,   path: ‘/v1/bpi/currentprice.json’,   method: ‘GET’,   port:443, }; const customReq = httpMod.request(options, res => {   context.log(`statusCode: ${res.statusCode}`) }) customReq.on(‘error’, (error) => {  context.log.error(error) })customReq.end();};

This is a sample code for making an API GET request to a publicly available endpoint, you can get an idea from the above code snippet.

STEP 04: To change the function running schedule, select the function.json from the drop-down and add the CRON expression and save it, or else select the Integration tab in the left-side panel and change the Trigger value.

function.json from drop-down
function.json

You can get an idea about the way of writing CRON expressions from here.

STEP 05: You can test the function using Test/Run button and you can see the logs from the logs tab

Logs tab

References

  1. https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-scheduled-function
  2. https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-javascript
  3. https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=v2-v3-v4-export%2Cv2-v3-v4-done%2Cv2%2Cv2-log-custom-telemetry%2Cv2-accessing-request-and-response%2Cwindows-setting-the-node-version#contextdone-method
  4. https://www.youtube.com/watch?v=Ft34VWPpiA4
  5. https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-javascript
  6. https://docs.microsoft.com/en-us/azure/azure-functions/functions-best-practices?tabs=csharp
  7. https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=v2-v3-v4-export%2Cv2-v3-v4-done%2Cv2%2Cv2-log-custom-telemetry%2Cv2-accessing-request-and-response%2Cwindows-setting-the-node-version#use-async-and-await
  8. https://stackoverflow.com/questions/57631020/making-an-https-request-inside-of-an-azure-function/72726173#72726173
  9. https://apipheny.io/free-api/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store