How to create an API in APIM programmatically using Azure SDK for .Net

Published Apr 12 2022 10:14 PM 1,442 Views
Microsoft

Azure API Management (APIM) helps organizations publish APIs to external, partner, and internal developers to unlock the potential of their data and services.

 

It is easy for us to add the API manually in the Azure portal, or there are several options for you to create the API programmatically.

  • ARM template
  • Powershell or Azure CLI
  • Bicep
  • Visual Studio Code
  • .Net SDK

In this article, we will provide a sample for creating an API via Azure SDK for .Net – APIManagement. and will use the latest preview package: Microsoft.Azure.Management.ApiManagement v8.0.0-preview

 

The complete code is shown at the end of this article.

 

To implement importing an API in APIM by .net SDK, the primary method is to create an API client to call REST API. It is like what we do in other ways. Basically, we need an API client and call the Create or Update REST API.

 

Considering a service credential is needed when the API client interacts with Azure resources, we need first to create an App on behalf of the API client and then grant permission in APIM.

 

In summary, there are mainly 3 steps to creating an API via.Net SDK.

  1. Authentication- grant permission for your API client to access APIM.
  2. API client creation - create an API client to send HTTP requests.
  3. API creation – set the API properties & send the HTTP request.

This article will provide friendly guidance for new APIM users.

 

Prerequisite:

  • Microsoft Visual Studio 2019. Visual Studio 2019, including the free community edition, can be downloaded here.
  • Microsoft Azure subscription. To use Azure services, including APIM and App registration, you need a subscription.
  • Create an APIM service. You could also use the C# code to create a new APIM instance. However, this article focuses on creating a new API via .net SDK, so we suppose you already have an APIM instance. If you don’t have an existing APIM instance, you can create it by following instructions from this article.

 

Package Installation:

1. In visual studio, create a console application: start visual studio 2019 ->select create a new project -> select Console Application 

 

Hildat_0-1649782760765.png

 

2.select Tools > NuGet Package Manager > Package Manager Console from the menu.

3. Run the following command to install the Microsoft.Azure.Management.ApiManagement package:

 

Install-Package Microsoft.Azure.Management.ApiManagement -Version 8.0.0-preview

 

4. Run the below command to install Microsoft.IdentityModel.Clients.ActiveDirectory package.

 

Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 5.2.9

 

 

Authentication:

 

To construct an API client, Credentials is required for the client to connect to Azure. This section will introduce how to get the value of servicecredential.

 

Step1. Register an application in Azure AD to represent the API client.

a. In the Azure portal, search for and select App registrations.

b. Select New registration.

c. In the Register an application page, enter your application’s registration information.

-Name. You can select a meaningful name for this app. In my sample, I use APIM-client.

-Select an option that suits your scenario in the Supported account types section.

-Leave the Redirect URI section empty.

d. Click Register to create the application.

Hildat_0-1649778179258.png

e. On the app Overview page, find the Application(client) ID value and record it for later.

Hildat_1-1649778179270.png

 

Step2. Create a client secret for this application to use in a subsequent step.

a. Under the Manage section of the side menu, select Certificates&secrets.

b. Under Client secrets, select New client secret.

c. In the Add a client secret page, enter the Description and the expiration time.

d. Click on Add.

e. You can find the client secret Value in the portal, and you need to record it for later use.

Hildat_2-1649778179282.png

 

Step3. Grant delegated permission to your Application in APIM Access control (IAM)

  1. Navigate to your APIM.
  2. Under Access control (IAM), select Add -> Add role assignment.
  3. Assign a role to the above created application, which should be able to create or update API in this APIM, such as APIM service contributor.

Hildat_3-1649778179292.png

If you want to understand the permission for a different role, you can click on view, and all permissions under the selected role will be displayed.

Hildat_4-1649778179304.png

 

Now, we can use the above created application to get access to APIM. To get the value of service credentials, we need the Azure tenant ID, the above created application client ID and client seceret value

The sample code below provides how to get the servicecredential token and initial an API client.

 

 

    //create service credentials
    public class AzureApiManagementServiceCredentials : ServiceClientCredentials
    {
        private readonly string _tenantId;
        private readonly string _clientId;
        private readonly string _clientSecret;

        public AzureApiManagementServiceCredentials(string tenantId, string clientId, string clientSecret)
        {
            _tenantId = tenantId;
            _clientId = clientId;
            _clientSecret = clientSecret;
        }

        public override void InitializeServiceClient<T>(ServiceClient<T> client)
        {
            var authenticationContext = new AuthenticationContext($"https://login.microsoftonline.com/{_tenantId}");
            var credential = new ClientCredential(clientId: _clientId, clientSecret: _clientSecret);
            var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;

            if (result == null)
            {
                throw new InvalidOperationException($"Failed to obtain the JWT token for Azure API Management Rest connection for user {_clientId}");
            }

            client.HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken);
        }

    }
    // ref github-https://github.com/Azure/azure-sdk-for-net/issues/4727

 

 

