Logic Apps: Tips and Tricks to use Azure Functions inside Logic Apps

  • Sandro Pereira
  • Apr 14, 2016
  • 10 min read

In one of my last posts, I explained how to call an Azure Function to create a basic dynamic Hello World Logic App, but it was a simple introduction to Azure Functions Integration inside Logic Apps. However, there are many other scenarios that weren’t addressed in the post, like:

  • How can you pass inputs to an Azure Function?
  • How to deal with “complex” outputs from the Azure Function?
  • What types of Azure Functions are supported?
  • and so on.

In this post, we will try to deepen some of these topics and provide you with some tips and tricks to get you started using Azure Functions on your Logic Apps.

📝 One-Minute Brief

This post expands on using Azure Functions inside Azure Logic Apps, sharing practical tips and tricks for passing inputs, handling outputs, supported function types, and avoiding common pitfalls when integrating serverless code into workflows.

What types of Azure Functions are supported?

Azure Functions provides you a variety of template options to create a function, from BlobTrigger (C# and Node), Empty (C# and Node), EventHubTrigger (C# and Node), HttpTrigger (C# and Node)… to Generic WebHook (C# and Node)

Azure Functions Choose a template

Despite all these varied offers, at the moment (everything on Azure can evolve and change rapidly), the only supported Azure Functions that you can call inside your Logic Apps are:

  • WebHook Node JS Functions
  • Or WebHook C# Functions
Azure Functions supported types-on Logic Apps

How can we pass inputs to an Azure Function and access to the output?

After a while, it is very simple to understand what is needed to send inputs to an Azure Function; however, access to the response output, or outputs, and use them in subsequent connectors… can bring some challenges.

Let’s create some basic samples in order to try to demystify some of these challenges, and for these samples, we will be using WebHook Node JS Functions.

An important thing you need to be aware of is that, within Logic Apps, you need to always send a valid JSON message as input to a Webhook Function.

Sending empty inputs to an Azure Function

So, for example, if your Function doesn’t really need to have any input, you will still need to send a valid JSON message, but because any value that we send is unnecessary, the trick here is to send an empty JSON message:

  • “{}” – without double quotes
Azure Functions sending an empty msg

If we don’t do this and leave the Input Payload Object empty or “”, the Logic App will fail with the following error:

“The WebHook request must contain an entity body formatted as JSON.”

Sending inputs to an Azure Function

To send inputs to the Azure Function, we need to, for example, manually create our JSON message in the “Input payload object” property inside the Configure Function Inputs panel. Let’s look at an example:

  • I added a Twitter – When a new tweet appears trigger in my Logic App, which looks for a new tweet containing the hashtag #LogicApps.
    • In my case, I will use #nodejs for the sake of speed, since there are constantly emerging tweets with this hashtag.
Azure Functions Tweet Appears query

And I want to send the Tweeted by and the Tweet text as inputs for my Azure Functions. Let’s ignore for now what the function does, but the function needs to receive the following JSON format.

{    
   "Tweet": {    
     "by": "New York",    
     "text": "21 2nd Street"    
   }    
 }

To accomplish this on the “INPUT PAYLOAD OBJECT” property, I need, for example, (that depends on the code of the function), to:

  • On the text property, type: {“Tweet”: {“by”:
    • Here I’m starting to create my JSON message.
  • Next, I need to select the Tweeted by output from my Twitter trigger. You can see all the elements of the previous steps that you can use at the bottom of the window of this action.
Azure Functions add first element
  • After that, we need to continue to create the remainder of the message by typing:
    • ,”text”:
  • Select the Tweeted text output from the Twitter trigger and finally type the rest of the JSON message:
    • }}

The end result will look like this:

Azure Functions add second element

Retrieving the output of an Azure function

Now that we know how to send inputs to our Azure Function, let’s see how we can use its output in subsequent connectors.

Following the input sample provided above:

{  
  "Tweet": {  
    "by": "New York",  
    "text": "21 2nd Street"  
  }  
}

Let’s say that we have the following Azure Function:

module.exports = function (context, data) {    
        
  var Tweet = data.Tweet;    
  
  var d = new Date().getTime();    
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {    
        var r = (d + Math.random()*16)%16 | 0;    
        d = Math.floor(d/16);    
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);    
  });    
        
  // Response of the function to be used later.    
  context.res = {       
    body: {          
      Msg: 'Sandro you have a new tweet msg: ' + Tweet.text,    
      FileName: 'TweetMsg_' + uuid + ".txt"         
    }         
  };    
                
  context.done();    
};

That returns the following output (in the result of the input provided earlier):

{    
  "Msg": "Sandro you have a new tweet msg: This is #LogicApps demo msg",    
  "FileName": "TweetMsg_315381a5-847f-4962-9f1f-4fb65a958753.txt"    
}

And we want to create a Dropbox file containing only the value of the output parameter Msg, and the name of the file should be the value of the output parameter Filename.

But for now, let’s set the filename statically as Demo1.txt.

If we add a Dropbox – Create file connector to our Logic App:

Azure Functions add Dropbox Create file
  • Set the Folder path, let’s say the root, by typing: /
    • Note: You can also use the “” button to navigate to your Dropbox folders.
  • Click on the Filename and type: Demo1.txt.
  • And then, click on the File content text box, we will notice that the output parameters of the Azure Function are in the Body and Status code.
    • Let’s select “Body” as the content of the file we want to create.
Azure Functions Dropbox Create file-Demo 1

