Integrating Power Automate with ASP.NET Core Web APIs.

This is the third and final post in our ASP .NET Core Web API series.

If you already have ASP .NET Core Web API which exposes an OpenAPI document(Swagger) and want to Integrate it with Power Platform then you are at the right place.

In this post, I’ll provide you with step-by-step instructions to integrate our Web APIs with a Power Platform.

In this three part series for ASP.NET Core Web APIs, we have covered the following –

Note – If you’ve been following my posts, then you can skip Step 1 and Step 2 as we have already implemented the changes in our project in previous posts.

Step 1 – Configuring your ASP .NET Core Web API project to support OpenAPI 2.0 format.

  • By default, ASP .NET Core Web API project generates and exposes Swagger(OpenAPI Specification) JSON in version 3.0. However, the Power Platform currently supports OpenAPI version 2.0.
  • To make the Swagger compatible with Power Platform, you will need to make the changes to the Startup.Configure method.
public void Configure(IApplicationBuilder app)
{
    //** Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger(c =>
    {
        c.SerializeAsV2 = true;
    });
}

Step 2 – Make sure you add OperationID for the Controller Method.

  • The operationId is normally an optional and unique string used to identify an operation, but in Power Automate it is necessary or else your custom connector will return an error.
  • You can use the Name attribute and set it to any string, but I’m partial to using nameof like this: nameof(ActualMethodName). i.e. Decorate routes with a Name property
[HttpGet("{id}", Name = nameof(GetProductById)]
public IActionResult Get(int id) // operationId = "GetProductById"

For more details on how to assign explicit OperationIds see link.

The above two steps is very important if you are planning to use the OpenAPI definition with Power Platform.

Step 3 – Save the OpenAPI(Swagger) file to your local computer.

Step 4 : Create a custom connector from an OpenAPI definition:

  • Import the OpenAPI definition for Power Automate and Power Apps
  • Go to  flow.microsoft.com.
  • In the navigation pane, select Data > Custom connectors.
  • Select custom connector.
  • Select New custom connector, then choose Import your OpenAPI file .
  • Import an OpenAPI file.
  • Enter a name for the custom connector, then navigate to the OpenAPI definition that you downloaded or created, and choose Continue.

On the General page, review the information that was imported from the OpenAPI definition and add the API host URL to determine how to call the API.

  • On the Security page, review authentication type. Our APIs use API key authentication, so that’s what’s specified in the OpenAPI definition.
  • The Definition page of the custom connector wizard gives you a lot of options for defining how your connector functions, as well how it’s exposed in logic apps, flows, and apps. Note: You can also Edit your Swagger using the “Swagger Editor” but, as we have already edited the “Swagger”, just click on ‘next’.

  • Skip the Code Review Step.
  • Now that you’ve created the connector, test it to make sure it’s working properly. Note: When using an API key, we recommend not testing the connector immediately after you create it. It can take a few minutes until the connector is ready to connect to the API.
  • On the Test page, choose New connection.
  • Return to the Test page, for the Operations set the “ConvertToPDF to “Raw Body On”, paste the JSON below, and click on ‘Test operation’.
  • You will get a JSON response that looks like the following example:

Accessing Custom Connector in Power Automate to Convert Documents to PDF using our Customer connector:

Step 1 : Open the Power Automate / Flow designer, create a Flow, find the “When a file is created or modified (properties only)” trigger. 

  • This trigger will allow you to start the Flow by adding a file to a folder or when modifying a property of a file already in the folder. Naturally other triggers can be used as well (email attachments, drop box, OneDrive, you name it).

Step 3: Add our Custom “Convert Document to PDF” to the Flow Canvas.

  • Source_file_name:  “File name with Extension” the output of the “When a file is created or modified (properties only)” action.
  • Source_file_content: “File Content” the output of the “Get File Content” action.

Step 4: Use the “Create file” SharePoint action to write the generated PDF document to the output document library.

  • File name: Use the “Name“.pdf the output of  “When a file is created or modified (properties only)” action .
  • File Content: “Processed file content” is the output variable of the  Custom “Convert Files to PDF” action.

That’s it- you’re done!  Publish your Flow and upload a supported file type to the folder that is monitored by the configured SharePoint trigger. After a short wait, you can find the PDF in the output folder.

Subscribe to this blog for the latest updates about SharePoint Online, Power Automate (Microsoft Flow), Power Apps and document conversion and manipulation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s