APIM Client creation:

After getting the value of servicecredential, we can create an API client by using ApiManagementClient Class.

Don’t forget to assign your subscription ID to the subscriptionId properties.

 

 

            string tenantId = "[Azure tenant ID]";
            string clientId = "[application client ID]";
            string clientSecret = "[application client secret]";

            //construct client
            var serviceCredentials = new AzureApiManagementServiceCredentials(tenantId, clientId, clientSecret);

            apiClient = new ApiManagementClient(serviceCredentials);

            apiClient.SubscriptionId = "[APIM subscription ID]";

 

 

Create API

 

Step 1. APIManagementmodel Construction – set API properties

Similar to putting API properties in the Request body when creating API via Create or Update REST API, we need to construct an API parameter model to define your API via using ApiCreateOrUpdateParameter Class.

 

You could set your API properties in this part—for example, API display name, API path, and protocols.

 

Below is the sample. I just created an open API called “test-httpbin2”.

 

            apiProperties = new ApiCreateOrUpdateParameter();
            apiProperties.DisplayName = "test-httpbin2";
            apiProperties.Path = "testhttpbin2";
            apiProperties.ApiType = "http";

            var protocols = new List<string>();
            protocols.Add("http");
            protocols.Add("https");

            apiProperties.Protocols = protocols;

 

 

Also, you could customize your API by adding the value for the properties below. For more details, please see this document.

 

Hildat_0-1649780310948.png

 

Step 2. Send Request to create API in APIM

Set the value of your resource group name, APIM service name, and apiID.

 

            string resourceGroupName = "start";
            string APIMserviceName = "apim-study-Hilda";
            string apiID = "api01";

 

 

Now we can use the APIM client to send an HTTP request to create our API

 

 

            var response = await apiClient.Api.CreateOrUpdateAsync(resourceGroupName, APIMserviceName, apiID, apiProperties);

 

 

Test result:

In the Azure portal, you can find the API has been created successfully.

Hildat_10-1649778179347.png

 

The complete code is shown below:

 

using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Azure.Management.ApiManagement;
using Microsoft.Azure.Management.ApiManagement.Models;
using Microsoft.Rest;
using Microsoft.IdentityModel.Clients.ActiveDirectory;


namespace createAPI

{
    //create service credentials
    public class AzureApiManagementServiceCredentials : ServiceClientCredentials
    {
        private readonly string _tenantId;
        private readonly string _clientId;
        private readonly string _clientSecret;

        public AzureApiManagementServiceCredentials(string tenantId, string clientId, string clientSecret)
        {
            _tenantId = tenantId;
            _clientId = clientId;
            _clientSecret = clientSecret;
        }

        public override void InitializeServiceClient<T>(ServiceClient<T> client)
        {
            var authenticationContext = new AuthenticationContext($"https://login.microsoftonline.com/{_tenantId}");
            var credential = new ClientCredential(clientId: _clientId, clientSecret: _clientSecret);
            var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;

            if (result == null)
            {
                throw new InvalidOperationException($"Failed to obtain the JWT token for Azure API Management Rest connection for user {_clientId}");
            }

            client.HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken);
        }

    }
    // ref github-https://github.com/Azure/azure-sdk-for-net/issues/4727


    class Program

    {

        //API client
        static ApiManagementClient apiClient;
        //APIManagementmodel, for request body
        static ApiCreateOrUpdateParameter apiProperties;


        static async  Task Main()
        { 
            string tenantId = "[Azure tenant ID]";
            string clientId = "[application client ID]";
            string clientSecret = "[application client secret]";

            //construct client
            var serviceCredentials = new AzureApiManagementServiceCredentials(tenantId, clientId, clientSecret);

            apiClient = new ApiManagementClient(serviceCredentials);

            apiClient.SubscriptionId = "[APIM subscription ID]";

            //construct api properties <-
            apiProperties = new ApiCreateOrUpdateParameter();
            apiProperties.DisplayName = "[API Name]";
            apiProperties.Path = "[API path]";
            apiProperties.ApiType = "[API type]";

            var protocols = new List<string>();
            protocols.Add("http");
            protocols.Add("https");

            apiProperties.Protocols = protocols;
            //create api
            string resourceGroupName = "[resource group name]";
            string APIMserviceName = "[APIM service name]";
            string apiID = "[API ID]";
            var response = await apiClient.Api.CreateOrUpdateAsync(resourceGroupName, APIMserviceName, apiID, apiProperties);

        }
    }
}

 

 

 

 

 

 

 

 

 

 

1 Comment
%3CLINGO-SUB%20id%3D%22lingo-sub-3283347%22%20slang%3D%22en-US%22%3EHow%20to%20create%20an%20API%20in%20APIM%20programmatically%20using%20Azure%20SDK%20for%20.Net%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3283347%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20API%20Management%20(APIM)%20helps%20organizations%20publish%20APIs%20to%20external%2C%20partner%2C%20and%20internal%20developers%20to%20unlock%20the%20potential%20of%20their%20data%20and%20services.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20is%20easy%20for%20us%20to%20add%20the%20API%20manually%20in%20the%20Azure%20portal%2C%20or%20there%20are%20several%20options%20for%20you%20to%20create%20the%20API%20programmatically.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EARM%20template%3C%2FLI%3E%0A%3CLI%3EPowershell%20or%20Azure%20CLI%3C%2FLI%3E%0A%3CLI%3EBicep%3C%2FLI%3E%0A%3CLI%3EVisual%20Studio%20Code%3C%2FLI%3E%0A%3CLI%3E.Net%20SDK%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIn%20this%20article%2C%20we%20will%20provide%20a%20sample%20for%20creating%20an%20API%20via%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Foverview%2Fazure%2Fapimanagement%2Fmanagement%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20SDK%20for%20.Net%20%E2%80%93%20APIManagement.%3C%2FA%3E%20and%20will%20use%20the%20latest%20preview%20package%3A%20%3CSTRONG%3E%3CFONT%20color%3D%22%23696969%22%3EMicrosoft.Azure.Management.ApiManagement%20v8.0.0-preview%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EThe%20complete%20code%20is%20shown%20at%20the%20end%20of%20this%20article.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20implement%20importing%20an%20API%20in%20APIM%20by%20.net%20SDK%2C%20the%20primary%20method%20is%20to%20create%20an%20API%20client%20to%20call%20REST%20API.%20It%20is%20like%20what%20we%20do%20in%20other%20ways.%20Basically%2C%20we%20need%20an%20API%20client%20and%20call%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Frest%2Fapi%2Fapimanagement%2Fcurrent-ga%2Fapis%2Fcreate-or-update%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECreate%20or%20Update%20REST%20API%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConsidering%20a%20service%20credential%20is%20needed%20when%20the%20API%20client%20interacts%20with%20Azure%20resources%2C%20we%20need%20first%20to%20create%20an%20App%20on%20behalf%20of%20the%20API%20client%20and%20then%20grant%20permission%20in%20APIM.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20summary%2C%20there%20are%20mainly%203%20steps%20to%20creating%20an%20API%20via.Net%20SDK.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EAuthentication-%20grant%20permission%20for%20your%20API%20client%20to%20access%20APIM.%3C%2FLI%3E%0A%3CLI%3EAPI%20client%20creation%20-%20create%20an%20API%20client%20to%20send%20HTTP%20requests.%3C%2FLI%3E%0A%3CLI%3EAPI%20creation%20%E2%80%93%20set%20the%20API%20properties%20%26amp%3B%20send%20the%20HTTP%20request.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EThis%20article%20will%20provide%20friendly%20guidance%20for%20new%20APIM%20users.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-1095715262%22%20id%3D%22toc-hId-1116987114%22%20id%3D%22toc-hId-1116987114%22%3E%3CSTRONG%3EPrerequisite%3A%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EMicrosoft%20Visual%20Studio%202019.%20%3C%2FSTRONG%3EVisual%20Studio%202019%2C%20including%20the%20free%20community%20edition%2C%20can%20be%20downloaded%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fvisualstudio%2Freleases%2F2019%2Frelease-notes%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EMicrosoft%20Azure%20subscription.%3C%2FSTRONG%3E%20To%20use%20Azure%20services%2C%20including%20APIM%20and%20App%20registration%2C%20you%20need%20a%20subscription.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ECreate%20an%20APIM%20service.%3C%2FSTRONG%3E%20You%20could%20also%20use%20the%20C%23%20code%20to%20create%20a%20new%20APIM%20instance.%20However%2C%20this%20article%20focuses%20on%20creating%20a%20new%20API%20via%20.net%20SDK%2C%20so%20we%20suppose%20you%20already%20have%20an%20APIM%20instance.%20If%20you%20don%E2%80%99t%20have%20an%20existing%20APIM%20instance%2C%20you%20can%20create%20it%20by%20following%20instructions%20from%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fget-started-create-service-instance%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ethis%20article%3C%2FA%3E.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH4%20id%3D%22toc-hId--711739201%22%20id%3D%22toc-hId--690467349%22%20id%3D%22toc-hId--690467349%22%3E%26nbsp%3B%3C%2FH4%3E%0A%3CH4%20id%3D%22toc-hId-1775773632%22%20id%3D%22toc-hId-1797045484%22%20id%3D%22toc-hId-1797045484%22%3EPackage%20Installation%3A%3C%2FH4%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3E1.%20In%20visual%20studio%2C%20create%20a%20console%20application%3A%20start%20visual%20studio%202019%20-%26gt%3Bselect%20%3CSTRONG%3Ecreate%20a%20new%20project%3C%2FSTRONG%3E%20-%26gt%3B%20select%20%3CSTRONG%3EConsole%20Application%3C%2FSTRONG%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hildat_0-1649782760765.png%22%20style%3D%22width%3A%20466px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F363474i791DE4B08711B37E%2Fimage-dimensions%2F466x300%3Fv%3Dv2%22%20width%3D%22466%22%20height%3D%22300%22%20role%3D%22button%22%20title%3D%22Hildat_0-1649782760765.png%22%20alt%3D%22Hildat_0-1649782760765.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3E2.select%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3ETools%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%26gt%3B%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3ENuGet%20Package%20Manager%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%26gt%3B%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EPackage%20Manager%20Console%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efrom%20the%20menu.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3E3.%20Run%20the%20following%20command%20to%20install%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E%3CSPAN%3EMicrosoft.Azure.Management.ApiManagement%3C%2FSPAN%3E%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Epackage%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EInstall-Package%20Microsoft.Azure.Management.ApiManagement%20-Version%208.0.0-preview%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3E%3CSPAN%3E4.%20Run%20the%20below%20command%20to%20install%20%3CSPAN%20class%3D%22title%22%3EMicrosoft.%3CWBR%20%2F%3EIdentityModel.%3CWBR%20%2F%3EClients.%3CWBR%20%2F%3EActiveDirectory%20package.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EInstall-Package%20Microsoft.IdentityModel.Clients.ActiveDirectory%20-Version%205.2.9%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--31680831%22%20id%3D%22toc-hId--10408979%22%20id%3D%22toc-hId--10408979%22%3E%26nbsp%3B%3C%2FH4%3E%0A%3CH4%20id%3D%22toc-hId--1839135294%22%20id%3D%22toc-hId--1817863442%22%20id%3D%22toc-hId--1817863442%22%3E%3CSTRONG%3EAuthentication%3A%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20construct%20an%20API%20client%2C%20Credentials%20is%20required%20for%20the%20client%20to%20connect%20to%20Azure.%20This%20section%20will%20introduce%20how%20to%20get%20the%20value%20of%20%3CEM%3Eservicecredential%3C%2FEM%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%231460AA%22%3EStep1.%20Register%20an%20application%20in%20Azure%20AD%20to%20represent%20the%20API%20client.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Ea.%20In%20the%20%3CSTRONG%3EAzure%20portal%3C%2FSTRONG%3E%2C%20search%20for%20and%20select%20%3CSTRONG%3EApp%20registrations%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Eb.%20Select%20%3CSTRONG%3ENew%20registration%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Ec.%20In%20the%20%3CSTRONG%3ERegister%20an%20application%20page%3C%2FSTRONG%3E%2C%20enter%20your%20application%E2%80%99s%20registration%20information.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E-%3CSTRONG%20style%3D%22font-family%3A%20inherit%3B%22%3EName%3C%2FSTRONG%3E%3CSPAN%3E.%20You%20can%20select%20a%20meaningful%20name%20for%20this%20app.%20In%20my%20sample%2C%20I%20use%20APIM-client.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E-Select%20an%20option%20that%20suits%20your%20scenario%20in%20the%20%3CSTRONG%3ESupported%20account%20types%3C%2FSTRONG%3E%20section.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E-Leave%20the%20%3CSTRONG%3ERedirect%20URI%3C%2FSTRONG%3E%20section%20empty.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Ed.%20Click%20%3CSTRONG%3ERegister%3C%2FSTRONG%3E%20to%20create%20the%20application.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hildat_0-1649778179258.png%22%20style%3D%22width%3A%20664px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F363430i2B5226D63C574804%2Fimage-dimensions%2F664x267%3Fv%3Dv2%22%20width%3D%22664%22%20height%3D%22267%22%20role%3D%22button%22%20title%3D%22Hildat_0-1649778179258.png%22%20alt%3D%22Hildat_0-1649778179258.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Ee.%20On%20the%20app%20Overview%20page%2C%20find%20the%20Application(client)%20ID%20value%20and%20record%20it%20for%20later.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hildat_1-1649778179270.png%22%20style%3D%22width%3A%20667px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F363432i33162CD537BFB366%2Fimage-dimensions%2F667x260%3Fv%3Dv2%22%20width%3D%22667%22%20height%3D%22260%22%20role%3D%22button%22%20title%3D%22Hildat_1-1649778179270.png%22%20alt%3D%22Hildat_1-1649778179270.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%231460AA%22%3EStep2.%20Create%20a%20client%20secret%20for%20this%20application%20to%20use%20in%20a%20subsequent%20step.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Ea.%20Under%20the%20Manage%20section%20of%20the%20side%20menu%2C%20select%20%3CSTRONG%3ECertificates%26amp%3Bsecrets%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Eb.%20Under%20Client%20secrets%2C%20select%20%3CSTRONG%3ENew%20client%20secret%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Ec.%20In%20the%20%3CSTRONG%3EAdd%20a%20client%20secret%20page%3C%2FSTRONG%3E%2C%20enter%20the%20%3CSTRONG%3EDescription%3C%2FSTRONG%3E%20and%20the%20%3CSTRONG%3Eexpiration%20time%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Ed.%20Click%20on%20%3CSTRONG%3EAdd.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3Ee.%20You%20can%20find%20the%20client%20secret%26nbsp%3B%3CSTRONG%3EValue%3C%2FSTRONG%3E%20in%20the%20portal%2C%20and%20you%20need%20to%20%3CU%3Erecord%20it%20for%20later%20use.%3C%2FU%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hildat_2-1649778179282.png%22%20style%3D%22width%3A%20641px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F363431iE7BDF365FB13F729%2Fimage-dimensions%2F641x290%3Fv%3Dv2%22%20width%3D%22641%22%20height%3D%22290%22%20role%3D%22button%22%20title%3D%22Hildat_2-1649778179282.png%22%20alt%3D%22Hildat_2-1649778179282.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%231460AA%22%3EStep3.%20Grant%20delegated%20permission%20to%20your%20Application%20in%20APIM%20Access%20control%20(IAM)%3C%2FFONT%3E%3C%2FP%3E%0A%3COL%20class%3D%22lia-list-style-type-lower-alpha%22%3E%0A%3CLI%3ENavigate%20to%20your%20%3CSTRONG%3EAPIM%3C%2FSTRONG%3E.%3C%2FLI%3E%0A%3CLI%3EUnder%20%3CSTRONG%3EAccess%20control%20(IAM)%2C%3C%2FSTRONG%3E%20select%20%3CSTRONG%3EAdd%20-%26gt%3B%20Add%20role%20assignment%3C%2FSTRONG%3E.%3C%2FLI%3E%0A%3CLI%3EAssign%20a%20role%20to%20the%20above%20created%20application%2C%20which%20should%20be%20able%20to%20create%20or%20update%20API%20in%20this%20APIM%2C%20such%20as%20%3CSTRONG%3EAPIM%20service%20contributor%3C%2FSTRONG%3E.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hildat_3-1649778179292.png%22%20style%3D%22width%3A%20641px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F363433i86DC72364A2C6992%2Fimage-dimensions%2F641x226%3Fv%3Dv2%22%20width%3D%22641%22%20height%3D%22226%22%20role%3D%22button%22%20title%3D%22Hildat_3-1649778179292.png%22%20alt%3D%22Hildat_3-1649778179292.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIf%20you%20want%20to%20understand%20the%20permission%20for%20a%20different%20role%2C%20you%20can%20click%20on%20view%2C%20and%20all%20permissions%20under%20the%20selected%20role%20will%20be%20displayed.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hildat_4-1649778179304.png%22%20style%3D%22width%3A%20654px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F363435i2D6A0E4E4EB3031D%2Fimage-dimensions%2F654x237%3Fv%3Dv2%22%20width%3D%22654%22%20height%3D%22237%22%20role%3D%22button%22%20title%3D%22Hildat_4-1649778179304.png%22%20alt%3D%22Hildat_4-1649778179304.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%2C%20we%20can%20use%20the%20above%20created%20application%20to%20get%20access%20to%20APIM.%20To%20get%20the%20value%20of%20service%20credentials%2C%20we%20need%20the%26nbsp%3B%3CFONT%20color%3D%22%2334385E%22%3E%3CSTRONG%3EAzure%20tenant%20ID%3C%2FSTRONG%3E%3C%2FFONT%3E%2C%20the%20above%20created%20application%20%3CSTRONG%3E%3CFONT%20color%3D%22%2334385E%22%3Eclient%20ID%3C%2FFONT%3E%3C%2FSTRONG%3E%20and%20%3CFONT%20color%3D%22%2334385E%22%3E%3CSTRONG%3Eclient%20seceret%20value%3C%2FSTRONG%3E%3C%2FFONT%3E.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20sample%20code%20below%20provides%20how%20to%20get%20the%20servicecredential%20token%20and%20initial%20an%20API%20client.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20%20%2F%2Fcreate%20service%20credentials%0A%20%20%20%20public%20class%20AzureApiManagementServiceCredentials%20%3A%20ServiceClientCredentials%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20private%20readonly%20string%20_tenantId%3B%0A%20%20%20%20%20%20%20%20private%20readonly%20string%20_clientId%3B%0A%20%20%20%20%20%20%20%20private%20readonly%20string%20_clientSecret%3B%0A%0A%20%20%20%20%20%20%20%20public%20AzureApiManagementServiceCredentials(string%20tenantId%2C%20string%20clientId%2C%20string%20clientSecret)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20_tenantId%20%3D%20tenantId%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_clientId%20%3D%20clientId%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_clientSecret%20%3D%20clientSecret%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20override%20void%20InitializeServiceClient%3CT%3E(ServiceClient%3CT%3E%20client)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20authenticationContext%20%3D%20new%20AuthenticationContext(%24%22https%3A%2F%2Flogin.microsoftonline.com%2F%7B_tenantId%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20credential%20%3D%20new%20ClientCredential(clientId%3A%20_clientId%2C%20clientSecret%3A%20_clientSecret)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20result%20%3D%20authenticationContext.AcquireTokenAsync(resource%3A%20%22https%3A%2F%2Fmanagement.azure.com%2F%22%2C%20clientCredential%3A%20credential).Result%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(result%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20InvalidOperationException(%24%22Failed%20to%20obtain%20the%20JWT%20token%20for%20Azure%20API%20Management%20Rest%20connection%20for%20user%20%7B_clientId%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20client.HttpClient.DefaultRequestHeaders.Authorization%20%3D%20new%20System.Net.Http.Headers.AuthenticationHeaderValue(%22Bearer%22%2C%20result.AccessToken)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%7D%0A%20%20%20%20%2F%2F%20ref%20github-https%3A%2F%2Fgithub.com%2FAzure%2Fazure-sdk-for-net%2Fissues%2F4727%3C%2FT%3E%3C%2FT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-648377539%22%20id%3D%22toc-hId-669649391%22%20id%3D%22toc-hId-669649391%22%3E%26nbsp%3B%3C%2FH4%3E%0A%3CH4%20id%3D%22toc-hId--1159076924%22%20id%3D%22toc-hId--1137805072%22%20id%3D%22toc-hId--1137805072%22%3E%3CSTRONG%3EAPIM%20Client%20creation%3A%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3EAfter%20getting%20the%20value%20of%20servicecredential%2C%20we%20can%20create%20an%20API%20client%20by%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.management.apimanagement.apimanagementclient%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EApiManagementClient%20Class%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EDon%E2%80%99t%20forget%20to%20assign%20your%20%3CFONT%20color%3D%22%2334385E%22%3E%3CSTRONG%3Esubscription%20ID%3C%2FSTRONG%3E%3C%2FFONT%3E%20to%20the%20subscriptionId%20properties.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20%20%20%20%20%20%20%20%20%20string%20tenantId%20%3D%20%22%5BAzure%20tenant%20ID%5D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20clientId%20%3D%20%22%5Bapplication%20client%20ID%5D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20clientSecret%20%3D%20%22%5Bapplication%20client%20secret%5D%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fconstruct%20client%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20serviceCredentials%20%3D%20new%20AzureApiManagementServiceCredentials(tenantId%2C%20clientId%2C%20clientSecret)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20apiClient%20%3D%20new%20ApiManagementClient(serviceCredentials)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20apiClient.SubscriptionId%20%3D%20%22%5BAPIM%20subscription%20ID%5D%22%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-1328435909%22%20id%3D%22toc-hId-1349707761%22%20id%3D%22toc-hId-1349707761%22%3E%26nbsp%3B%3C%2FH4%3E%0A%3CH4%20id%3D%22toc-hId--479018554%22%20id%3D%22toc-hId--457746702%22%20id%3D%22toc-hId--457746702%22%3E%3CSTRONG%3ECreate%20API%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%231460AA%22%3EStep%201.%20APIManagementmodel%20Construction%20%E2%80%93%20set%20API%20properties%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ESimilar%20to%20putting%20API%20properties%20in%20the%20Request%20body%20when%20creating%20API%20via%20Create%20or%20Update%20REST%20API%2C%20we%20need%20to%20construct%20an%20API%20parameter%20model%20to%20define%20your%20API%20via%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.management.apimanagement.models.apicreateorupdateparameter%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EApiCreateOrUpdateParameter%20Class%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20could%20set%20your%20API%20properties%20in%20this%20part%E2%80%94for%20example%2C%20%3CSTRONG%3EAPI%20display%20name%3C%2FSTRONG%3E%2C%20%3CSTRONG%3EAPI%20path%3C%2FSTRONG%3E%2C%20and%20%3CSTRONG%3Eprotocols%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBelow%20is%20the%20sample.%20I%20just%20created%20an%20open%20API%20called%20%E2%80%9Ctest-httpbin2%E2%80%9D.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20%20%20%20%20%20%20%20%20%20apiProperties%20%3D%20new%20ApiCreateOrUpdateParameter()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20apiProperties.DisplayName%20%3D%20%22test-httpbin2%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20apiProperties.Path%20%3D%20%22testhttpbin2%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20apiProperties.ApiType%20%3D%20%22http%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20protocols%20%3D%20new%20List%3CSTRING%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20protocols.Add(%22http%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20protocols.Add(%22https%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20apiProperties.Protocols%20%3D%20protocols%3B%3C%2FSTRING%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlso%2C%20you%20could%20customize%20your%20API%20by%20adding%20the%20value%20for%20the%20properties%20below.%20For%20more%20details%2C%20please%20see%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.management.apimanagement.models.apicreateorupdateparameter%3Fview%3Dazure-dotnet%23properties%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ethis%20document%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%20120px%3B%20%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hildat_0-1649780310948.png%22%20style%3D%22width%3A%20481px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F363451i1DF7632FE5D4ADF6%2Fimage-dimensions%2F481x543%3Fv%3Dv2%22%20width%3D%22481%22%20height%3D%22543%22%20role%3D%22button%22%20title%3D%22Hildat_0-1649780310948.png%22%20alt%3D%22Hildat_0-1649780310948.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%231460AA%22%3EStep%202.%20Send%20Request%20to%20create%20API%20in%20APIM%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ESet%20the%20value%20of%20your%20%3CSTRONG%3Eresource%20group%20name%3C%2FSTRONG%3E%2C%20%3CSTRONG%3EAPIM%20service%20name%3C%2FSTRONG%3E%2C%20and%20%3CSTRONG%3EapiID%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20%20%20%20%20%20%20%20%20%20string%20resourceGroupName%20%3D%20%22start%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20APIMserviceName%20%3D%20%22apim-study-Hilda%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20apiID%20%3D%20%22api01%22%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20we%20can%20use%20the%20APIM%20client%20to%20send%20an%20HTTP%20request%20to%20create%20our%20API%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%20%20%20%20%20%20%20%20%20%20%20%20var%20response%20%3D%20await%20apiClient.Api.CreateOrUpdateAsync(resourceGroupName%2C%20APIMserviceName%2C%20apiID%2C%20apiProperties)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-2008494279%22%20id%3D%22toc-hId-2029766131%22%20id%3D%22toc-hId-2029766131%22%3E%26nbsp%3B%3C%2FH4%3E%0A%3CH4%20id%3D%22toc-hId-1431910415%22%20id%3D%22toc-hId-1453182267%22%20id%3D%22toc-hId-1453182267%22%3E%3CSTRONG%3ETest%20result%3A%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3EIn%20the%20Azure%20portal%2C%20you%20can%20find%20the%20API%20has%20been%20created%20successfully.%3C%2FP%3E%0A%3CP%20style%3D%22%20padding-left%20%3A%2060px%3B%20%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hildat_10-1649778179347.png%22%20style%3D%22width%3A%20796px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F363440iA3790F719CAC2591%2Fimage-dimensions%2F796x390%3Fv%3Dv2%22%20width%3D%22796%22%20height%3D%22390%22%20role%3D%22button%22%20title%3D%22Hildat_10-1649778179347.png%22%20alt%3D%22Hildat_10-1649778179347.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20complete%20code%20is%20shown%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20System%3B%0Ausing%20System.Threading.Tasks%3B%0Ausing%20System.Collections.Generic%3B%0Ausing%20Microsoft.Azure.Management.ApiManagement%3B%0Ausing%20Microsoft.Azure.Management.ApiManagement.Models%3B%0Ausing%20Microsoft.Rest%3B%0Ausing%20Microsoft.IdentityModel.Clients.ActiveDirectory%3B%0A%0A%0Anamespace%20createAPI%0A%0A%7B%0A%20%20%20%20%2F%2Fcreate%20service%20credentials%0A%20%20%20%20public%20class%20AzureApiManagementServiceCredentials%20%3A%20ServiceClientCredentials%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20private%20readonly%20string%20_tenantId%3B%0A%20%20%20%20%20%20%20%20private%20readonly%20string%20_clientId%3B%0A%20%20%20%20%20%20%20%20private%20readonly%20string%20_clientSecret%3B%0A%0A%20%20%20%20%20%20%20%20public%20AzureApiManagementServiceCredentials(string%20tenantId%2C%20string%20clientId%2C%20string%20clientSecret)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20_tenantId%20%3D%20tenantId%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_clientId%20%3D%20clientId%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20_clientSecret%20%3D%20clientSecret%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20override%20void%20InitializeServiceClient%3CT%3E(ServiceClient%3CT%3E%20client)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20authenticationContext%20%3D%20new%20AuthenticationContext(%24%22https%3A%2F%2Flogin.microsoftonline.com%2F%7B_tenantId%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20credential%20%3D%20new%20ClientCredential(clientId%3A%20_clientId%2C%20clientSecret%3A%20_clientSecret)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20result%20%3D%20authenticationContext.AcquireTokenAsync(resource%3A%20%22https%3A%2F%2Fmanagement.azure.com%2F%22%2C%20clientCredential%3A%20credential).Result%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(result%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20InvalidOperationException(%24%22Failed%20to%20obtain%20the%20JWT%20token%20for%20Azure%20API%20Management%20Rest%20connection%20for%20user%20%7B_clientId%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20client.HttpClient.DefaultRequestHeaders.Authorization%20%3D%20new%20System.Net.Http.Headers.AuthenticationHeaderValue(%22Bearer%22%2C%20result.AccessToken)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%7D%0A%20%20%20%20%2F%2F%20ref%20github-https%3A%2F%2Fgithub.com%2FAzure%2Fazure-sdk-for-net%2Fissues%2F4727%0A%0A%0A%20%20%20%20class%20Program%0A%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%2F%2FAPI%20client%0A%20%20%20%20%20%20%20%20static%20ApiManagementClient%20apiClient%3B%0A%20%20%20%20%20%20%20%20%2F%2FAPIManagementmodel%2C%20for%20request%20body%0A%20%20%20%20%20%20%20%20static%20ApiCreateOrUpdateParameter%20apiProperties%3B%0A%0A%0A%20%20%20%20%20%20%20%20static%20async%20%20Task%20Main()%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20tenantId%20%3D%20%22%5BAzure%20tenant%20ID%5D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20clientId%20%3D%20%22%5Bapplication%20client%20ID%5D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20clientSecret%20%3D%20%22%5Bapplication%20client%20secret%5D%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fconstruct%20client%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20serviceCredentials%20%3D%20new%20AzureApiManagementServiceCredentials(tenantId%2C%20clientId%2C%20clientSecret)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20apiClient%20%3D%20new%20ApiManagementClient(serviceCredentials)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20apiClient.SubscriptionId%20%3D%20%22%5BAPIM%20subscription%20ID%5D%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fconstruct%20api%20properties%20%26lt%3B-%0A%20%20%20%20%20%20%20%20%20%20%20%20apiProperties%20%3D%20new%20ApiCreateOrUpdateParameter()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20apiProperties.DisplayName%20%3D%20%22%5BAPI%20Name%5D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20apiProperties.Path%20%3D%20%22%5BAPI%20path%5D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20apiProperties.ApiType%20%3D%20%22%5BAPI%20type%5D%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20protocols%20%3D%20new%20List%3CSTRING%3E()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20protocols.Add(%22http%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20protocols.Add(%22https%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20apiProperties.Protocols%20%3D%20protocols%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fcreate%20api%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20resourceGroupName%20%3D%20%22%5Bresource%20group%20name%5D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20APIMserviceName%20%3D%20%22%5BAPIM%20service%20name%5D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20apiID%20%3D%20%22%5BAPI%20ID%5D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20response%20%3D%20await%20apiClient.Api.CreateOrUpdateAsync(resourceGroupName%2C%20APIMserviceName%2C%20apiID%2C%20apiProperties)%3B%0A%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FSTRING%3E%3C%2FT%3E%3C%2FT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-3283347%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20article%20provides%20guidance%20on%20how%20to%20create%20API%20via%20Azure%20SDK%20for%20.net.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3283347%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20API%20management%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3387832%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20create%20an%20API%20in%20APIM%20programmatically%20using%20Azure%20SDK%20for%20.Net%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3387832%22%20slang%3D%22en-US%22%3E%3CP%3ENuGet%20is%20telling%20me%20that%20the%20%60Microsoft.IdentityModel.Clients.ActiveDirectory%60%20package%20is%20deprecated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EApparently%20we%20should%20be%20using%20%60Microsoft.Identity.Client%60.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20update%20your%20very%20useful%20article%20accordingly%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Apr 12 2022 10:17 AM
Updated by: