Microsoft Power Apps — Using REST API in Microsoft Power Platform
In this blog post, we will have a look at how to collect data from a REST-API source into a PowerApps collection and Update data using REST API and Microsoft Power Automate.
Loading/Consuming data from REST API into PowerApps
Step 1: Create an Instant cloud flow with a PowerApps trigger
Step 2: Add an HTTP action. Set properties
Method = GET
URI = REST API’s URL from where data is to be retrieved
Note: If you wish to apply filter criteria or pass authentication parameters you can do so using Headers
Step 3: Add a Compose action. Set Inputs using expressions outputs(‘HTTP’)[‘body’][‘data’]
Note: Make sure that an Array input is provided to the Compose action
Step 4: Add a Select action. Set From to Outputs of Compose action. And Map fields according to your data.
{
“id”: @{item()?[‘id’]},
“employee_name”: @{item()?[‘employee_name’]},
“employee_salary”: @{item()?[‘employee_salary’]},
“employee_age”: @{item()?[‘employee_age’]}
}
Note: While mapping values use expression item()?[‘field_name’]
Step 5: Add a Response action. Set Body to outputs of the Select action. Set JSON schema using the below example
[{
“id”: “1”,
“employee_name”: “Tiger Nixon”,
“employee_salary”: “320800”,
“employee_age”: “61”
}]
Note: If is API can have null values for any data field then set “type”: [“null”, “datatype_of_field”]
Step 6: Add your flow to PowerApps Power Automate>>Add Flow>>Select your flow. Once flow is added insert a button and set OnSelect = ClearCollect(OrderData,’Your_Flow_Name’.Run()). Add a Gallery or a Data table to view your data.
Using PowerApps to Update source data using PUT/POST REST API
Step 1: Create an Instant cloud flow with a PowerApps trigger
Step 2: Add Initialize Variable action. Set datatype to String and value to Ask value from PowerApps
Step 3: Add another Initialize Variable. Set datatype to Array and value to json(variables(‘StringArray’))
Note: Use the variable name that you set in the previous action.
Step 4: Add HTTP action. Set properties
Method = PUT
URI = REST API’s URL
Body = Initialize_variable2_Name i.e., JSONArray
Note: If you wish to apply filter criteria or pass authentication parameters, you can use Headers. After saving you can use the previously given sample record to test the flow
Step 5: Use the previously created gallery for selecting a Record. Create a form or use TextInputs controls to get user inputs. Insert a button and Add the following to OnSelect:
ClearCollect(
UpdateAPIRecords,
ForAll(
Filter(
OrderData,
id = Gallery1.Selected.id
),
{
id: Gallery1.Selected.id,
employee_age: Gallery1.Selected.employee_age,
employee_name: Gallery1.Selected.employee_name,
employee_salary: Gallery1.Selected.employee_salary
}
)
);
Set(
ScriptValue,
JSON(UpdateAPIRecords)
);
‘UpdateAPI-Test’.Run(ScriptValue);
Navigate(MainScreen)
Note:
· You can have Status returned by API to PowerApps using response action
· You can use this flow to edit records in bulk.
· Make sure API’s Body JSON Schema matches the collection that you provide as an input to the Flow