Skip to content

API Keys and Secrets

Venetya Evans edited this page Jul 5, 2017 · 2 revisions

We will investigate the use of Yelp and Google APIs for displaying location information. In order to make use of these we will need to obtain a Client ID/Secret from Yelp's and Google's Developer sites. Once we have obtained the necessary credentials, it is best practice to store them in environment variables and retrieve them at runtime.

Setting Environment Variables

Append the ID/Secret to your .bash_profile file.

subl ~/.bash_profile

Once the file is open, append the credential info

export MY_APP_CONSUMER_KEY=fjioej32if290320u9f2309uf32

export MY_APP_CONSUMER_SECRET=fjioej32if290320u9f2309uf32_jj32if29f2320u9f2309uf32

Once the file has been saved, you’ll need to source it in the currently open window for the environment variable to work in your current session:

source ~/.bash_profile

In any new session that you open (new terminal windows), the environment variable will be sourced automatically because ~/.bash_profile is sourced by default when a new session is open.

To use your new environment variable inside of a Ruby program, you can use the ENV constant, which is a hash of all of the environment variables on the system. For instance, to access the environment variable set above inside a Ruby program, I would use:

ENV['MY_APP_CONSUMER_KEY']

Heroku Deployment

When program is deployed on another computer, for instance via Heroku or Amazon Web Services, we will need to set the environment variables on Heroku or AWS as well since they need to exist wherever a copy of your app is running.

Source :http://blog.zfeldman.com/2014-04-07-Using-Environment-Variables-to-Safely-Store-API-Credentials/