Calling An Azure Function From Power Automate (MS Flow)

calling azure function from power automate ms flow

In this Azure tutorial, we will discuss Calling An Azure Function From Power Automate (MS Flow)How to create PowerShell Azure Function. Along with this, we will also discuss a few other topics How To Call An Azure Function From Power Automate (MS Flow), Create An Azure Function App, Power Automate Call Azure Function, Test the Power Automate (Flow), and Finally, we will also discuss Call Azure Function From Power Automate (MS Flow) With OAuth.

Calling An Azure Function From Power Automate (MS Flow)

Well, here we will discuss How to Call An Azure Function From Power Automate (Microsoft Flow). Before starting the development we should know the Prerequisites needed for this development activity.

Prerequisites

  • An Azure Subscription or Azure Account. If you don’t have an Azure Account, Create an Azure Free Account now.
  • Power Automate (MS Flow) license (O365 E3 Developer)

How To Call An Azure Function From Power Automate (MS Flow)

So as part of the development activity, First, we will create An Azure Function App In the Azure Portal and then we will create an HTTP trigger Azure Function. Finally, we will do the changes to the Power Automate(MS Flow). Now let’s create the Azure Function App

Create An Azure Function App

Login to the Azure Portal (https://portal.azure.com/)

Once you logged in to the Azure Portal, Search for the Function App in the Azure Portal and click on the search result

Calling An Azure Function From Power Automate (MS Flow)

On the Azure Function App page, click on the + Add button to create a new Azure Function App.

The next step is, Provide the below Details On the Create Function App Page,

  • Subscription: Choose the Correct Azure Subscription that you want to use here.
  • Resource GroupChoose your existing Resource Group or you can create a new one by clicking on the Create new link if you don’t have any existing Resource group.
  • Function App name: You need to provide a unique name for your Azure Function App n your choice.
  • Publish: Select the Code option here.
  • Runtime Stack: Make sure to choose the .Net Core option here.
  • Version: Choose the latest version of .Net Core here.
  • Region: Provide the Region for your Azure Resource.

Finally, Click on the Review + create button.

Call An Azure Function From Power Automate (MS Flow)

Now all the information, you have entered will be validated and if all are ok then, you can able to see the Create button is enabled on the next window. Finally, click on the Create button to create the Azure Function app.

Now you can able to see Your deployment is completed successfully. Now you need to click on the Go To Resource button to navigate to the Azure Function that you have created now.

Call Azure Function From Power Automate (MS Flow)

The next step is to create the Azure Function. We will create an HTTP triggered Azure Function here. To create the HTTP triggered Azure Function, click on the Functions option from the left side of the Azure Function App page and then click on the + Add button to create the Azure Function.

How to Call Azure Function From Power Automate (MS Flow)

Now you need to choose the Development environment as Develop in portal and then choose the template option as HTTP trigger

How to Call Azure Function From Power Automate

Now Provide a unique name for your Azure Function, Choose the Authorization level as Function, and then finally click on the Add button to create the Azure Function.

How to Call Azure Function From Power Automate or MS Flow

Now you can able to see below, Our Azure Function is created successfully. Click on the Code + Test link from the left side navigation on the Function page as highlighted below,

How to Call Azure Function From Power Automate or Microsoft Flow

You can able to see the run.csx file code as below

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    string responseMessage = string.IsNullOrEmpty(name)
        ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
}

Now click on the Test/Run button from the top to test if the function is working fine or not. Choose the HTTP method and provide the Body in JSON format in the Input Window and then click on the Run button to test the Azure Function.

How to Call Azure Function From MS Flow

You can able to see we got the response code as 200 Ok and the expected HTTP response content as highlighted below.

How to Call Azure Function From Microsoft Flow

Now click on the Get function URL button and then click on the Copy button as highlighted below to copy the complete Azure Function URL

Call Azure Function From Microsoft Flow

The Function URL should look like below

