Calling Azure Functions from XSLT map in Logic Apps
Scenario
Let's say we have to build an expense processing application. It takes the receipt with amount and currency information and converts it into final amount in US dollars based on the exchange rate.
Solution
We will use Logic Apps to build the solution. It takes the receipt information through a Request trigger, uses XML Transform to generate the final amount and returns the result using Response action. We don't want to hard-code the currency conversion rate in the XSLT map as they keep changing and will Azure Functions to look it up.
Let's start with the building blocks - Azure Function and XSLT map. Later we will stitch everything together in a Logic App.
Azure Function
We have a C# Webhook Function here which reads the currency information from request query parameters and gets the conversion rate to USD. Here we have hardcoded the mapping in the Function, but in a real world scenario it would be coming from a database or in real-time from a web service etc. Click here to download the source code.
XSLT Map
The XSLT map takes Azure Function App URL as a parameter (line #5). There are several benefits of doing this -
- The Function URL has authorization code embedded in it. We don't want to hard-code them in the map, rather fetch them from Azure Key Vault during deployment.
- We might have different Function App URL for test vs. production environment.
To learn more about XSLT parameters support in Logic Apps you can try out Azure Logic Apps - XSLT with parameters on github.
It then creates the output XML consisting of original currency, original amount and calculated amount in USD. For the calculation it first call the Azure Function passing the currency as input parameter and then multiplies it with the original amount.
We have used inline C# scripting to call the Function App using the HTTP client. In this example, we are passing the input parameters through query parameters and making a GET request but it can be easily modified to do more complex things like making a POST request with XML payload in request body.
Click here to download the source code.
Logic App
Let's start with creating an Integration Account first and uploading the XSLT map into it.
Next, create a Logic App in the same region and associate the Integration Account with it.
Its a simple Logic App containing a Request trigger, XML Transform action and a Response action.
The Request trigger exposes a REST endpoint and expects the receipt information (XML format) in the request body.
The Transform action shows 2 fields -
- The Content field takes the receipt information from the trigger body.
- The Map field shows the list of maps in the associated Integration Account. Select the map uploaded in previous steps.
The designer detects that the map contains an input parameter called AzureFunctionUri and automatically adds another field in the action. Copy the Function URL from the Function blade and paste it here.
In the end, we call the Logic App from Postman passing in the receipt payload in the request body and get back the amount in USD.