This article demonstrates how we can integrate swagger for API documentation for Azure Function APIs. As we know, Swagger UI offers a web-based UI that provides information about REST APIs service (In our case HTTP trigger Azure functions).
Let’s see step by step to understand integration of Swagger UI.
Step 1 – Create Azure Functions Project and Install Required Nuget
Open Visual Studio 2019 and create new Azure function project with empty template. Let’s install the below packages -
<PackageReference Include="AzureExtensions.Swashbuckle" Version="3.3.2" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
Please note that these packages may vary based on your .NET version and Azure Function version.
Step 2 - Add Swagger and Swagger UI functions
Let’s add HTTP trigger function for swagger and swagger UI.
public static class SwaggerFunctions {
[SwaggerIgnore]
[FunctionName("Swagger")]
public static Task < HttpResponseMessage > Swagger(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/json")] HttpRequestMessage req,
[SwashBuckleClient] ISwashBuckleClient swasBuckleClient) {
return Task.FromResult(swasBuckleClient.CreateSwaggerJsonDocumentResponse(req));
}
[SwaggerIgnore]
[FunctionName("SwaggerUI")]
public static Task < HttpResponseMessage > SwaggerUI(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/ui")] HttpRequestMessage req,
[SwashBuckleClient] ISwashBuckleClient swasBuckleClient) {
return Task.FromResult(swasBuckleClient.CreateSwaggerUIResponse(req, "swagger/json"));
}
}
Step 3 - Add Startup code for Swashbuckle
Add or modify startup code and add required swagger configuration as per below.
[assembly: WebJobsStartup(typeof(AzFuncWithSwagger.SwashbuckleStartup))]
namespace AzFuncWithSwagger {
public class SwashbuckleStartup: FunctionsStartup {
public override void Configure(IFunctionsHostBuilder builder) {
builder.AddSwashBuckle(Assembly.GetExecutingAssembly(), opts => {
opts.AddCodeParameter = true;
opts.Documents = new [] {
new SwaggerDocument {
Name = "v1",
Title = "Swagger document",
Description = "Integrate Swagger UI With Azure Functions",
Version = "v2"
}
};
opts.ConfigureSwaggerGen = x => {
x.CustomOperationIds(apiDesc => {
return apiDesc.TryGetMethodInfo(out MethodInfo mInfo) ? mInfo.Name : default (Guid).ToString();
});
};
});
}
}
}




Raghuram RaichootiPosted Apr 17, 2023, 2:08 PM
thankyou, as this article helped me and also add the <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" /> for Swagger UI display
sri kanthPosted Dec 11, 2022, 11:55 PM
I used the same steps but when I deployed the swagger UI is making http request which is creating mixed content error
John StraumannPosted Aug 3, 2022, 1:46 PM
Hello: I changed the name to johnzfunc1, so to call this from Postman locally, the URL is: http://localhost:7071/api/johnzfunc1, and then the JSON would be: {"TrainingTopic":"Test","Trainer":"JohnStraumann", "Date:"8/3/2022" } Is that right?
Kirthikha VelusamyPosted Jul 7, 2022, 7:02 AM
True lifesaver!