How to Run Power Automate Flow using API in C#

In this post, we will trigger the Microsoft Flow using an API in C#. This approach is useful when you want to reduce your application code and let Power Automate take care of most of the things.

This demo is part of E-commerce bot for email authentication feature.

Prerequisites

  1. Visual Studio
  2. Microsoft Flow Account
  3. Run Power Automate flow from the Postman using API

Visual Studio link is available in the Downloads page.

Video

Create a Console Project in Visual Studio

Make sure you have completed requirement 3. There, we have created a flow to send an email on the HTTP request. We have also got the HTTP POST URL from there which is required for us to call the API.

Create a new Console Application project and name it TriggerMSFlow.

Choose the target framework .NET Core 3.1 (Long-term support).

Add the following code the Program.cs file.

using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace TriggerMSFlow
{
    public class Program
    {
        static async Task Main(string[] args)
        {
            Program program = new Program();
            await program.SendEmailForCodeVerificationAsync(174582, "info@jd-bots.com", "JD Bots", "https://prod-02.centralindia.logic.azure.com:443/workflows/<your Power Automate POST URI>");
        }

        public async Task SendEmailForCodeVerificationAsync(int verificationCode, string toAddress, string username, string uri)
        {
            try
            {
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(uri);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, client.BaseAddress);
                var body = $"{{\"emailAddress\": \"{toAddress}\",\"emailSubject\":\"[NaamiChocos] Email Verification Code\",\"userName\":\"{username}\",\"OTP\":\"{verificationCode}\"}}";
                var content = new StringContent(body, Encoding.UTF8, "application/json");
                request.Content = content;
                var response = await MakeRequestAsync(request, client);
                Console.WriteLine(response);

                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new Exception();
            }
        }

        public async Task<string> MakeRequestAsync(HttpRequestMessage getRequest, HttpClient client)
        {
            var response = await client.SendAsync(getRequest).ConfigureAwait(false);
            var responseString = string.Empty;
            try
            {
                response.EnsureSuccessStatusCode();
                responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            }
            catch (HttpRequestException)
            {
                // empty responseString
            }

            return responseString;
        }
    }

    public class AuthenticationModel
    {
        public string status { get; set; }
        public string message { get; set; }
    }
}

Make sure to install and import necessary NuGet packages. Replace your Power Automate POST URI in the Main. Below is the output on the console window.

Output from Email:

Output from Power Automate:

You can open up the individual steps to see their output.

Thank you All!!! Hope you find this useful.


Up ↑

%d bloggers like this: