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 Organization Service. 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 Organization Service.

The tables in Create and use Custom APIs describe all the properties you can set using code. For the following code examples, please refer to the following tables:

Create a Custom API using the Web API

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 entity records 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 = 3926874e-387b-4045-94b2-a5cafe44b06f.

Request

POST [Organization URI]/api/data/v9.1/customapis HTTP/1.1
MSCRM.SolutionUniqueName: CustomAPIExample
Content-Type: application/json

{
    "allowedcustomprocessingsteptype": 0,
    "boundentitylogicalname": null,
    "uniquename": "sample_CustomAPIExample",
    "displayname": "Custom API Example",
    "bindingtype": 0,
    "executeprivilegename": null,
    "isfunction": false,
    "isprivate": false,
    "name": "sample_CustomAPIExample",
    "description": "A simple example of a Custom API",
    "iscustomizable": {
                "Value": false
            },
    "CustomAPIRequestParameters": [
        {
            "type": 10,
            "isoptional": false,
            "displayname": "Custom API Example String Parameter",
            "name": "sample_CustomAPIExample.StringParameter",
            "uniquename": "StringParameter",
            "logicalentityname": null,
            "description": "The StringParameter request parameter for Custom API Example",
            "iscustomizable": {
                "Value": false
                }
        }
    ],
    "CustomAPIResponseProperties": [
        {
            "type": 10,
            "name": "sample_CustomAPIExample.StringProperty",
            "logicalentityname": null,
            "displayname": "Custom API Example String Property",
            "uniquename": "StringProperty",
            "description": "The StringProperty response property for Custom API Example",
            "iscustomizable": {
                "Value": false
                }
        }
    ],
    "PluginTypeId@odata.bind": "plugintypes(3926874e-387b-4045-94b2-a5cafe44b06f)"
}

Response

HTTP/1.1 204 No Content
OData-Version: 4.0
OData-EntityId: [Organization URI]/api/data/v9.1/customapis(b532b299-4684-eb11-a812-0022481d298f)

Create a Custom API using the Organization Service

This code uses the CrmServiceClient with a early-bound programming style: More information:

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 = 3926874e-387b-4045-94b2-a5cafe44b06f.

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 CrmServiceClient(conn);

//The plug-in type
var pluginType = new EntityReference("plugintype", new Guid("3926874e-387b-4045-94b2-a5cafe44b06f"));
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
Create a Custom API in the maker portal
Create a Custom API with solution files

Note

Can you tell us about your documentation language preferences? Take a short survey.

The survey will take about seven minutes. No personal data is collected (privacy statement).