Редагувати

Поділитися через


Connect or call REST API endpoints from workflows in Azure Logic Apps

Applies to: Azure Logic Apps (Consumption + Standard)

To call a REST API endpoint from a logic app workflow in Azure Logic Apps, you can use the built-in HTTP + Swagger operations to call any REST API endpoint through a Swagger file. The HTTP + Swagger trigger and action work the same as the HTTP trigger and action but provide a better experience in the workflow designer by exposing the API structure and outputs described by the Swagger file. To implement a polling trigger, follow the polling pattern that's described in Create custom APIs to call other APIs, services, and systems from logic app workflows.

Limitations

The HTTP + Swagger built-in operations currently support only OpenAPI 2.0, not OpenAPI 3.0.

Prerequisites

  • An account and Azure subscription. If you don't have an Azure subscription, sign up for a free Azure account.

  • The URL for the Swagger file that describes the target REST API endpoint that you want to call

    Typically, the REST endpoint has to meet the following criteria for the trigger or action to work:

    • The Swagger file must be hosted on an HTTPS URL that's publicly accessible.

    • The Swagger file must contain an operationID property for each operation in the definition. If not, the connector only shows the last operation in the Swagger file.

    • The Swagger file must have Cross-Origin Resource Sharing (CORS) enabled.

    The examples in this guide use Azure AI Face, which requires an Azure AI services resource key and region.

    Note

    To reference a Swagger file that's unhosted or that doesn't meet the security and cross-origin requirements, you can upload the Swagger file to a blob container in an Azure storage account, and enable CORS on that storage account so that you can reference the file.

  • The Consumption or Standard logic app workflow from where you want to call the target endpoint. To start with the HTTP + Swagger trigger, create a logic app resource with a blank workflow. To use the HTTP + Swagger action, start your workflow with any trigger that you want. This example uses the HTTP + Swagger trigger as the first operation.

Add an HTTP + Swagger trigger

This built-in trigger sends an HTTP request to a URL for a Swagger file that describes a REST API. The trigger then returns a response that contains that file's content.

  1. In the Azure portal, open your Standard logic app resource and a blank workflow in the designer.

  2. On the designer, follow these general steps to add the HTTP trigger named HTTP + Swagger.

  3. In the Swagger Endpoint box, enter the URL for the Swagger file that you want, and select Add Action.

    Make sure to use or create your own endpoint. As an example only, these steps use the following Azure AI Face API Swagger URL located in the West US region and might not work in your specific trigger:

    https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face%20API%20-%20V1.0

    Screenshot shows Standard workflow designer with trigger named httpswaggeraction. The Swagger Endpoint property is set to a URL value.

  4. When the designer shows the operations described by the Swagger file, select the operation that you want to use.

    The following example renames the trigger to Face - Detect so that the trigger has a more descriptive name.

    Screenshot shows Standard workflow, Face - Detect trigger, and list with Swagger operations.

  5. Provide the values for the trigger parameters, which vary based on the selected operation, that you want to include in the endpoint call. Set up the recurrence for how often you want the trigger to call the endpoint.

  6. To add other available parameters, open the Advanced parameters list, and select the parameters that you want.

    For more information about authentication types available for HTTP + Swagger, see Add authentication to outbound calls.

  7. Continue building your workflow with the actions that you want to run when the trigger fires.

  8. When you're done, save your workflow. On the designer toolbar, select Save.

Add an HTTP + Swagger action

This built-in action sends an HTTP request to the URL for the Swagger file that describes a REST API. The action then returns a response that contains that file's content.

  1. In the Azure portal, open your Standard logic app resource and workflow in the designer.

  2. On the designer, follow these general steps to add the HTTP action named HTTP + Swagger.

  3. In the Swagger Endpoint box, enter the URL for the Swagger file that you want, and select Add Action.

    Make sure to use or create your own endpoint. As an example only, these steps use the following Azure AI Face API Swagger URL located in the West US region and might not work in your specific trigger:

    https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face%20API%20-%20V1.0

    Screenshot shows Standard workflow designer with trigger named Face - Detect, and action named httpswaggeraction. The Swagger Endpoint property is set to a URL value.

  4. When the designer shows the operations described by the Swagger file, select the operation that you want to use.

    The following example renames the action to Face - Identify so that the action has a more descriptive name.

    Screenshot shows Standard workflow, Face - Identify action, and list with Swagger operations.

  5. Provide the values for the action parameters, which vary based on the selected operation, that you want to include in the endpoint call.

  6. To add other available parameters, open the Advanced parameters list, and select the parameters that you want.

    For more information about authentication types available for HTTP + Swagger, see Add authentication to outbound calls.

  7. Continue building your workflow with any other actions that you want to run.

  8. When you're done, save your workflow. On the designer toolbar, select Save.

Host Swagger in Azure Storage

You can still reference a Swagger file that's not hosted or that doesn't meet the security and cross-origin requirements. Upload the Swagger file to blob container in an Azure storage account and enable CORS on that storage account. To create, set up, and store Swagger files in Azure Storage, follow these steps:

  1. Create an Azure storage account.

  2. Now enable CORS for the blob. On your storage account's menu, select CORS. On the Blob service tab, specify these values, and then select Save.

    Property Value
    Allowed origins *
    Allowed methods GET, HEAD, PUT
    Allowed headers *
    Exposed headers *
    Max age (in seconds) 200

    Although this example uses the Azure portal, you can use a tool such as Azure Storage Explorer, or automatically configure this setting by using this sample PowerShell script.

  3. Create a blob container. On the container's Overview pane, select Change access level. From the Public access level list, select Blob (anonymous read access for blobs only), and select OK.

  4. Upload the Swagger file to the blob container, either through the Azure portal or Azure Storage Explorer.

  5. To reference the file in the blob container, get the HTTPS URL that follows this format, which is case-sensitive, from Azure Storage Explorer:

    https://<storage-account-name>.blob.core.windows.net/<blob-container-name>/<complete-swagger-file-name>?<query-parameters>

Connector technical reference

This section provides more information about the outputs from an HTTP + Swagger trigger and action.

Outputs

The HTTP + Swagger call returns the following information:

Property name Type Description
headers Object The headers from the request
body Object The object with the body content from the request
status code Integer The status code from the request
Status code Description
200 OK
202 Accepted
400 Bad request
401 Unauthorized
403 Forbidden
404 Not Found
500 Internal server error. Unknown error occurred.

Next steps