https://mymsflowazfn.azurewebsites.net/api/MyHTTPTriggerFN?code=QpeBSTLGTO2ss7RwOMsLgjarO7Tm5bq05KGkXpDmJSxbtcluKXTw8w==

Just copy this and keep it in a notepad as we are going to use this Function URL later.

Now, Our Azure Function part is done, We will move to the next step i.e Calling Azure Function From Power Automate (MS Flow)

Power Automate Call Azure Function

Assuming you have the Complete Azure Function URL, Let’s start with the implementation for calling the Azure function from Power Automate or MS Flow.

Creating the Power Automate (Flow)

  1. The first step is, to access the Power automate or MS Flow, Go to https://flow.microsoft.com
  2. Once you logged in, click on the My flows link from the left navigation and then click on the + New button from the top navigation as highlighted below.
How to Call Azure Function From Power Automate or Flow

3. Once you will click on the + New button, then click on the + Instant-from blank link as highlighted below.

call azure function from ms flow

4. On the Build, an instant flow page, Provide a name for the Flow or Power automate, and for the option “Choose how to trigger a flow”, choose Manually trigger a flow as of now. You can choose the other trigger option as well based on your requirement. But for the time being, let’s choose the first option.

call azure function from flow

5. You can able to see, the flow has been created with the Manually trigger a flow option.

call azure function from Power automate (flow)

6. Click on the ‘+ New Step’ step and from the Choose an action search for HTTP and select the HTTP under the All tab and then click on the HTTP Premium under the actions tab is highlighted below.

call azure function from Power automate

7. It might ask you to start your Free trial based on your Flow plan. Then It will add the HTTP action like below.

call azure function from Power automate(MS Flow)

8. Now the next step is, select the Method as Post and paste the complete URL appending the name parameter value that you have copied in the above steps. The complete URL will look like below

https://mymsflowazfn.azurewebsites.net/api/MyHTTPTriggerFN?code=QpeBSTLGTO2ss7RwOMsLgjarO7Tm5bq05KGkXpDmJSxbtcluKXTw8w==&name=Raj

Choose the Authentication as None and then finally click on the Save button as shown below.

call azure function from power automate ms flow

Test the Power Automate (Flow)

Now we have created the complete flow and saved the Power Automate or flow. The time to test the Power automate (Flow) and check if it is working fine.

Now To test the Power automate or Microsoft Flow, click on the Test button that is present next to the Flow checker button on the top right corner of the page.

how to call azure function

On the Test Flow, select the I’ll perform the trigger action and then click on the Test button to check if the Power automate is working or not.

call azure function from power automate or ms flow

Now on the next screen, it will show you the Owner details and it will enable the Run Flow button. So click on the Run Flow button to run the Power Automate or Flow.

Now you can able to see below, your flow run successfully started.

power automate call azure function

You can click on the Flow Runs Page link as highlighted above. You can able to see the Run history of the Power Automate with details on the time details when exactly the flow has executed and the status as Succeeded as shown below.

call azure functions from Microsoft Flow

Now click on the history as highlighted above, you can able to see your flow ran successfully message with the Flow structure. Now click on the HTTP action as highlighted below.

power automate azure function

Now, you can able to see below, we got the expected Status Code as 200 and The exact output message in the Body i.e Hello, Raj. This HTTP triggered function executed successfully. So we successfully called the Azure Function from the Power Automate or Microsoft Flow and we got the expected Outcome.

Microsoft flow call azure function

So This is how to call azure function power automate using the above steps.

Call Azure Function From Power Automate (MS Flow) With OAuth

So Above we discussed, call the azure function from power automate (ms flow) with Authentication as none. But as security is very much important, We will discuss here How to Call Azure Function From Power automate With OAuth.

Remember, we will use here, Active Directory (OAuth). To configure the OAuth here, we need few stuffs like

  • Client ID
  • Tenant
  • Secret

You might be thinking from where I will get the Client ID and Secret for my Azure Function App. So here are the steps to configure and get those values

