Create a custom connector from scratch
Note
This topic is part of a tutorial series on creating and using custom connectors in Azure Logic Apps, Microsoft Power Automate, and Microsoft Power Apps. 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 from scratch, without using a Postman collection or an OpenAPI definition to describe the Cognitive Services Text Analytics Sentiment API (our example for this series). Instead, you describe the connector completely in the custom connector wizard.
For other ways to describe an API, see the following topics:
- Create a custom connector from an OpenAPI definition
- Create a custom connector from a Postman collection
Note
You can currently create a custom connector from scratch in Power Automate and Power Apps. For Logic Apps, you must start with at least a basic OpenAPI definition or Postman collection.
Prerequisites
- An API key for the Cognitive Services Text Analytics API
- One of the following subscriptions:
Start the custom connector wizard
Sign in to Power Apps or Power Automate.
In the navigation pane, select Data > Custom connectors.
Select New custom connector, then choose Create from blank.
Enter a name for the custom connector, then select Continue.
Parameter Value Custom connector title "SentimentDemo"
Step 1: Update general details
From this point, we'll show the Power Automate UI, but the steps are largely the same across the technologies. We'll point out any differences.
On the General tab:
In the Description field, enter a meaningful value. This description will appear in the custom connector's details, and it can help others know if the connector could be useful to them.
Update Host to the address for the Text Analytics API. The connector uses the API host and the base URL to determine how to call the API.
Parameter Value Description "Uses the Cognitive Services Text Analytics Sentiment API to determine whether text is positive or negative" Host "westus.api.cognitive.microsoft.com" Note
For more information about the option Connect via on-premises data gateway, see Connect to on-premises APIs using the data gateway.
Step 2: Specify authentication type
There are several options available for authentication in custom connectors. The Cognitive Services APIs use API key authentication, so that's what you specify for this tutorial.
On the Security tab, under Authentication type, choose API Key.
Under API Key, specify a parameter label, name, and location. Specify a meaningful label, because this is displayed when someone first makes a connection with the custom connector. The parameter name and location must match what the API expects. Choose Connect.
Parameter Value Parameter label "API key" Parameter name "Ocp-Apim-Subscription-Key" Parameter location "Header" At the top of the wizard, make sure the name is set to "SentimentDemo", then choose Create connector.
Step 3: Create the connector definition
The custom connector wizard gives you many options for defining how your connector functions, and how it's 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.
Create an action
The first thing to do is create an action that calls the Text Analytics Sentiment API.
On the Definition tab, the left area displays any actions, triggers (for Logic Apps and Power Automate), and references that are defined for the connector. Choose New action.
There are no triggers in this connector. You can learn about triggers for custom connectors in Use webhooks with Azure Logic Apps and Power Automate.
The General area displays information about the action or trigger currently selected. Add a summary, description, and operation ID for this action.
Parameter Value 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." Operation ID "DetectSentiment" Leave the Visibility property set to none. This property for operations and parameters in a logic app or flow has the following options:
- none: displayed normally in the logic app or flow
- advanced: hidden under another menu
- internal: hidden from the user
- important: always shown to the user first
The Request area displays information based on the HTTP request for the action. Choose Import from sample.
Specify the information necessary to connect to the API, and the request body, then select Import. We provide this information for you (below the image). For a public API, you typically get it from the documentation, like Text Analytics Sentiment API.
Parameter Value Verb "POST" URL <https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment>
Body See below { "documents": [ { "language": "string", "id": "string", "text": "string" } ] }
The Response area displays information based on the HTTP response for the action. Choose Add default response.
Specify the response body, then choose Import. Like the request, we provide this information for you, but it's typically in the API documentation.
{ "documents": [ { "score": 0.0, "id": "string" } ], "errors": [ { "id": "string", "message": "string" } ] }
The Validation area displays any issues that are detected in the API definition. Check the status, then at the top right of the wizard, choose Update connector.
Update the definition
Now let's change a few things so that the connector is more friendly when someone uses it in a logic app, flow, or app.
In the Request area, choose body then Edit.
In the Parameter area, you now see the three parameters that the API expects:
id
,language
, andtext
. Choose id then Edit.In the Schema Property area, update values for the parameter, then choose Back.
Parameter | Value |
---|---|
Title | "ID" |
Description | "An identifier for each document that you submit" |
Default value | "1" |
Is required | "Yes" |
In the Parameter area, choose language then Edit, and repeat the process you used for
id
, with the following values.Parameter Value Title "Language" Description "The 2 or 4 character language code for the text" Default value "en" Is required "Yes" In the Parameter area, choose text then Edit, and repeat the process you used for
id
andlanguage
, with the following values.Parameter Value Title "Text" Description "The text to analyze for sentiment" Default value None Is required "Yes" In the Parameter area, choose Back to take you back to the main Definition tab.
At the top right of the wizard, choose Update connector.
Step 4: (Optional) Use custom code support
Note
This step is optional. You can complete the codeless experience for creating your connector by ignoring this step and going to Step 5: Test the connector.
Custom code support is available in public preview.
Custom code transforms request and response payloads beyond the scope of existing policy templates. Transformations include sending external requests to fetch additional data. When code is used, it will take precedence over the codeless definition. This means the code will execute, and we will not send the request to the backend.
You can either paste in your code or upload a file with your code. Your code must meet the following requirements:
- Be written in C#.
- Have a maximum execution time of 5 seconds.
- Can’t be more than 1 MB (size of the code file).
For instructions and samples of writing code, go to Write code in custom connectors.
For frequently asked questions on custom code, go to Custom code FAQ.
On the Code tab, insert your custom code using one of the following options:
- Copy/paste, or
- Select the Upload button.
If you choose to upload your custom code, only files with a .cs or .csx extension will be available.
Important
Currently, we only support syntax highlighting in the code editor. Make sure to test your code locally.
Once you paste or upload your code, select the toggle next to Code Disabled to enable your code. The toggle name changes to Code Enabled.
You can enable or disable your code anytime. If the toggle is Code Disabled, your code will be deleted.
Select the actions and triggers to apply to your custom code by selecting an option in the dropdown list. If no operation is selected, it's applied to all operations.
Step 5: 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 Power Automate and Power Apps.
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.
On the Test tab, choose New connection.
Enter the API key from the Text Analytics API, then choose Create connection.
Note
For APIs that require Bearer Authentication, add "Bearer" and one space before the API Key.
Return to the Test tab:
- In Power Automate, you are taken back to the Test tab. Choose the refresh icon to make sure the connection information is updated.
In Power Apps, you are taken to the list of connections available in the current environment. In the navigation pane, choose Data > Custom connectors. Choose the connector you created, then go back to the Test tab.
On the Test tab, enter a value for the text field (the other fields use the defaults that you set earlier), then choose Test operation.
The connector calls the API, and you can review the response, which includes the sentiment score.
(For CLI users) Best practices
Download all your connectors and use git or any other source code management system to save the files.
If there's an incorrect update, redeploy the connector by rerunning the update command with the correct set of files from the source code management system.
Test the custom connector and the settings file in a test environment before deploying in the production environment.
Always double check that the environment and connector id are correct.
Next steps
Now that you've created a custom connector and defined its behaviors, you can use the connector from:
You can also share a connector within your organization and/or get the connector certified so that people outside your organization can use it:
Provide feedback
We greatly appreciate feedback on issues with our connector platform, or new feature ideas. To provide feedback, go to Submit issues or get help with connectors and select your feedback type.