Creating a Custom API in Power Apps
Creating a Custom API in Power Apps
Custom APIs are a powerful feature in Microsoft Dataverse that allow developers to expose custom business logic as reusable operations. They provide a modern and secure way to execute server-side code and integrate Dynamics 365 and Power Platform applications with external systems.
Unlike traditional custom actions, Custom APIs offer better control, improved performance, and enhanced support for modern development practices.
Business Scenario
Suppose an organization wants to calculate customer loyalty scores based on multiple business rules. Instead of duplicating logic across Power Apps, Power Automate, and external applications, a Custom API can centralize the logic and expose it as a reusable service.
What is a Custom API?
A Custom API is a Dataverse message that allows developers to execute custom business logic through plugins and invoke it from:
- Power Apps
- Power Automate
- JavaScript
- Plugins
- External Applications
- Dynamics 365 CRM
Step 1: Create Custom API
- Open Power Apps Maker Portal.
- Navigate to Solutions.
- Select your Solution.
- Click New → Custom API.
- Provide API details.
Display Name: Calculate Loyalty Score Name: new_CalculateLoyaltyScore Binding Type: Global
Step 2: Create Request Parameter
Parameter Name: CustomerId Type: String
Request parameters pass data into the Custom API.
Step 3: Create Response Property
Property Name: LoyaltyScore Type: Integer
Response properties return results to callers.
Step 4: Create Plugin Logic
public class
CalculateLoyaltyScore :
IPlugin
{
public void Execute(
IServiceProvider serviceProvider)
{
var context =
(IPluginExecutionContext)
serviceProvider.GetService(
typeof(
IPluginExecutionContext));
context.OutputParameters[
"LoyaltyScore"] = 95;
}
}
Step 5: Register Plugin
- Open Plugin Registration Tool.
- Register Assembly.
- Create Step for Custom API.
- Publish Customizations.
Call Custom API Using JavaScript
var request = {
CustomerId:
"12345",
getMetadata: function () {
return {
boundParameter: null,
operationType: 0,
operationName:
"new_CalculateLoyaltyScore",
parameterTypes: {
CustomerId: {
typeName:
"Edm.String",
structuralProperty: 1
}
}
};
}
};
Xrm.WebApi.online
.execute(request);
Benefits of Custom APIs
- Reusable business logic
- Secure server-side processing
- Improved performance
- Better integration support
- Modern Dataverse architecture
- Centralized business rules
Key Takeaways
- Custom APIs expose reusable business logic in Dataverse.
- They support request parameters and response properties.
- Plugins execute the server-side processing.
- APIs can be called from Power Apps, JavaScript, and Power Automate.
- Custom APIs are the preferred modern alternative to Custom Actions.