DEV Community

Robertino
Robertino

Posted on

Create an API server with Clojure and Pedestal

Build your first web API in the functional programming world of Clojure with this step-by-step guide.


Introduction

This article will walk you through building a web API using Pedastal, a Clojure web framework. Pedastal is focused on APIs first and exemplifies the very best possibilities of Clojure's functional paradigm.

You'll put together a basic project with the necessary dependencies and serve up some basic information about superheroes. In a later article, we'll also secure the API using a collection of best practices and Auth0!

Requirements

For this tutorial, you won't need much out of the box. Be sure you've:

  1. Installed the JDK. This tutorial was written with the latest and greatest version of Java.
  2. If you're on windows, set JAVA_HOME as an environment variable.
  3. Wondered why JAVA_HOME isn't set by the JDK installation in 2021
  4. Installed Clojure

Open up your command-line interface and check that you're good to go:

clj --version

Clojure CLI version 1.10.3.967
Enter fullscreen mode Exit fullscreen mode

Setup a New Clojure Project

If you're brand new to Clojure, check out "Why Developers Should Look into Clojure" first.

Clojure's ecosystem provides several build and dependency management tools. While Leiningen and Boot provide this functionality and have been around a while, the Clojure team has provided the solution we'll be using for this article tools.deps.

Skipping Leiningen reduces the number of things we need to install. tools.deps is included with the Clojure CLI and satisfies all of the requirements for this article, so you should be set.

Like package.json for Node, pom.xml for Maven projects, and requirements.txt in Python, Clojure's CLI uses a single file to manage its dependencies. Create a root folder (clojure-pedastal) for your project on your system and, in it, create a file called deps.edn.

deps.edn contains dependencies (for different environments, like development or production), additional commands/scripts, and much more. We'll keep it simple for now - but do know that EDN stands for "Extensible Data Notation" and is used across the Clojure universe.

Read more...

Top comments (0)