Chicago Boss is an awesome web framework built on top of the functional programming language Erlang. While working on a web application with CB that used the Twilio API, I needed a way to store sensitive data that could be accessed from any module in my application. Essentially I wanted some way to create and use environment variables. "Environment variable" may not be the right term to use in this case, but that was the most relevant term I could think of.

Storing Environment Variables

When you generate a new CB project using $ make app PROJECT=myapp a large number files are generated in your project directory. Among these files is boss.config. This whole file is actually just a single list of tuples that gets interpreted as configurations. At the bottom of this list is a line that reads %% APPLICATION CONFIGURATIONS. Below this line is a tuple that contains your application's name, followed by a list of tuples. This list of tuples is where we can add our own "environment variables".

If we generated our project using the command above, you'll see some lines like this:

%% APPLICATION CONFIGURATIONS

%% domains - A list of domains to serve the application on
%% static_prefix - The URL prefix of static assets
%% doc_prefix - The URL prefix for developer documentation
{ myapp, [
    {path, "../myapp"},
    {base_url, "/"},
%    {domains, all},
%    {static_prefix, "/static"},
%    {doc_prefix, "/doc"},

    {dummy, true}
]}

We can add our own data to this list by simply adding more comma-separated tuples. Ignoring the commented out lines, here's an example of what we could add if we wanted to store our Twilio credentials:

{ myapp, [
    {path, "../myapp"},
    {base_url, "/"},

    {dummy, true},
    {account_sid, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"},
    {auth_token, "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"},
    {phone_number, "5555555555"}
]}

Using Environment Variables

Now that we know where to place them, our environment variables can be accessed using get_env/2.
Here are some example uses:

  • If we want to simply print out our Twilio phone number to the console we could use the following line: io:fwrite("~p", [application:get_env(myapp, phone_number)])
  • If we want to assign our account sid to a local variable we would use pattern matching: {ok, Sid} = application:get_env(myapp, account_sid). The variable Sid will now contain our account_sid.

Hopefully this post was helpful. There's not a whole lot of information out there on Chicago Boss besides the project's own documentation, which is why I felt the need to write this. Feel free to reach out to me on Twitter @brodan_ if you have any questions/feedback.