Building a REST API in Clojure

Backend API development made simple with Clojure

Functional Human
The Startup

--

I am going to write this up for anyone who is just starting to get into Clojure or for anyone else who is curious to see how a simple API server could be built.

Clojure is a really great functional programming language with many cool features. Hopefully you will find this tutorial useful if you have skimmed through the Clojure documentation to get a feel for the basic datatypes and language, or perhaps dabbled in the REPL writing some simple functions.

Photo by Ana Martin on Unsplash

We are going to build a simple REST API to get a feel for what a simple API back end might look like. To do this I will be using the fantastic HTTPKit Compojure and data.JSON libraries and you can also clone the final code on my github.

In production you might also choose to use a more feature rich implementation such as Yada, Liberator or Pedestal, but for our simple purposes Compojure, HTTPKit and Ring will suffice, as in many cases these frameworks often share the same basic building blocks.

We will develop a simple API that defines the following basic operations:

GET http://127.0.0.1:3000/ - prints "Hello World"
GET http://127.0.0.1:3000/request/ - Shows us the HTTPRequest object
GET http://127.0.0.1:3000/hello?name=fh - prints…

--

--