DEV Community

Rajasegar Chandran
Rajasegar Chandran

Posted on

4 3

How to use environment variables with cl-dotenv in a Common Lisp web app

In this post, we are going to take a look at how securely we can store our confidential information like security keys, api keys and similar stuff in .env files and use them in our Common Lisp web applications.

If you want some refresher about writing Common Lisp web applications, I have already written a post about it here.

Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology. In Common Lisp, we have an awesome library called cl-dotenv created by Olle Lauri Boström to load information from .env files and pass it along to our web apps.

Installation

The package is available through Quicklisp.

(ql:quickload :cl-dotenv)
Enter fullscreen mode Exit fullscreen mode

Usage

You can call the load-env function to load the environment from the specified .env file. You can also use any of the available nicknames cl-dotenv, .env or dotenv.

  (.env:load-env (merge-pathnames "./path/.env"))
Enter fullscreen mode Exit fullscreen mode

If you are inside any web application framework or a Lisp project like Caveman, say for example a project called cl-hello,

Let's say you have a .env file like this:

#.env file
API_KEY=1234XXXX
Enter fullscreen mode Exit fullscreen mode

you can use the following snippet to load the .env file inside your project root folder.

(.env:load-env (asdf:system-relative-pathname "cl-hello" "./.env"))
(defvar *api-key* (uiop:getenv "API_KEY"))
(print *api-key*)
Enter fullscreen mode Exit fullscreen mode

References

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay