Phoenix: Setting up ENV Variables

My goal this year is to learn Elixir/Erlang and Phoenix. It has been as expected very exciting and difficult to learn something new, especially when you can probably do the same activity significantly faster with something you’re familiar with. To me, my usual go-to language/framework for making API’s is Ruby/Ruby on Rails.

I found myself experimenting with o-auth using the ueberauth library. The setup process was pretty straightforward but eventually I got to a point where I needed to store my API secret keys.

Using Rails, the library of choice is the figaro gem. Setup is very straightforward.. run a bundle command and apply your variables in a config/application.yml file. As long as that application.yml file is in your .gitignore you can assure that your secrets will not be exposed on github.

So.. naturally in Phoenix I tried to find something similar. I literally searched figaro elixir because I was 99% sure someone a lot smarter than me had a similar problem and created it. Long behold, I found figaro-elixir! Setup again, was straight forward and similar to that of figaro gem… it seemed pretty flawless. I created a application.yml file in my config directory, defined my variables, and confirmed on the phoenix terminal that it indeed worked. I set up my Ueberauth via code below and tested it out on the browser…. My expectation was to see my keys would persist when making the o-auth call.

However, I found whenever I made the request the keys would be return null values.. thus making an unsuccessful request. I haven’t done a deeper dive into why it wasn’t working but I assumed it was based on how the code was being compiled by the time it reach the configs.exefile. Long story short, I could not seem to persist the keys with the current configuration/dependency.

After much research and googling, I found this interesting tweet by Jose Valim regarding yaml files.

yep ^

How the hell do you set up environment variables then?! I heard that with Phoenix, setting up environment variables come right out of the box but there wasn’t a clear tutorial on HOW to actually do it.

Phoenix Environment Setup

So without further ado, here is the “tutorial” section I wish I found.

  1. Create .env file in your project directory
  2. Define your variables… in my case it would be GITHUB_CLIENT_ID & GITHUB_CLIENT_SECRET. For the sake of this example, I will not reveal my api keys.
export GITHUB_CLIENT_ID="####"

3. In your configs.exs file use your defined variables. In my case, it applies to the ueberauth configurations.

config :ueberauth, Ueberauth.Strategy.Github.OAuth,
client_id: System.get_env("GITHUB_CLIENT_ID"),
client_secret: System.get_env("GITHUB_CLIENT_SECRET")

4. Make sure the .env file is included in your .gitignore file to ensure secret keys aren’t exposed.

# Environment Variables

5. type source.envon the terminal

6. Complete!


Finding the solution took a lot longer than I would’ve liked. However, after figuring out the process, I do have to have to agree it was very simple. No need for an unnecessary yaml file, extra dependency, etc. Life is good :)

Show your support

Clapping shows how much you appreciated Alexander Lee’s story.