Swagger for F# Web Apps

nlp-logo-navbarSwagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.

Swagger is very powerful framework that is able to generate schema and rich UI for your RESTful API. As I know, Swagger is very popular framework especially in the non-.NET world. You probably have already seen Swagger UI (like this) at several resources.

It turns out that it is not hard to use Swagger for .NET F# apps. There is a project called Swashbuckle, which adds Swagger to WebApi projects.

First of all, you need to create F# ASP.NET Web API project. You can do it using “F# Web Application templates (MVC 5 and Web API 2.2) by Ryan Riley and Daniel Mohl“. Choose “Web API 2.2 and Katana 3.0” option in project creation wizard.

So, now you have F# Web App with RESTful API: CarsController with two services ‘/api/cars‘ and ‘/api/car/{id}‘. Everything is awesome but we do not have UI that is able to show a list of available services, their parameters and return types. Swashbuckle will help us here, we need to install ‘Swashbuckle.Core‘ package to our web app.

Install-Package Swashbuckle.Core

The last step is to update HttpConfiguration in Startup.fs in proper way to register Swagger. Add following three lines to the end of RegisterWebApi method.

open Swashbuckle.Application

type Startup() =
    static member RegisterWebApi(config: HttpConfiguration) =
        // ...
        // Swagger configuration
          .EnableSwagger(fun c -> c.SingleApiVersion("v1", "My API") |> ignore)

That’s all! When you start your web application and open ‘/swagger/ui/index‘ URI you will see beautiful documentation for your RESTful API.

15 thoughts on “Swagger for F# Web Apps

  1. FYI – I had problems with this on .NET 4.5, but was able to fix them by using “Install-Package Swashbuckle.Core.Net45”.

  2. Hi Sergey,
    I’m learning F# to potentially give myself some new career options. Something that drives me nuts about the whole C# web stack is how little it supports open generics out-of-the-box. Swagger generates documentation based on type composition, as opposed to function composition. Same thing with how Razor works. You cannot create a Razor view on an open generic type like List. Same thing with how most MVC model validation works – you cannot bind a “ModelValidator where T : new, MyModelBase” but you can bind a ModelValidator where MyModel is-a MyModelBase. Not sure if this interests you like it does me, but the lack of handling of open generics (while admittedly unsolvable in a general sense) makes writing some terse, but easy to understand, boilerplate code really hard.

  3. Hey, thank you for this post, it is almost helpful to me.

    Having a slightly newer project, I am missing a “Startup” class, so for the life of me I cannot figure out how to add access the HttpConfiguration.

      1. Thank you for your replies… Much awesome.
        I apologies for the double/spam comment, I took a while for the initial to reflect, and then once I saw both I could not figure out how to remove one.

        The link and description are helpful 🙂

  4. Thank you for this post.
    It is almost helpful to me.

    I am having trouble that I have a slightly newer project structure, so I have no Startup Class.
    For the life of me I cannot figure out how to access HttpConfiguration

    1.     let builder = WebApplication.CreateBuilder(args)
          builder.Services.AddSwaggerGen(fun c ->
              c.SwaggerDoc("v1", OpenApiInfo(Title = "My API", Version = "v1"));)
          let app = builder.Build()
          app.UseSwaggerUI(fun c ->
              c.SwaggerEndpoint("v1/swagger.json", "My API V1");

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s