In today’s blog, we will explore how to call a Logic App using Python code. I have a scenario where I need to send an email to the desired recipient with some information in the middle of the data processing. This can also be achieved by using the SMTP connection in Python for sending email. However, this blog is more about calling the Logic App using Python.
Initially, you must create a Logic App capable of receiving HTTP requests, followed by setting up an Outlook connection that includes input fields for the subject line, recipient, and email body. As per your needs, this logic can accommodate multiple inputs. I need a simple three inputs to send the email so I have only three inputs. Feel free to refer to the blog for guidance on creating the Logic App. https://techdiw.com/send-customized-email-using-logic-app-from-adf-synapse-pipeline/
After creating the Logic App, you can utilize its URL within your Python code to call it.
We are going to use the Python “request” module to trigger it. We will be using the POST method to send the request to the Logic App URL
import requests input_data = { "body" : f"Hi Diwakar,<br> This is a blog on triggering the logic app using the python code.<br><br>", "subject" : "This is test email using the logic app" "receiver" : "diwakar.kumar@outlook.com" } logic_app_url = "https://prod-08.southeast.logic.azure.com:443/workflows/ckahsgeinaksdvgia2357knafi73jjn9/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=isadgnaks;ASGdsaFSGE" response = requests.post(url=logic_app_url, json=input_data) print(response.text)
First, you need to define a JSON object containing the input values that the logic expects. The requests.post method includes arguments such as URL, data, JSON and others (you can refer to the Python requests module documentation for details on the POST method).
Additionally, you should define a variable for the Logic App URL. Then, you can pass these values as inputs to the POST method.
I am printing the response of the API call to check for any errors. You can use the status_code method of the response to determine if the call was successful or failed. If the status is 200 then the call was successful, if it is not 200 there may be an issue somewhere.
Post completing these setups, you are good at running the code. Hope this blog helped you understand how to trigger the Logic App from the python code. The same code can be used in the Synapse notebook.
