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:

Step 1: Create Custom API

  1. Open Power Apps Maker Portal.
  2. Navigate to Solutions.
  3. Select your Solution.
  4. Click New → Custom API.
  5. 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

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

Important: Custom APIs are recommended over legacy Custom Actions for new development because they provide better control, flexibility, and integration capabilities.

Key Takeaways