Azure Logic App with simple API App with inputs and outputs

Logic Apps is a new service offered by Microsoft Azure. It allows users to easily create and manage a flow of triggers and actions. Triggers start when an event happens, it can be periodic triggers or triggers based on an arrival of an Email, etc… when a trigger kicks in, the actions specified in the Logic App are invoked. All triggers and actions are API Apps, for more details about Logic Apps please visit this documentation  and for API Apps, you can find documentation here  

 Here I will go through the creation of a simple API App that takes an input and does some processing and has an output. I chose my operation to be ToUpper, it takes a string and converts all characters to upper case. To do that I used Visual studio to create and publish the API App, and then Azure portal to create and manage the Logic App. 

Let’s start by creating a new API App. To do that I had Visual studio 2013 and I had to install Azure SDK 2.5 for visual studio 2013. (I installed it using Web Platform Installer, search for visual studio azure SDK). Open visual studio, create new project -> visual C# -> Web -> ASP.NET Web Application, you will see these options:

Choose Azure API App and ok.

This will create a new project and then will add 2 main components

  1. Web API: Microsoft Framework to create web services based on ASP.NET for more info please visit http://www.asp.net/web-api
  2. Swashbuckle: a utility that helps create swagger api definitions for your web api for more info please visit https://github.com/domaindrivendev/Swashbuckle

The new project will have ValuesController I will just rename the controller to ToUpperController, make sure you change the controller name and the file name too to ToUpperController.cs. And this is how my controller looks like 

 public class ToUpperController : ApiController
 {
 // GET api/values
 public string Get(string toConvert)
 {
 return toConvert.ToUpper();
 }
 }

In the project go to App_Start, SwaggerConfig class and enable the UI by uncommenting this 

  /*
 })
 .EnableSwaggerUi(c =>
 {
 */

Run your site and go to {localhost:port}/swagger you will see this UI:

And you can pass in any string and “try it out!” you see that the body will have the string you sent in capitalized letters. If you browse to {localhost:port}/swagger/docs/v1, you will get the swagger API definition file

Now here is the trick that took me some time to figure out, Logic apps only work with the default response and not the 200 response defined in the swagger JSON  

So to add default response to all your APIs here is what you need to do is to go to SwaggerConfig.cs and do the following

  1. Uncomment

     c.OperationFilter<AddDefaultResponse>();
    

     

  2. Add the following class

     public class AddDefaultResponse: IOperationFilter
     {
     public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
     {
     if (!operation.responses.ContainsKey("default") && operation.responses.ContainsKey("200"))
     {
     operation.responses.Add("default", operation.responses["200"]);
     }
     }
     } 
    

And that is how the swagger response will look like:

Now we are ready to publish to a live API App, right click on the project then hit publish. To do that you need to be signed in to Azure and you need a subscription. Documentation on how to publish API App here 

Ok now let’s play with the new API App that we created, go to the portal and create a new twitter API app, just search Azure marketplace and install the package “Twitter Connector”.

Once you have all deployed, create a new Logic app, go the designer by clicking triggers and actions.

In my scenario I will trigger this flow manually so I will check the box “Run this Logic Manually”, now it will ask to drop in an action, from the right list drop in twitterconnector, I will choose search tweets action, and I will search for Azure. Now drop in your API App in and choose the only action you got now “ConverToUpper_Get” it will ask for input called “toConvert” let’s click on the “…” button to get a list or parameters that we can get from the previous action. I will choose Search Tweets Tweeted By as input. Notice here it only shows the “variables” that are of type string, because we specified that our input to the API is string. This is how the design will look after you are done

 

Now save your changes, close the designer and go back to the Logic App blade and click “Run Now” you will see in the operation part a new run shows up, if you click on that you can get to the results of the run 

 

Now if you open the logs for each action you will see 2 links “INPUTS LINK” and “OUTPUTS LINK” each will open JSON data files that contain the information. If you go to your ToUpperApp input links you will see something similar to this:

And to outputs link you will see that the name DanWahlin was transformed to Capital letters and is sent back in the body.

Now you have created the API App from scratch and it can take in parameters and send back ouputs. That opens up the gate for lots of innovative Logic apps you can easily and rapidly develop.