In Dynamics 365 Customer Engagement (CE), Custom APIs offer a powerful way to extend system functionality and create custom, reusable logic.
Example Scenario: Suppose you want to create a Custom API that exposes customer order data from Dynamics 365 to a third-party application used by your sales partners. Instead of relying on complex workflows or plugins, you can set up a secure and efficient Custom API to provide the required data.
Example Sample code that you can understand and use:
https://github.com/neeraj1202/Dynamics365_Customizations/blob/main/Dynamics365_Customizations/CustomAPI/GetCustomerOrders%20.cshttps://github.com/neeraj1202/Dynamics365_Customizations/tree/main/Dynamics365_Customizations/CustomAPI: How to Create a Custom API in Dynamics 365 CE: Exposing Limited Data to Third Party
Tip: Use the Custom API Manager tool in XRM Toolbox to easily create and manage Custom APIs. This tool simplifies the process, eliminating the need to manually add the Custom API to your solution.

Step 1: Set Up Your Solution
- Open Power Apps Maker Portal: Go to make.powerapps.com and select the desired environment.
- Create a New Solution: Navigate to Solutions > + New Solution. Give your solution a name and publisher.

Step 2: Defining the Custom API
- Add a Custom API: Within your solution, click on Add New > More > Custom API.
- Fill in the Fields:
- Name: Enter a unique name (e.g. GetCustomerOrders).
- Display Name: Provide a descriptive name for user reference. (e.g. Get Customer Orders for Third-Party)
- Binding Type: Choose whether your API operates on an entity, entity collection, or is unbound.
- Plugin Type: Select the Plugin type to associate with this API (after creating the plugin).
Tip: Keep descriptions clear so other developers can easily understand the API’s purpose.

Step 3: Define Request Parameters (Optional)
- Navigate to Custom API Request Parameters: Under the Custom API in your solution, click + New.
- Configure Parameters:
- Name: Provide a name (e.g.,
customerId
). - Type: Select the data type (e.g.,
String
). - Is Optional: Indicate whether the parameter is optional.
Example : Suppose you want to receive some data from the user (such as account number or date) on which you want to retrive the data in dynamics 365 and send the response back to the user.
Step 4: Define Response Properties (Optional)
- Create Response Properties similar to request parameters. These specify the expected output from the API.
- Name and Type fields follow the same approach as parameters.
For example we will use below data
- OrderID (String)
- CustomerName (String)
- OrderStatus (String)
- TotalValue (Decimal)
Step 5: Develop and Register the Plugin
- Create a Plugin Project: Use Visual Studio and the Dynamics 365 SDK to create a C# plugin that contains the logic you want for your Custom API.
- Register the Plugin: Use the Plugin Registration Tool to associate the custom plugin with your Custom API.
- Message: The message name should match your Custom API name.
- Primary Entity: Leave this blank for unbound APIs.
Example Logic: The plugin queries the SalesOrder
entity, filters by CustomerID
, and returns the order data.
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Purpose: This Custom API retrieves sales orders for a given customer, returning details
//like the order ID, name, status, and total amount.
namespace Dynamics365_Customizations.CustomAPI
{
public class GetCustomerOrdersPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Retrieve input parameter (customerId)
if (!context.InputParameters.Contains("customerId") || !(context.InputParameters["customerId"] is string customerId))
{
throw new InvalidPluginExecutionException("CustomerId is required.");
}
// Query the SalesOrder entity for the specified customer
QueryExpression query = new QueryExpression("salesorder")
{
ColumnSet = new ColumnSet("salesorderid", "name", "statuscode", "totalamount"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("customerid", ConditionOperator.Equal, new Guid(customerId))
}
}
};
EntityCollection orders = service.RetrieveMultiple(query);
// Transform the results into a list of dictionaries for JSON serialization
var orderList = orders.Entities.Select(order => new Dictionary<string, object>
{
{ "OrderID", order.Id.ToString() },
{ "CustomerName", order.GetAttributeValue<string>("name") },
{ "OrderStatus", order.FormattedValues["statuscode"] },
{ "TotalValue", order.GetAttributeValue<Money>("totalamount")?.Value ?? 0 }
}).ToList();
// Serialize the results to JSON
string jsonResponse = Newtonsoft.Json.JsonConvert.SerializeObject(orderList);
// Return the JSON response as an output parameter
context.OutputParameters["customerOrders"] = jsonResponse;
}
}
}

Step 6: Test the Custom API
- Use Postman, Power Automate, or another tool to test your API.
- Send requests to your Custom API endpoint to ensure correct behavior and responses.
- Use Custom API Tester in XRM tool box to test your api faster. Please find the steps on how to test your custom API.



Benefits of This Approach:
- Efficient Data Exposure: Directly exposes only the needed data securely.
- Customizable Logic: Modify logic as per business needs without impacting other processes.
- Control and Security: Easily manage permissions, authentication, and data visibility.
Conclusion
Custom APIs in Dynamics 365 CE offer flexible, powerful ways to create tailored logic and integrations. By defining custom messages, parameters, and secure execution paths, you can extend CRM functionality beyond traditional limits.

Senior Power Platform Consultant with deep expertise in customization, optimization, and integrations. Committed to giving back to the CRM community by breaking down complex concepts, sharing real-world insights, and helping others succeed with Dynamics 365 & Azure.
No responses yet