How to deal with environment variables on Heroku
If you’re building a web app, chances are you have environment-specific configurations. You have API keys, session secrets, and other private information that you definitely don’t want on any public repository. But you do need those variables to be accessible in order for your app to run. So, the question is, how does an app on Heroku gain access to those variables? The simplest and most effective way is to use config vars.
If you’ve ever factored out sensitive information in your code to a separate config file, then the concept of using config vars won’t be new to you. The idea here is that instead of using a file that is on your repository and pushing that to Heroku, you will create and store your variables directly on Heroku. This can be done very easily from your terminal. To declare a config var while in your repo, run…
heroku config:set SOME_API_KEY=notanactualapikeythatwouldbedumb
Just like that, your variable is stored! If you want to review your config vars at any time, you can simply run
heroku config to check all variables, or you can check a specific variable with
heroku config:get SOME_API_KEY. If you wanted to delete a key, use
heroku config:unset SOME_API_KEY.
process.env.SOME_API_KEY. If you’ve ever used process.env.PORT, this will seem very familiar to you. You were using a config var that is automatically set by your hosting service.
And that’s all you have to do. If this article seemed short to you, that’s because using config vars with Heroku is just dead simple! Please like this article if you would like more tips on deploying your apps in the future. Thanks for reading!