Invoke REST API endpoints within workflows using Azure Logic Apps

Calling REST API endpoints from workflows in Azure Logic Apps is a common use case. You can use the HTTP or HTTP + Swagger actions to interact with REST APIs. Below is a detailed guide and example of how to use the HTTP action to call a REST API and handle the response.

Steps to Call a REST API Endpoint Using HTTP Request and Response

  1. Add a Trigger:
    • Start your Logic App with a trigger. For example:
      • Use the HTTP Request trigger if you want to trigger the workflow via an external HTTP request.
      • Use a Recurrence trigger if you want to run the workflow on a schedule.
  2. Add an HTTP Action:
    • In the Logic App Designer, click on + New Step.
    • Search for and select the HTTP action.
  3. Configure the HTTP Action:
    • Method: Choose the HTTP method (e.g., GET, POST, PUT, DELETE).
    • URI: Enter the REST API endpoint URL.
    • Headers: Add any required headers (e.g., Content-TypeAuthorization).
    • Body: If the method requires a body (e.g., POST or PUT), provide the JSON payload.
  4. Handle the Response:
    • Use the output of the HTTP action to process the response (e.g., parse JSON, store data, or send a response back).
  5. Return a Response (Optional):
    • If your workflow is triggered by an HTTP request, you can use the Response action to send a response back to the caller.

Example: Call a REST API and Return the Response

Scenario:

You want to create a Logic App that:

  1. Accepts an HTTP request with a city name.
  2. Calls a weather API to get the current weather for that city.
  3. Returns the weather data as an HTTP response.

Steps in Logic App Designer

  1. TriggerWhen an HTTP request is received
    • This trigger allows the Logic App to be invoked via an HTTP POST request.
    • Add a sample JSON schema to define the expected input:
      {
        "type": "object",
        "properties": {
          "city": {
            "type": "string"
          }
        }
      }
  2. ActionHTTP
    • Add an HTTP action to call the weather API.
      • MethodGET
      • URIhttps://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=@{triggerBody()?['city']}
        • Use dynamic content @{triggerBody()?['city']} to pass the city name from the trigger.
      • Headers: None (unless the API requires authentication).
  3. ActionResponse
    • Add a Response action to return the weather data to the caller.
      • Status Code200
      • Headers:
        • Content-Typeapplication/json
      • Body: Use the Body output from the HTTP action to return the weather API response.

Here's how Logic Apps should look:

estudy247-la-test-restapi-21

Testing the Workflow

  1. Save and Run the Logic App.
  2. Use a tool like Postman or cURL to send an HTTP POST request to the Logic App’s HTTP trigger URL.
    • Example request body:
      {
        "city": "London"
      }
  3. The Logic App will:
    • Call the weather API for the specified city.
    • Return the weather data as the HTTP response.
You can download the logic app template from the estudy247 GitHub repository – la-test-restapi-21

Key Notes:

  • Dynamic Content: Use dynamic content (e.g., @{triggerBody()?['city']}) to pass data between steps.
  • Error Handling: Use the Configure run after settings to handle errors (e.g., retry or return an error response).
  • Authentication: If the API requires authentication, configure the headers or use a managed identity.

This approach allows you to create flexible and powerful workflows that interact with REST APIs and return responses to clients.

Post a comment

Leave a Comment

Scroll to Top