In this post, we are discussing, how to Get Started with Azure API Management, how we can automate API subscription and management using Azure API Management.
This post is a part of the Azure Services series, in this series, we will discuss about Microsoft Cloud Application Architecture and common cloud computing patterns to make application resilient and scalable. We will be using Asp.net MVC as our framework for applicable examples and Azure as cloud services provider.
You can find all the Microsoft Azure Services; we have covered already at Azure series.
How to setup your Open API in Azure API management : Part 3
In previous post we discussed, how to create your own Open API in Dot.net Core using swagger and published on Azure App Service.
In this post we are going to configure our Open API in Azure API management.
We had published our Open API on Azure App service, we are going to use it to configure it with Azure API management. In order to proceed we need to have Full URL of the json file generated by the swagger, in our case it is as below:
you can click on the URL marked below and it will take you to the swagger.json file.
Let go to the Azure Portal | Azure API Management instance | under API management click APIs | Click on OpenAPI Specification | and copy the URL of the swagger.json file.
Now go to Azure API Over view | click on publisher portal | APIs | Import API | select From URL | choose Swagger | choose New API radio button | mention the suffix URL, this could be name of your service | and choose the scheme, we are going for https | click save.
Now if you go to Azure API Management Instance | APIs , you could see that our service is available there.
Let us assign it to ultimate product, in Products text box type unlimited | choose unlimited | save | verify the API is associated with product by going to unlimited product, as you can see our API is listed there.
Now to use this API, all we need to do is subscribe, which in turn will generate the subscription id that can be used to call the service methods. Since in this series we already subscribed to the unlimited product so we can use our primary key to call the methods of our service.
Go to Overview in Azure API Management | click on developer portal | sign in | Click APIs tab | click on Azure API management API | click try it | fill the field | send
By default, Delete method is select so beware of this when you test the API on production.
You should receive the successful response as below.
Let us consume this service, create a new Asp.net Core Web application with the name client.
We will use NSwagStudio to generate client side classes for the C#, download the tool from below Address:
First get the swagger URL for the API. Go to developer portal | API tab | Click on AzureAPIManagementV1 | click on API definition | choose Open API | then copy the URL
Once NSwagStudio is installed, open the tool, mention the URL and check CSharp Client and either you can click on Generate output button and get the CSharp client code which you can copy paste in visual studio or you can generate the file for you. I copied the path to my client project including the cs file name which will be generated. If you are getting error while generating, try to run the tool as administrator.
If you go to your visual studio the File is already added for you, if the file is not visible use the add existing item option to include it in the project.
Now let us use this to call the API. Open, Index.cshtml.cs in Get method type below code:
IndexModel : PageModel
System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
MyNamespace.Client client = new MyNamespace.Client(httpClient);
System.Collections.ObjectModel.ObservableCollection<string> x = await client.ApiValuesGetAsync();
As you can see we need to include the Ocp-Apim-Subscription-Key
in default header.
If you debug you will get the proper response.