Importing Azure Function Swagger JSON In Azure API Management

I was struggling a lot with importing the Swagger of an Azure Function in API Management. Then, I found an innovative way to create Swagger in Azure Functions, generally, the HTTP Trigger functions. There was a requirement for aggregating all the APIs under API Management. So, it was a challenge for me to find a way to create the Swagger from Azure function and make it automated.

There are multiple ways to create swagger from Azure Function Apps. You can create swagger from Azure Portal or using PowerShell commands by calling REST endpoints of Azure Functions. But I chose it to use an Azure Function itself.

You know that Swagger from Azure Function using the Azure portal is still in the Preview mode. I am not sure what’s the plan of Microsoft to make it generally available.

Let’s start with understanding the process of creating Swagger.

I have created a sample HTTP Trigger Azure Function project. You can find several articles on the internet for creating an HTTP Trigger Azure Function from scratch.

Importing Azure Function Swagger JSON In Azure API Management

You can run this application and do any task you want. But when we want this function to be accessed using some other tools, then we need to think of a way for integration. With Azure API Management, you can directly import an Azure function from the portal. But that’s a manual way, not the automated one. My purpose here is to make this functionality of creating swagger automated, so we can integrate this using CI/CD (Continuous Integration/ Continuous Deployment) pipeline. And we have achieved that. You can build CI/CD using Visual Studio Build Pipeline or other tools.

Here is the flow of creating a swagger and importing it into API Management in an automated way.

Importing Azure Function Swagger JSON In Azure API Management

From our sample project, if we run it, we get the below output.

Importing Azure Function Swagger JSON In Azure API Management

Now, let’s add the swagger function in this project. It will also be an HTTP Trigger Function. You need to add the reference to “System.ComponentModel.Annotations” version 4.1.0 NuGet package in this project.

Importing Azure Function Swagger JSON In Azure API Management

This Swagger Function will create swagger for completing the Function App. When we run this project, we get 2 hosting URLs for Azure Function.

Importing Azure Function Swagger JSON In Azure API Management

We will call swagger function URL in the browser. It will give us swagger 2.0 version JSON in return response.

Importing Azure Function Swagger JSON In Azure API Management

This is the base work we have done until now.

After this the real integration starts, we will create a CI/CD pipeline with any tool and using PowerShell, we can import this swagger into API Management.

You configure CI/CD for deployment of Azure Function app. Once you are able to successfully deploy the function app using CI/CD Pipeline, you can add “Execute PowerShell” step to it. This step refers to the hosted function app URL of swagger function. You can use the PowerShell command “Import-AzureRmApiManagementApi”. Below is a sample of command.

Import-AzureRmApiManagementApi -Context $ApiMgmtContext -SpecificationPath $SwaggerJSONUrl -SpecificationFormat Swagger -Path $APIURLSuffix -ErrorAction $ErrorActionPreference

Post importing the function app in API Management, it looks like this.

Importing Azure Function Swagger JSON In Azure API Management

With this approach, your automated swagger import works. In the future, if you add any new function in this Function App, it automatically creates the swagger.

Thanks for reading this article. I hope you like these steps. I'm waiting for your feedback or comments in the comment box.