Hello and welcome back!
In our last lesson, we explored how to integrate a Canvas App with Application Insights, giving you the power to capture and analyze production telemetry. This involved connecting your app to an external Azure service and using the Trace() function to send data.
Today, we'll build on that theme of external connectivity. Our learning outcome is to consume data from a pre-built custom connector within a Canvas App. This is a fundamental skill for a Power Platform developer, as it unlocks the ability to integrate your apps with virtually any REST API, extending their reach far beyond the standard set of connectors. For the PL-400 exam and complex enterprise projects, mastering custom connectors is essential.
Given your strong background in C# and REST API development, you can think of a custom connector as a declarative, low-code equivalent of writing a client SDK. It defines the API's endpoints, authentication, and data contracts, making them easily consumable within Power Fx. We won't be building a connector from scratch today (we'll cover that in-depth in Module 7), but we will quickly review its structure to understand what we're working with before diving into the main focus: calling it from a Canvas App.
1. What is a Custom Connector? A Quick Refresher
Before we consume a connector, let's briefly clarify what it is. Standard connectors (like for SharePoint or Dataverse) are pre-built "wrappers" that Microsoft provides for popular services. A custom connector is a wrapper that you build yourself for any REST API that doesn't have a standard connector.
It defines key aspects of an API so that Power Platform can understand how to communicate with it:
- General Information: Host URL and base path (e.g.,
api.weatherapi.com). - Security: The authentication method (e.g., API Key, OAuth 2.0).
- Definition: The specific operations (actions) the API supports, like
GET /dataorPOST /users, including their parameters and expected response schemas.
To quickly see what this looks like, the following video offers a great high-level introduction.
Power Platform Custom Connectors (Power Apps, Power Automate)
Watch the beginning of this video from Deepak Shrivastava for a concise explanation of what custom connectors are and why they are a crucial part of the Power Platform ecosystem.
Please watch the first 1 minute and 38 seconds of the video. The presenter explains the role of custom connectors when a pre-built one isn't available.
2. The Process: From Connector to Canvas
Consuming a custom connector in a Canvas App follows a logical, four-step process that will feel familiar from your programming experience:
- Add the Data Source: Add the custom connector to your app, creating a connection to the target API.
- Call the Action: Use a Power Fx formula to invoke a specific action defined in the connector (e.g., get weather data).
- Process the Response: Store the data returned by the API call in a variable or a collection.
- Display the Data: Bind UI controls (like labels or galleries) to the stored data to display it to the user.
Let's walk through this process with a hands-on example.
3. A Practical Walkthrough: The Weather API
We will use a video that demonstrates connecting to a simple public weather API. This example clearly shows all the steps from creating a simple connector to consuming it in an app. While you will watch the whole process, pay closest attention to the second half, where the focus shifts to the Canvas App.
PowerApps - Call custom APIs with custom connector instantly
This video from M365 & Modern Tech Hub provides an excellent end-to-end demonstration. It will show you how a custom connector is structured and, most importantly, how to call it from a Canvas App.
Please watch the video from 1:02 to 11:22. Part 1 (1:02 - 7:10): Pay attention to the Definition step. Notice the creation of the GetWeatherData action, the q (query) parameter for the city name, and how the expected JSON response is defined. This context is crucial for knowing how to call it later. Don't worry about memorizing the creation steps. Part 2 (7:47 - 11:22): This is the core of our lesson. Observe carefully how the presenter: Adds the custom connector as a data source. Writes the Power Fx formula on the dropdown's OnChange property to call the connector. Stores the result in a variable using Set(). Binds a label's Text property to the variable to display the temperature.
4. Deconstructing the Power Fx Formula
As you saw in the video, the key step is writing the Power Fx formula. Let's break down the pattern used:
// The action is triggered, for example, by a button's OnSelect or a dropdown's OnChange
Set(
varTemperature, // Create a global variable named varTemperature
'Interactive weather API connector'.GetWeatherData({q: Dropdown1.Selected.Value}).current.temp_f
);
Let's analyze this line by line:
Set(varName, value): This is a Power Fx function that sets the value of a global variable. This is analogous to assigning a value to a variable in C# or JavaScript.'Interactive weather API connector'.GetWeatherData(...): This is the call to the connector. The syntax is'ConnectorName'.ActionName(parameters). IntelliSense will guide you here once the connector is added to your app.{q: Dropdown1.Selected.Value}: This is the parameter being passed to theGetWeatherDataaction. The connector's definition specified a parameter namedq, so we provide it as a record. The value is taken from the selected item in the dropdown control..current.temp_f: After the function call, we use dot notation to navigate through the JSON object returned by the API to extract the specific value we need—in this case, the temperature in Fahrenheit. The ability to do this depends on having a well-defined response schema in the connector's definition.
The Microsoft Learn documentation provides another excellent example of this process. It uses a different API (Cognitive Services for sentiment analysis) and a slightly different pattern for storing the result, which helps to generalize the concept.
Use a custom connector from a Power Apps app
This official Microsoft documentation, 'Use a custom connector from a Power Apps app,' reinforces the steps we just saw. It uses a collection to store the results, which is a common and powerful pattern.
Please read the following sections: 'Create the app and add the custom connector': Confirms the first step of adding the connection. 'Add formulas to drive behavior': This is the most important section. Notice the use of ClearCollect() to store the API response in a collection. This is useful when the API might return multiple records. Also, note how they access the nested score: ...documents.score. 'Test the app': A quick read to see the final result.
The key formula from that article is:ClearCollect(sentimentCollection, SentimentDemo.DetectSentiment(...).documents.score)
Here, ClearCollect first clears the sentimentCollection and then adds the new results from the API call. This is the preferred method when you expect a list of items and want to bind the results to a gallery or data table.
Test your understanding!
Imagine the weather API from the video has been updated. When you call 'Interactive weather API connector'.GetWeatherData(...), it now returns an object where the main temperature is at current.temp_c, and the "feels like" temperature is at current.feelslike_c.
You want to display both values in a single label. Modify the Power Fx Set function to store the entire current object in a variable, and then write the formula for a label's Text property to display both values.
Show answer
1. Modify the Set function:
Instead of extracting a single value, store the entire current object. This is more efficient if you need multiple pieces of data from it.
// OnChange of the dropdown or OnSelect of a button
Set(
varCurrentWeather,
'Interactive weather API connector'.GetWeatherData({q: Dropdown1.Selected.Value}).current
);
2. Set the Label's Text property:
Use the variable and dot notation to access its properties.
// Text property of the label
"Current: " & varCurrentWeather.temp_c & "°C, Feels Like: " & varCurrentWeather.feelslike_c & "°C"
This approach is better practice than making two separate API calls or storing each value in a different variable, as it reduces redundancy and keeps related data together.
Conclusion
In this lesson, you've learned how to make your Canvas Apps significantly more powerful by consuming data from external REST APIs through custom connectors. This skill is a bridge between the low-code world of Power Apps and the limitless possibilities of backend services.
Key Takeaways:
- A custom connector acts as a proxy or wrapper around a REST API, defining its actions and data structures for Power Platform.
- To use a connector, you first add it as a data source to your Canvas App.
- You call connector actions using the Power Fx syntax:
'ConnectorName'.ActionName({param1: value1, ...}). - It's best practice to store the API response in a variable (
Set) for single items or a collection (ClearCollect) for lists of items. - You use dot notation to navigate the returned object and extract the specific data you need to display in your UI controls.
Up Next:
Now that you can retrieve data from both standard data sources and custom APIs, the next logical step is to perform more sophisticated operations on that data within your app. In our next lesson, we will dive deep into Power Fx to implement complex formulas combining Filter, LookUp, and With functions for advanced scenarios.