Login to the Azure Portal again and Navigate to your Azure Function App page again.

Click on the Authentication / Authorization link from the left navigation. Select the App Service Authentication as On. Choose “Log in with Azure Active Directory” for the option Action to take when the request is not authenticated

Then click on the Azure Active Directory (Not Configured) Option.

call azure function from power automate (ms flow) with oauth

Then Select the Management mode as Express, Now again select the Management mode as Create New AD App and then Provide a name for the Create App, By Default if will show you the name as your Function App but if you want to change the Create App name, you can change also. Then click on the Ok Button.

Trigger Azure Function From Microsoft Flow

Then, Make sure to click on the Save button from the below window.

Trigger Azure Function From Power automate

Now the next step is search for the Azure Active Directory in the Azure Portal and click on the Search result.

On the Azure Active Directory pane, click on the App registrations link from the left navigation and then you can able to see the Azure Function App registration for your Azure Function App that you have configured above. Click on that.

Trigger Azure Function From Power automate(MS Flow)

Client ID

Now On the Overview tab, You can able to see the Client ID as highlighted below, You can copy this to a note pad and keep it as we are going to use this in our MS Flow Configuration.

Trigger Azure Function From Power automate or MS Flow

Secret

Now the next thing, we need the Secret value, Click on the Certificate & Secrets from the left navigation like highlighted in the above screen shot.

Click on the + New Client secret under the Client secrets section to generate a client secret to access the Azure Function App.

Trigger Azure Function From Power automate or Flow

On the Add a client secret window, select the Expires option based on your requirement, and then click on the Add button.

How to Trigger Azure Function From Power automate or Microsoft Flow

Now you can able see the secret ID that you have created just now, Copy that value by clicking on the Copy button as highlighted and keep it in a notepad.

One important point to note down here is to copy the secret immediately or else it will get encrypted after a few minutes.

Trigger Azure Function From Power automate or Microsoft Flow

Now we have our Client ID and secret with us. Let’s configure the OAuth in the Microsoft Flow.

After the Step number 7 above, You need to configure the below options now

  • Authentication: Select the Active Directory OAuth
  • Authority: https://login.microsoftonline.com
  • Tenant: Mention your Tenant Name
  • Audience: Put the client ID that you have copied before.
  • Client ID: Put the client ID that you have copied before.
  • Credential Type: Select Secret as the Credential Type.
  • Secret: Enter the Secret that you have generated above and copied it.

Now Click on the Save button as high lighted below.

How to Trigger Azure Function From Power automate

Now Click on the Test button that is present next to the Flow checker. On the Test Flow, select the I’ll perform the trigger action and then click on the Test button.

Click on the Run Flow button on the next screen to run the Power Automate or Flow. Now you can able to see, your flow runs successfully started.

You can click on the Flow Runs Page link, you can able to see the History with the Status as Succeeded. You can click on history. Then click on the HTTP action, you can able to see the Expected OutPut as below (Refer to the Test the Power Automate (Flow) section for the detailed step with screenshots ).

You can able to see, we got the expected output with status code as 200 ok and expected Outcome message. You can able to see the Authentication section, which contains the client ID, Secret, and all other details needed for the ActiveDirectoryOAuth configuration.

Steps to Trigger Azure Function from Power Automate
Steps to Trigger Azure Function from Microsoft Flow

So, This is How to Call Azure Function From Power Automate (MS Flow) with Authentication as none and Call Azure Function From Power Automate (MS Flow) With OAuth i.e ActiveDirectoryOAuth by following the above steps.

You may also like following the below Articles

Wrapping Up

Well, in this article, we have discussed Calling An Azure Function From Power Automate (MS Flow), How To Call An Azure Function From Power Automate (MS Flow), Create An Azure Function App, Power Automate Call Azure Function, Test the Power Automate (Flow) and Finally, we also discussed Call Azure Function From Power Automate (MS Flow) With OAuth. Hope you have enjoyed this article !!!

Leave a Comment