Loading...

Step-by-Step Guide to Building Custom APIs in Dataverse

Step-by-Step Guide to Building Custom APIs in Dataverse

Custom APIs in Microsoft Dataverse allow developers and architects to create their own reusable, secure, and well-defined service endpoints that extend the standard Dataverse Web API. Instead of relying solely on out-of-the-box messages (like Create, Update, or Retrieve), Custom APIs let you define your own business operations — complete with input and output parameters — that encapsulate complex logic and can be called from Power Automate, Power Apps, or external systems. These APIs are registered once and behave like native Dataverse operations, making them ideal for implementing domain-specific logic, integrating with external services, or simplifying client-side development.

From a technical standpoint, a Custom API can be implemented through a plugin class, where the backend logic is written in C#. Developers can control security through privilege definitions and ensure consistency by centralizing key business rules. Custom APIs are particularly valuable in enterprise environments where modularity, performance, and scalability are essential — offering a clean, service-oriented alternative to using multiple plugin steps or custom workflow activities.  

As a developer working with Microsoft Dataverse, you’ve likely built Plugins, Workflows, or Actions. But have you explored Custom APIs?

Custom APIs provide a clean, reusable, and secure way to expose your business logic to other Dataverse components and even external systems.

In this blog, we’ll dive into the core components of a Custom API, their technical implementation, and provide a step-by-step guide to building one from scratch.

Why Should Developers Care About Custom APIs?

  • Reusable Logic – Call your API from Power Automate, Canvas Apps, or external clients.
  • Security-Controlled – Follows Dataverse security model.
  • Supports Complex Business Rules – Ideal for logic that goes beyond simple CRUD operations.
  • Externally Accessible – Available via Web API endpoints.

Core Components of a Custom API (Deep Dive)

Before building, understand what makes up a Custom API:

Custom API Definition

This is the blueprint that defines:

  • Name: Unique logical name (e.g., `new_CalculateDiscountAPI`)
  • Type: Bound (tied to entity) or Unbound (global)

Allowed HTTP Verb: POST

Action or Function:

  • Action → modifies data
  • Function → read-only

Developer Tip: Keep it Unbound for APIs serving multiple entities.

Request Parameters

These are the inputs your API expects.


Developer Tip: Always validate parameters in your Plugin code for null or invalid data.

Response Properties

These are the outputs your API returns.


Developer Tip: Keep responses lightweight and meaningful.

Plugin (C# Business Logic)

Your API will execute a Plugin written in C#.

 Access Input Parameters:

var customerId = (EntityReference)context.InputParameters["CustomerId"];

Set Output Parameters:

context.OutputParameters["DiscountPercentage"] = calculatedValue;

Best Practices:

  • Use early-bound entities for type safety
  • Keep transactions atomic
  • Handle exceptions gracefully with `InvalidPluginExecutionException`

Security Roles

  • Controls who can execute your API.
  • Assign privileges to roles within Dataverse.

Developer Tip: Don’t forget this step – without it, even admins may get “insufficient privilege” errors.

Invoker (Consumers)

Once created, your API can be called from:

  •  Power Automate
  •  Canvas Apps
  •  Model-driven Apps
  •  External systems (via Postman or custom HTTP clients)

Step-by-Step Guide: Building a Custom API

Step 1: Create Custom API

1. Go to Power Apps > Solutions > + New > Custom API.

2. Fill in:

    • Name: `new_CalculateDiscountAPI`
    • Binding: Unbound
    • Type: Action

3. Save & Publish.

Step 2: Add Request Parameters

1. Navigate to Custom API Request Parameters > + New.

2. Define inputs like `CustomerId (EntityReference)` and `OrderAmount (Decimal)`.

3. Save & Publish.

Step 3: Add Response Properties

1. Go to Custom API Response Properties > + New.

2. Define output like `DiscountPercentage (Decimal)`.

3. Save & Publish.

Step 4: Write the Plugin Code

Register a Plugin step for your API:



public class CalculateDiscountPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)

{

var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

var service = serviceFactory.CreateOrganizationService(context.UserId);

var customerId = (EntityReference)context.InputParameters["CustomerId"];

var orderAmount = (decimal)context.InputParameters["OrderAmount"];

decimal discount = (orderAmount > 1000) ? 0.1M : 0.05M; // Business Logic

context.OutputParameters["DiscountPercentage"] = discount;

}

}

Step 5: Register Plugin

Use Plugin Registration Tool:

  •  Register assembly
  •  Register step on your Custom API

Step 6: Test Your API

Test in:

  • Power Automate (Call Custom API action)
  • Canvas App (Patch with API call)
  • Postman (Using Web API endpoint `/api/data/v9.1/new_CalculateDiscountAPI`)

Best Practices for Developers

  • Keep business logic inside Plugin – keep API layer thin
  • Validate all inputs
  • Use Dependency Injection for external dependencies
  • Secure APIs via role-based access
  • Document API contracts (inputs/outputs)

Key Takeaways

  •  Custom APIs allow clean, reusable, and secure business logic.
  •  They are developer-friendly for complex integrations.
  •  They are accessible across Power Platform and external apps.

Summary:

Building Custom APIs in Microsoft Dataverse allows developers and solution architects to create reusable, secure, and extensible business logic that goes beyond standard Dataverse messages such as Create, Update, or Delete. A Custom API acts as a custom operation that can encapsulate complex server-side logic and expose it to other Dataverse components, Power Platform tools, or even external systems through the Web API.

This capability empowers teams to define custom request and response parameters, implement C# plugin handlers for execution, and register them with Dataverse using tools like the Plugin Registration Tool or Solution Explorer. Custom APIs help achieve clean separation of business logic, improved code maintainability, and consistent integration points for Power Automate, Canvas Apps, and Model-Driven Apps.

By using Custom APIs, organizations can standardize business rules, simplify integrations, and enhance performance by reducing the need for multiple plugins or complex workflows. When combined with security roles, environment variables, and solution-aware design, Custom APIs become a vital component for enterprise-grade, scalable Dataverse architecture.

Published on:

Learn more
Power Platform , D365 CE & Cloud
Power Platform , D365 CE & Cloud

Dynamics 365 CE, Power Apps, Powerapps, Azure, Dataverse, D365,Power Platforms (Power Apps, Power Automate, Virtual Agent and AI Builder), Book Review

Share post:

Related posts

Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy