Guest Posts on using Web APIs as a Custom API in Microsoft Flow
With Custom APIs in Microsoft Flow, you can use from flow any custom actions you build as a developer. Gene Vangampelaere has written a great blog post on how to get started with a Web API and register that as a Custom API in a flow. After you've followed the below tutorial, you can check out another post, by Tsuyoshi Matsuzaki, on how to set up Authentication for your Web API. This covers both how to use Azure AD and Google authentication.
Thanks to Gene and Tsuyoshi, on behalf of the Flow team! If anyone else has blog posts they would like to highlight, please let us know and we can highlight it on the blog.
Use your custom WebApi with Microsoft Flow
Microsoft Flow is the new workflow engine from Microsoft. With flow you can create automated workflows between your favorite apps and services to get notifications, synchronize files, collect data, and more.
You can find Microsoft Flow at: https://flow.microsoft.com
Flow contains many predefined templates to use: https://flow.microsoft.com/en-us/templates/. Things like send reminders via the Flow mobile app to you phone when you get a mail or sync your Dropbox files with OneDrive, …
The real power of Microsoft flow is the fact that you can use you own services and extend your flows with custom data. In the following demo I will create a flow to send a notification email to users from a custom REST web service.
To start I create a custom C# ASP.NET WebApi application in Visual Studio 2015. I called the application “SwaggerDemo“.
If you don’t want to create it yourself, you can download the solution from my GitHub account.
The Api contains the following code:
In the old days we created soap based web services that used WSDL to describe the service. Nowadays we use Swagger.io to do this. It is pretty easy to implement Swagger to an existing WebApi.
Use NuGet to add the Swashbuckle package to you project.
To know more about this, check this page: http://devcenter.wintellect.com/paulballard/give-your-rest-apis-some-metadata-swagger!
When you run your project now you get to see the “Swagger” output:
Now you can deploy your server to a public webserver and we can start using it with Flow.
To start using the Api we need to create a new service in Flow. Services are used to provide you the data and do operations.
To add your custom API, go to the Flow dashboard via https://flow.microsoft.com and click the settings icon in the top right corner:
Select “Custom API’s” and click the “Create custom API” button in the next screen.
To add an API you need a Swagger file, a JSON file that describes you service. To get the Swagger file for you WebApi go to https://editor.swagger.io, click “File” and “Import URL…”. Enter the url of you webapi in the next screen.
Click on “Import”.
Now you click “Download JSON” in the “File” menu:
Save that file because we’ll need it in a moment.
We go back to our Flow – Create Api screen and upload our Swagger JSON file and enter the details for our custom API:
That is pretty much all we need to do to register our Api in Flow.
No we can start creating our first custom flow in Microsoft Flow. Click on “My flows” in the top menu bar and click on the “Create from blank” button.
Start with giving you flow a name on top of the screen and add the first action. I added the “Recurrence” action to run my flow every 10 minutes for example.
In the next step I will call my custom service. Click on “Add action” and select your service. Mine was called Swagger DEMO:
You don’t need to do a lot of things here. The API will be called and the results will be stored in a some variables that we will use in the next step.
In this example I’ll send an email to each user I get from the service. I use the “Apply to each” action to iterate the results:
The output of the REST call to my custom service is stored in the “body” variable. The “Apply to each action will loop all the results and will execute each action in the container. I add a “Send an email” action to it.
The Swagger file that I uploaded while creating my custom service will provide me the objects and properties that I can use in my action. In this example I get an email address and a name for each user that the service call returns.
Now my flow is ready. All I need to do is save and publish it!