Create a custom connector from an OpenAPI definition

Note

This topic is part of a tutorial series on creating and using custom connectors in Azure Logic Apps, Microsoft Flow, and PowerApps. Make sure you read the custom connector overview to understand the process.

To create a custom connector, you must describe the API you want to connect to so that the connector understands the API's operations and data structures. In this topic, you create a custom connector using an OpenAPI definition that describes the Cognitive Services Text Analytics Sentiment API (our example for this series).

For other ways to describe an API, see the following topics:

Prerequisites

Import the OpenAPI definition

You're now ready to work with the OpenAPI definition you downloaded. All the required information is contained in the definition, and you can review and update this information as you go through the custom connector wizard.

Start by importing the OpenAPI definition for Logic Apps, or for Microsoft Flow and PowerApps.

Import the OpenAPI definition for Logic Apps

  1. Go to the Azure portal, and open the Logic Apps connector you created earlier in Create an Azure Logic Apps custom connector.

  2. In your connector's menu, choose Logic Apps Connector, then choose Edit.

    Edit Logic Apps Connector

  3. Under General, choose Upload Postman collection V1, then navigate to the OpenAPI definition that you created.

    Upload Postman collection

Note

This tutorial focuses on a REST API, but you can also use a SOAP API with Logic Apps.

Import the OpenAPI definition for Microsoft Flow and PowerApps

  1. Go to powerapps.com or flow.microsoft.com.

  2. In the upper right corner, choose the gear icon, then choose Custom connectors.

    Gear icon in service

  3. Choose Create custom connector, then choose Import an OpenAPI definition.

    Create custom connector

  4. Enter a name for the custom connector, then navigate to the OpenAPI definition that you downloaded or created, and choose Continue.

    Upload Postman collection

    Parameter Value
    Custom connector title "SentimentDemo"

Review general details

From this point, we'll show the Microsoft Flow UI, but the steps are largely the same across all three technologies. We'll point out any differences. In this part of the topic, we'll mostly review the UI and show you how the values correspond to sections of the OpenAPI file.

  1. At the top of the wizard, make sure the name is set to "SentimentDemo", then choose Create connector.

  2. On the General page, review the information that was imported from the OpenAPI definition, including the API host and the base URL for the API. The connector uses the API host and the base URL to determine how to call the API.

    Custom connector General page

    Note

    For more information about connecting to on-premises APIs, see Connect to on-premises APIs using the data gateway.

    The following section of the OpenAPI definition contains information for this page of the UI:

      "info": {
        "version": "1.0.0",
        "title": "SentimentDemo",
        "description": "Uses the Cognitive Services Text Analytics Sentiment API to determine whether text is positive or negative"
      },
      "host": "westus.api.cognitive.microsoft.com",
      "basePath": "/",
      "schemes": [
        "https"
      ]
    

Review authentication type

There are several options available for authentication in custom connectors. The Cognitive Services APIs use API key authentication, so that's what's specified in the OpenAPI definition.

On the Security page, review the authentication information for the API key.

API key parameters

The label is displayed when someone first makes a connection with the custom connector; you can choose Edit and change this value. The parameter name and location must match what the API expects, in this case "Ocp-Apim-Subscription-Key" and "Header".

The following section of the OpenAPI definition contains information for this page of the UI:

  "securityDefinitions": {
    "api_key": {
      "type": "apiKey",
      "in": "header",
      "name": "Ocp-Apim-Subscription-Key"
    }
  }

Review the connector definition

