Building a Web API using F# and Giraffe, hosting on Azure Cloud

Today I am going to talk to you about programming Web APIs using F# programming language.

F# has been around for a while now, but what I would like to talk about is F# in .Net Core because it has a lot of cool features specially for Web Programming and building Web APIs and with playing around with it building some Web API, so far I personally think F# is a really great fit for building Web APIs.

Options for Web Programming with .Net Core and F#

At the moment there are various options to do web programming with F# and .Net Core. You can do Giraffe, Asp.Net Core MVC, Freya, Suave or Saturn and many more frameworks, but for today I will show you how you can use Giraffe to build a Web API with F#.

Some of these frameworks are more low level and some more high level. Some are more functional and some more Object Oriented. If you are interested you can look them up and see what are the differences between them.

What is this Giraffe about?

Giraffe in short is a Functional Asp.Net Core, whereas Asp.Net Core MVC is Object Oriented Asp.Net Core!

It’s also very light weight because it is just a library and not a full framework and because it is not much opinionated, at first it might feel a bit more low level when you are using it, and that is a big deal for a lot of developers to have such low level control over every single detail. And obviously for some other developers, specially people who are used to full opinionated frameworks like Asp.Net Core MVC, Giraffe would be too low level and a bit awkward to get started.

Giraffe uses Kleisli composition to define API routes. Functional Programming fans know what this is and I don’t need to say no more!

But for the rest of us, what it means is, it uses this little operators that look like arrows, and those arrows conceptually mean, The thing on the left is passed to the thing on the right! so you can actually read code left to right.

Oh, and by the way it is Open Source as well:

And of course Giraffe has this really cool logo!

Show me some code

First of all, if you don’t have the Giraffe template, the easiest way is to install it from command prompt with the following command:

dotnet new -i "giraffe-template::*"

This will pull and install the giraffe-template NuGet package in your .NET environment.