If we save and execute the Logic App when a new tweet is found, we will see a new file in our Dropbox; however, the content of the file will be the entire JSON message that is returned by the function

Azure Functions Dropbox Demo 1 Content

We can also receive the following error in the following Logic App executions. We will deal with it shortly:

"body": {
  "status": 409,
  "message": "File '/Demo1.txt' already exists. Use the Update operation to update an existing file.",
  "source": "127.0.0.1"
}

So how can I access to the output parameters and use them on the Dropbox connector properties?

The trick here is:

  • At design time, set the function output parameter Body both in the FILE NAME and FILE CONTENT properties of the Dropbox connector (or any other connector).
    • You don’t need to do this step, but the reason why I do it is that I am a little lazy and I want the designer to do half of the work for me.
Azure Functions Dropbox Create File Default configuration
  • Then, at the top of the Logic Apps Designer, click the Code view option to edit these values in Code View mode.
Azure Functions Logic Apps Designer Code View
  • Then we need to edit the Create file inputs to have the expected values.
Azure Functions Logic Apps Designer Code View properties
  • Change the inputs > queries > name property to: @{body(‘TwitterPassDataToFunction’)[‘FileName’].
  • And the inputs > body to: @{body(‘TwitterPassDataToFunction’)[‘Msg’].
Azure Functions Logic Apps Designer Code View properties fixed

Now, if we change to the Logic Apps Designer by clicking the Designer option, we will notice that the designer already renders these properties to set the expected parameters. Parameters that are not there out-of-the-box.

Azure Functions Logic Apps Designer properties fixed

If we once again save and execute the Logic App, when a new tweet is found, we will see a new file in our Dropbox with the expected file name:

Azure Functions Dropbox Files Created

And if we check the content of the file(s), we will also see that the file has the expected content:

Azure Functions Dropbox Files Expected-Content

I hope that the next version will have a better experience to deal with the Azure Functions outputs to have, by default, a first-class experience.

Azure Function returning only one parameter

The previous sample returns two parameters; now, if we want to return only one parameter, we can create a concept of “a string variable” that you can use in subsequent connectors. We can make small changes to our Azure Function to have a first-class experience without the need to switch to “Code View” to change the inputs of the connector.

Using the same sample, let’s assume that:

  • Our function only returns the message that we want to put in the context of our file.
  • We want the file name to have the “Tweet id” as the filename, instead of the previous “Filename” that will no longer exist.
  • The file context will be provided by the output of the Azure Function.

If we create a new Azure Function from the Logic App Designer, it will suggest the following code, which, of course, we can change:

module.exports = function (context, data) {     
  var Tweet = data.Tweet;    
        
  // Response of the function to be used later.    
  context.res = {         
    body: {    
                
      greeting: 'Hello ' + Tweet + '!'    
                
    }    
                
  };         
  context.done();    
};

If we use this template approach and change the code to have, for example:

body: {     
      Msg: 'Sandro you have a new tweet msg: ' + Tweet.text          
    }

We will face the same problem as the previous sample, and we would have to access to “Code View” and manually change/set the parameters of the subsequent connectors.

But in this case, we can implement a different approach to have a better user experience in the Logic Apps Designer. Instead of following the suggested approach, we can implement the following code:

module.exports = function (context, data) {
  
  var Tweet = data.Tweet;      
  // Response of the function to be used later.    
  context.res = {      
   body: 'Sandro you have a new tweet msg: ' + Tweet.text   
  };    
       
  context.done();       
};

If we try to execute this function through the Azure Portal, we will notice that the output is no longer a JSON message:

{    
  "Msg": "Sandro you have a new tweet msg: This is #LogicApps demo msg",    
  "FileName": "TweetMsg_83557388-97c4-4503-a3cd-7be145f31d42.txt"    
}

But instead it is now a string: Sandro you have a new tweet msg: 21 2nd Street.

Azure Functions test

If we now go back to the logic app that we created earlier, and:

  • Set the Dropbox – Create file filename property with the Twitter output parameter Tweet id and add a file extension by typing .txt.
  • And the file content with the Azure Function output parameters: Body.
Azure Functions Dropbox reconfigured

If we, once again, save and execute the Logic App, when a new tweet is found, we will see a new file(s) in our Dropbox that are created with the expected file name:

Azure Functions Dropbox Files Created

And if we, once again, check the content of the file(s), we will also see that the file has the expected content:

Azure Functions Dropbox Files Expected Content

Having this way, a better user experience inside the Logic Apps Designer. However, this approach only works if the output is a single value (int, string, double).

Hope you find this helpful! If you liked the content or found it useful and would like to support me in writing more, consider buying (or helping to buy) a Star Wars Lego set for my son. 

Thanks for Buying me a coffe
Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

3 thoughts on “Logic Apps: Tips and Tricks to use Azure Functions inside Logic Apps”

  1. Great article about how to use Azure Function in Logic Apps. Looking forward to see some real world integration scenario use case with Azure Function.

  2. Hi Sandro,
    I need your help. I have a one stored procedure on azure sql db. I have logic app , it will capture new data load into that table. After triggering I want to call Azure Function . This azure function will call SP.I tried it but azure function require “REQUEST BOSY”. How do I fill this.

Leave a Reply

Your email address will not be published. Required fields are marked *

The Ultimate Cloud
Management Platform for Azure

Supercharge your Azure Cost Saving

Learn More
Turbo360 Widget

Back to Top