How to add and access env variables in Phoenix
Adding environment variables for a Phoenix web app is fairly straightforward but if googling it can take some time to find a simple answer. In this tutorial I will show you how to set up your environment variables using a .env file.
- In the root of your Phoenix directory create a new
.envfile. - Add that file to your
.gitignoreso you don’t expose secrets to the outside world. If you accidentally forget to add this to your .gitignore and make a commit, anyone who gains to that commit will be able to view your secrets to that point. - Add and export your env variables in the .env file:
export STRIPE_API_KEY="some test_key"
- On your terminal execute
source .envfrom the root directory of your project.
Following the execution of that last step you will be able to access environment variables throughout your project through:
System.get_env("STRIPE_API_KEY")
In my case I made reference to this config value in config.exs:
config :stripity_stripe, api_key: System.get_env("STRIPE_API_KEY")