The Definition page of the custom connector wizard gives you a lot of options for defining how your connector functions, and how it is exposed in logic apps, flows, and apps. We'll explain the UI and cover a few options in this section, but we also encourage you to explore on your own. For information on defining objects from scratch in this UI, see Create the connector definition.

  1. The following area displays any actions, triggers (for Logic Apps and Microsoft Flow), and references that are defined for the connector. In this case, the DetectSentiment action from the OpenAPI definition is displayed. There are no triggers in this connector, but you can learn about triggers for custom connectors in Use webhooks with Azure Logic Apps and Microsoft Flow.

    Definition page - actions and triggers

  2. The General area displays information about the action or trigger currently selected. You can edit the information here, including the Visibility property for operations and parameters in a logic app or flow:

    • none: displayed normally in the logic app or flow

    • advanced: hidden under an additional menu

    • internal: hidden from the user

    • important: always shown to the user first

      Definition page - general

  3. The Request area displays information based on the HTTP request that's included in the OpenAPI definition. In this case, you see that the HTTP verb is POST, and the URL is "/text/analytics/v2.0/sentiment" (the full URL to the API is "https://westus.api.cognitive.microsoft.com//text/analytics/v2.0/sentiment"). We'll look closer at the body parameter shortly.

    Definition page - request

    The following section of the OpenAPI definition contains information for the General and Request areas of the UI:

    "paths": {
      "/text/analytics/v2.0/sentiment": {
        "post": {
          "summary": "Returns a numeric score representing the sentiment detected",
          "description": "The API returns a numeric score between 0 and 1. Scores close to 1 indicate positive sentiment, while scores close to 0 indicate negative sentiment.",
          "operationId": "DetectSentiment"
    
  4. The Response area displays information based on the HTTP response that's included in the OpenAPI definition. In this case, the only response defined is for "200" (a successful response), but you can define additional responses.

    Definition page - response

    The following section of the OpenAPI definition contains some of the information related to the response:

    "score": {
     "type": "number",
     "format": "float",
     "description": "score",
     "x-ms-summary": "score"
    },
    "id": {
     "type": "string",
     "description": "id",
     "x-ms-summary": "id"
    }
    

    This section shows the two values that are returned by the connector: id and score. It includes their data types, and the field x-ms-summary, which is an OpenAPI extension. For more information on this and other extensions, see Extend an OpenAPI definition for a custom connector.

  5. The Validation area displays any issues that are detected in the API definition. Make sure to check this area before you save a connector.

    Definition page - validation

Update the definition

The OpenAPI definition you downloaded is a good basic example, but you might work with definitions that require a lot of updating - so that the connector is more friendly when someone uses it in a logic app, flow, or PowerApps app. We'll show you how to make a simple change to the definition.

  1. In the Request area, choose body then Edit.

    Edit request body

  2. In the Parameter area, you now see the three parameters that the API expects: ID, Language, and Text. Choose ID then Edit.

    Edit request body id

  3. In the Schema Property area, update the description for the parameter, then choose Back.

    Edit schema property

    Parameter Value
    Description "A numeric identifier for each document that you submit"
  4. In the Parameter area, choose Back to take you back to the main definition page.

  5. At the top right of the wizard, choose Update connector.

Download the updated OpenAPI file

You can create a custom connector from an OpenAPI file, a Postman collection, or from scratch (in Microsoft Flow and PowerApps). Regardless of how you create the connector, you can download the OpenAPI definition that the service uses internally:

  • In Logic Apps, download from the custom connector:

    Download OpenAPI definition

  • In Microsoft Flow or PowerApps, download from the list of custom connectors:

    Download OpenAPI definition

Test the connector

Now that you've created the connector, test it to make sure it's working properly. Testing is currently available only in Microsoft Flow and PowerApps.

Important

When using an API key, we recommend not testing the connector immediately after you create it. It can take a few minutes until the connector is ready to connect to the API.

  1. On the Test page, choose New connection.

    New connection

  2. Enter the API key from the Text Analytics API, then choose Create connection.

    Create connection

  3. Return to the Test page:

    • In Microsoft Flow, you are taken back to the Test page. Choose the refresh icon to make sure the connection information is updated.

      Refresh connection

    • In PowerApps, you are taken to the list of connections available in the current environment. In the upper right corner, choose the gear icon, then choose Custom connectors. Choose the connector you created, then go back to the Test page.

      Gear icon in service

  4. On the Test page, enter a value for the text field (the other fields use the defaults that you set earlier), then choose Test operation.

    Test operation

  5. The connector calls the API, and you can review the response, which includes the sentiment score.

    Connector response

Next steps

Now that you've created a custom connector and defined its behaviors, you can use the connector:

You can also share a connector within your organization and/or get the connector certified so that people outside your organization can use it: