MSBA-272 · Lesson 4 of 12 · 20 min
Custom APIs
▶ Video coming soon A video version of this lesson is on the way.
Once the fee plug-in was stable, other people wanted the same answer. The Request a Transcript app in Teams needed to show a student adviser the fee before the request was submitted. A flow that sent fee quotes to employers who paid for staff transcripts needed it. Kwabena Asante-Lowe wanted to check historical fees in a report. Copying the fee rules into a canvas formula, a flow and a Power BI measure would create three versions that drifted apart the first time the registrar changed a rule. Signe's answer, planned in MSBA-270, was a custom API: a new Dataverse message, Calculate Transcript Fee, that any app, flow or integration could call and that ran the same code as the plug-in.
What a custom API is
A custom API defines a new message in Dataverse, with a name, typed input parameters and typed response properties. Once it exists, it behaves like the messages Microsoft provides: it appears in the Web API, it can be called from the SDK, it shows up as an action in Power Automate's Dataverse connector, and it can be called from Power Fx in canvas apps. The logic behind it is normally a plug-in registered as the API's main operation, which reads the inputs from InputParameters and writes the results to OutputParameters.
Custom APIs replaced the older practice of defining custom process actions with the classic workflow designer for this purpose. Microsoft recommends custom APIs for new work, and they offer more control: function or action behavior, binding to a table, a privilege required to call them, and control over whether other developers may register their own steps on the message.
The definition
A custom API is stored as rows in three tables: Custom API, Custom API Request Parameter and Custom API Response Property. Those rows can be created in several ways: in PRT, in the maker portal from the solution explorer, through code, or by editing solution files. Wiremu used PRT for the first definition because its form put every property on one screen, then exported the solution so the definition lived in source control with everything else.
| Property | Meaning | Calculate Transcript Fee |
|---|---|---|
| UniqueName | The message name, which must start with the publisher prefix | lcc_CalculateTranscriptFee |
| BindingType | Global (no table), Entity (one row) or EntityCollection (a table) | Global |
| IsFunction | A function is called with GET and returns data; an action is called with POST. Cannot be changed after creation | Yes |
| AllowedCustomProcessingStepType | None, Async Only, or Sync and Async: which steps others may register on the message | None |
| ExecutePrivilegeName | An existing privilege the caller must hold | Not set; any user may ask for a quote |
| IsPrivate | Hides the API from metadata so only its owner's code should call it | No |
| PluginTypeId | The plug-in type that runs as the main operation | CalculateTranscriptFeeApi |
| Is Customizable | Whether the definition can be changed in other environments after managed import | False |
Two choices deserved discussion. Making the API a function suited a calculation that changed nothing. Functions are called with GET, can be cached by clients and make the read-only intent obvious. Microsoft documents that a function must have at least one response property, and because its parameters travel in the URL, the URL must stay within a size limit, which ruled functions out for anything that took large inputs. Because IsFunction cannot be changed after the API is created, the decision had to be right the first time.
Setting AllowedCustomProcessingStepType to None meant that no one else could register plug-ins on lcc_CalculateTranscriptFee. Signe wanted that: a fee calculation with extra steps registered by another team could return different answers in different environments. For the Waive Transcript Fee API, which changed data, she chose Async Only, so that an auditing step or a business event subscription could react after a fee was waived without being able to change the result.
Parameters and response properties
Each request parameter and response property has a unique name, a display name, a type, and for parameters an IsOptional flag. The available types cover what most APIs need: Boolean, DateTime, Decimal, Entity, EntityCollection, EntityReference, Float, Integer, Money, Picklist, String, StringArray and Guid.
| Name | Kind | Type | Optional |
|---|---|---|---|
| DeliveryMethod | Request parameter | Picklist | No |
| RequestDate | Request parameter | DateTime | Yes; defaults to today in the college's time zone |
| StudentId | Request parameter | Guid | Yes; needed to apply the recent-graduate rule |
| Fee | Response property | Money | Not applicable |
| RateDescription | Response property | String | Not applicable |
Names matter more than they seem. Parameter and property names become part of a contract with every caller, and changing them later breaks apps and flows. Signe asked for plain names without prefixes, consistent capitalization, and no abbreviations a business user would not recognize in a flow designer.
The main operation
The plug-in behind a custom API is written like any other, but it reads its inputs from InputParameters by the parameter names and sets OutputParameters by the response property names. It is not registered with a step. Instead, the custom API's PluginTypeId points at the plug-in type, and Dataverse runs it in the MainOperation stage whenever the message is called.
public class CalculateTranscriptFeeApi : PluginBase
{
public CalculateTranscriptFeeApi(string unsecure, string secure)
: base(typeof(CalculateTranscriptFeeApi)) { }
protected override void ExecuteDataversePlugin(ILocalPluginContext local)
{
var ctx = local.PluginExecutionContext;
var method = (OptionSetValue)ctx.InputParameters["DeliveryMethod"];
DateTime asOf = ctx.InputParameters.Contains("RequestDate")
? (DateTime)ctx.InputParameters["RequestDate"]
: CollegeClock.Today();
Guid? student = ctx.InputParameters.Contains("StudentId")
? (Guid?)ctx.InputParameters["StudentId"] : null;
var calculator = new FeeCalculator(new TranscriptRateReader(local.InitiatingUserService));
FeeResult result = calculator.Calculate(method.Value, asOf, student);
ctx.OutputParameters["Fee"] = new Money(result.Amount);
ctx.OutputParameters["RateDescription"] = result.Description;
}
}The important design move was the shared FeeCalculator class. Wiremu refactored the original plug-in so that both it and the API called FeeCalculator, which held the rules and nothing else. The plug-in now did only the plug-in work, reading the Target and images and setting the fee, and the API did only the API work, reading parameters and setting outputs. A rule change went into one class with one set of tests.
Calling the API
Because Calculate Transcript Fee is an unbound function, a Web API client calls it with GET and passes parameters in the URL, using parameter aliases for complex values. Actions are called with POST and a JSON body; bound actions are called on the row they are bound to.
GET [Organization URI]/api/data/v9.2/lcc_CalculateTranscriptFee(DeliveryMethod=@m,RequestDate=@d)?@m=3&@d=2026-11-18T00:00:00Z
POST [Organization URI]/api/data/v9.2/lcc_transcriptrequests(00000000-0000-0000-0000-000000000001)/Microsoft.Dynamics.CRM.lcc_WaiveTranscriptFee
Content-Type: application/json
{ "Reason": "Graduate within twelve months; first copy" }From the SDK, a developer creates an OrganizationRequest with the message name, sets the parameters and calls Execute. Power Automate makers find the API under the Dataverse connector's Perform an unbound action or Perform a bound action, where the parameters appear as fields. Canvas app makers can call it through the Environment object in Power Fx once the Environment data source is added. In each case the caller gets the same fee from the same code.
Securing a custom API
A custom API without an ExecutePrivilegeName can be called by any user who can sign in to the environment. That was right for a fee quote, but not for Waive Transcript Fee, which must be restricted to Transcript Officers. The ExecutePrivilegeName property names a privilege the caller must hold. Microsoft documents that it must be an existing privilege; a custom API cannot create its own.
Lumenvale's solution was the one Signe had planned in MSBA-270. She created a small table, Transcript Fee Waiver, whose only purpose was to generate privileges, and named one of its privileges as the API's execute privilege. That privilege was then granted only in the Transcript Officer role. Anyone without it received an error when they called the API, whatever app they used. The main operation also checked that the request was in a state where waiving was allowed, so the security role and the business rule each did their own job.
Worked problem
Designing the Waive Transcript Fee API
Transcript Officers occasionally waive a fee, for example after an error by the college. The waiver must record a reason, set the fee to zero, record who waived it, and notify the finance office. Only Transcript Officers may do it, from the Transcript Office app or a flow.
Show solution
- Make it an action, not a function, because it changes data: IsFunction is No and it is called with POST.
- Bind it to the transcript request table (BindingType Entity), so it acts on one request and appears as a bound action.
- Add a request parameter Reason, a required String, and a response property WaivedOn, a DateTime.
- Set ExecutePrivilegeName to a privilege of the Transcript Fee Waiver table granted only in the Transcript Officer role.
- Set AllowedCustomProcessingStepType to Async Only, so a business event subscription can notify finance after the waiver without changing it.
- Write the main operation to check the request status, set the fee to zero and record the reason and the user, in the same transaction.
Answer: A bound action with one required parameter, an execute privilege held only by Transcript Officers, and asynchronous steps allowed for notifications. It stayed a plan in Wiremu's developer environment, to be built with the rest of the registrar work.
Custom APIs as business events
A custom API can also be used as a business event. When AllowedCustomProcessingStepType permits asynchronous steps, other systems can subscribe to the message: Power Automate's Dataverse trigger When an action is performed can start a flow when the API is called, and service endpoints such as Azure Service Bus can receive it. Lumenvale planned to use this for the waiver notification, so the API would not need to know who should be told. Signe preferred this to sending an email from inside the plug-in, because the notification could change without touching the code.
Background operations
Microsoft also documents a preview feature, background operations, that lets a caller run a custom API asynchronously and be notified when it finishes, for long work that would otherwise hit the two-minute limit. Only custom APIs can be run this way. Signe noted it for the preventive maintenance capstone in MSBA-270, where lcc_GeneratePreventiveJobs might one day create many rows, but did not plan to use a preview feature in production.
Changing a custom API safely
A custom API is a contract, and contracts are easier to extend than to change. Signe gave Wiremu four rules. New optional parameters can be added at any time, because existing callers simply do not send them. New response properties can be added too, because callers ignore what they do not read. Renaming or removing a parameter or property breaks every caller that uses it, so it should not happen once an API is in use. And a change in meaning, such as returning the fee including tax when it used to exclude it, is the most dangerous change of all, because nothing fails and every report quietly becomes wrong.
When a breaking change is truly needed, the safe route is a second API with a new name, such as lcc_CalculateTranscriptFeeV2, released alongside the first. Callers move over at their own pace, and the old API is removed only when nothing calls it. Lumenvale's pull request checklist flagged any change to an existing parameter in a pull request, so the question was always asked out loud.
Microsoft also notes that some properties of a custom API and its parameters cannot be changed after creation, including IsFunction and a parameter's type. Changing those means deleting and recreating the definition, which in a managed environment means a solution upgrade that removes the old component. For an API already called by flows, that is effectively the same as creating a new API, so the rules above apply anyway.
Documenting and testing the API
An API is only reusable if people can find out how to use it. Every custom API at Lumenvale had a short page in the docs folder of the repository: its purpose, its parameters with examples of valid values, its response properties, the privilege it required, the errors it could return and the name of the team that owned it. The display names and descriptions on the Custom API, parameter and property rows were filled in too, because the Power Automate designer shows them to makers. A maker choosing "Perform an unbound action" saw "Calculate Transcript Fee: returns the fee for a delivery method on a date" rather than a bare message name.
Testing had two layers, as for plug-ins. FeeCalculator had unit tests for every rule and every boundary date. The API itself had a small set of integration tests that called it through the Web API in Wiremu's developer environment, checked the fee for each delivery method, checked that an invalid delivery method returned a clear error, and checked that a user without the waiver privilege could not call Waive Transcript Fee. Those tests ran on demand rather than in every build, because they needed a live environment.
When a custom API is not the answer
Not every shared piece of logic needs a custom API. If the logic only ever runs when a row is saved, a plug-in step is enough. If the result is a simple formula over columns of the same row, a formula column computes it without code. If only one canvas app needs it and the rule is small, a Power Fx function in the app or a Dataverse function, covered in lesson 9, may be simpler to own. A custom API earns its place when several different callers need the same server-side answer, when the logic needs more than a formula can express, or when the operation must be secured by a privilege. Calculate Transcript Fee met all three conditions.
Common mistakes
MistakeCreating a custom API as an action when it only reads data.
InsteadUse a function for read-only operations; IsFunction cannot be changed later.
MistakeRegistering a step for the main operation plug-in.
InsteadLink the plug-in through the custom API's PluginTypeId and register no step.
MistakeExpecting ExecutePrivilegeName to create a new privilege.
InsteadUse an existing privilege, for example from a table created to generate privileges.
MistakeAllowing synchronous steps by others on a calculation API.
InsteadChoose None or Async Only so others cannot change the result.
MistakeDuplicating rules in the plug-in and the API.
InsteadPut the rules in a shared class that both call.
Summary and key terms
- A custom API defines a new Dataverse message with typed request parameters and response properties, usually implemented by a plug-in.
- Key properties are UniqueName, BindingType, IsFunction, AllowedCustomProcessingStepType, ExecutePrivilegeName and IsPrivate.
- Functions use GET and need at least one response property; actions use POST; IsFunction cannot change after creation.
- The main operation plug-in reads InputParameters, sets OutputParameters and is linked without a step.
- Custom APIs can be called from the Web API, SDK, Power Automate and Power Fx, secured with an execute privilege and used as business events.
- Custom API
- A developer-defined Dataverse message with typed inputs and outputs.
- Function
- A custom API that returns data without changing it and is called with GET.
- Action
- A custom API that may change data and is called with POST.
- Bound custom API
- A custom API that operates on a row or a table, set by BindingType.
- ExecutePrivilegeName
- The existing privilege a caller must hold to call the API.
- Business event
- A message that other systems can subscribe to, such as a custom API allowing asynchronous steps.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Which HTTP method does a client use to call a custom API function?
Show answer
GET.
How is the main operation plug-in connected to the custom API?
Show answer
Through the PluginTypeId property; no step is registered.
Why did Lumenvale create the Transcript Fee Waiver table?
Show answer
To generate a privilege to use as the waiver API's execute privilege.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.