One of the most powerful things about Logic Apps is the integration agility it provides. If something needs to change, you can easily swap in or swap out components without having to modify any production code. In addition, it's also possible to call Logic Apps from other Logic Apps which makes use and re-use even easier. There are a few ways to do this, and I think CarlosAg's blog post on nesting does a great job explaining using the "flow" function. For this post though, I'm going to walk through calling a Logic App from an HTTP Action because I think it helps illustrate what's happening behind-the-scenes, and also allows you to call Logic Apps from any HTTP endpoint.
Let's walk through a sample scenario - let's say I have a number of different services and apps I use, and each one of them is storing customer information. Suppose I want to have a full combined list of all of the contacts across all of my services - or I want to keep customer info in sync with a SaaS application like Salesforce. Logic Apps makes integrating or combining across multiple data sources easy. For the example in this post, I am going to store all of my contacts inside of a SQL Azure table.
Building the Logic App
First, let's set up the "AddContact" Logic App. This will be a simple manually triggered logic app that receives contact information, checks to see if that user already exists in the database, and if not, adds the contact. Here's an example of what that logic app would look like:
I've kept this fairly simple - first a SQL action performs a query that returns all rows for customers with the email address I am passing in. The next step has a condition to only execute if the previous SELECT returned an empty array (which means that customer isn't currently in the database). If that happens, it then does an Insert with the customer data values.
The first thing you may notice is that even though I have the Logic App set as "Manual", I'm still referencing @trigger().output
. That's because I plan to leverage the "Run Workflow" HTTP POST method to trigger this app, which allows me to pass in trigger parameters.
Calling the Logic App from HTTP
So now that the Logic App is deployed, we need to be able to call it. There are two things you need for this to work - the endpoint, and an access key - both of which can be retrieved in the settings pane of the Logic App under Settings->Properties. I usually use either Postman or Fiddler to test. Below is what the POST parameters would look like (documentation on parameters can be found in the Management API under "Run Workflow"). In order to create the authorization token, we use the username "default" with the access key retrieved in settings, and Base64 encode it for Basic auth. So you would Base64 encode default:{AccessKey}
(I usually just use this Bing search result page to get the encoded value) and use that in the Authorization header like below:
URL:
https://{endpointaddress}/run?api-version=2015-02-01-preview
Headers:
"Content-Type: application/json" "Authorization: Basic ZGVmYXxxxxxxxxxx5PajVj"
Body:
{
"name": "TestTrigger",
"outputs": {
"body" : {
"Name": "Jeff Hollan",
"Email": "jeff@email.com",
"Phone": "(555) 555-5555",
"Twitter": "jeffhollan"
}
}
}
You should see a "202 Accepted" returned, and if you check your Logic App Operations you'll see a Logic App fired and took the values you passed in. You could use this information to call a Logic App from any endpoint, but here's how we could use that to call a Logic App from within another Logic App
Calling the Logic App from another Logic App
Now let's create a second logic app that can call our "AddContact" Logic App with the necessary values. Since Logic Apps integrate well with SaaS Applications, for this example I'm going to add contact info for anyone who posts on my Facebook page. Here's a screenshot of what this sample app could look like:
You'll notice the last action I do is an HTTP Action - and I'm re-creating the HTTP Post we just did above, only now I am substituting in the values from the Facebook into the call. This will trigger the second Logic App, and this user will be added in my contacts database. Here is the code-view of the HTTP action for reference:
"http": {
"type": "Http",
"inputs": {
"method": "POST",
"uri": "https://westus....../AddContact/run?api-version=2015-02-01-preview",
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic ZGVmY.......jVj"
},
"body": {
"outputs": {
"body": {
"Name": "@{triggers().outputs.body.postedby}",
"Email": "@{triggers().outputs.body.Id}@facebook",
"Phone": "",
"Twitter": ""
}
}
}
},
"conditions": []
}
What makes this method convenient is now I can create multiple different Logic Apps, each triggering on different events (e.g. Customer added in On-Premise dataset, Tweet mention, Salesforce lead, etc.), and each of these Logic Apps could pull the needed information and pass it on to the AddContact Logic App. If I need to change how I store the contacts, or add additional steps, I can do it in one spot, and all of the Logic Apps will still work. This is just a small example of some of the scenarios that are made possible through Azure Logic Apps.