Create a custom API with code
Note
This is an advanced topic that assumes you have already read and understood these topics:
You should also understand how to create Microsoft Dataverse records, using either the Web API or SDK for .NET. For more information see:
Because custom API data is saved in tables, you can programmatically create new APIs using either the Web API or the SDK for .NET.
The tables in Custom API tables describe all the properties you can set using code.
This code uses the CrmServiceClient with a early-bound programming style. You can also use ServiceClient. More information:
- Use CrmServiceClient constructors to connect to Dataverse
- Late-bound and Early-bound programming using the SDK for .NET
- Create entities using the SDK for .NET
This example shows the creation of a custom API action with one request parameter and one response property in a single operation. More information: Create related entities in one operation
This custom api is created as part of a solution with the uniquename CustomAPIExample
and is associated with a plug-in type with id = 00000000-0000-0000-0000-000000000001
.
string conn = $@"
Url = {url};
AuthType = OAuth;
UserName = {userName};
Password = {password};
AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d;
RedirectUri = app://58145B91-0C36-4500-8554-080854F2AC97;
LoginPrompt=Auto;
RequireNewInstance = True";
//var service = new ServiceClient(conn);
var service = new CrmServiceClient(conn);
// var service = new ServiceClient(conn);
//The plug-in type
var pluginType = new EntityReference("plugintype", new Guid("00000000-0000-0000-0000-000000000001"));
var solutionUniqueName = "CustomAPIExample";
//The custom API
var customAPI = new CustomAPI
{
AllowedCustomProcessingStepType = new OptionSetValue(0),//None
BindingType = new OptionSetValue(0), //Global
Description = "A simple example of a custom API",
DisplayName = "Custom API Example",
ExecutePrivilegeName = null,
IsFunction = false,
IsPrivate = false,
Name = "sample_CustomAPIExample",
PluginTypeId = pluginType,
UniqueName = "sample_CustomAPIExample",
IsCustomizable = new BooleanManagedProperty(false),
customapi_customapirequestparameter = new List<CustomAPIRequestParameter>()
{
new CustomAPIRequestParameter {
Description = "The StringParameter request parameter for custom API Example",
DisplayName = "Custom API Example String Parameter",
LogicalEntityName = null,
IsOptional = false,
Name = "sample_CustomAPIExample.StringParameter",
Type = new OptionSetValue(10), //String
UniqueName = "StringParameter",
IsCustomizable = new BooleanManagedProperty(false)
}
},
customapi_customapiresponseproperty = new List<CustomAPIResponseProperty>()
{
new CustomAPIResponseProperty {
Description = "The StringProperty response property for custom API Example",
DisplayName = "Custom API Example String Property",
Name = "sample_CustomAPIExample.StringProperty",
Type = new OptionSetValue(10), //String
UniqueName = "StringProperty",
IsCustomizable = new BooleanManagedProperty(false)
}
}
};
var createReq = new CreateRequest
{
Target = customAPI
};
createReq["SolutionUniqueName"] = solutionUniqueName;
Guid customAPIId = ((CreateResponse)service.Execute(createReq)).id;
See also
Create and use custom APIs
Custom API tables
Create a custom API using the plug-in registration tool
Create a custom API in Power Apps
Create a custom API with solution files