Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide API key using Sinatra gem with Ruby

I am a beginner programmer who is currently practicing with the Sinatra gem.

One of my projects is a simple weather app that tells users the weather in different cities. Now I would like to hide the API key I've been using, since I want to upload this site to Heroku. How do I go about doing that? I know that I will need a config.yml file. Where does this go? Does it need to go into a certain folder?

It is just as simple as sticking this in the config.yml file?

    weather_api_key: *api key here*

Will I need to put any additional commands in my server.rb file?Can anyone give me any pointers here?

like image 652
Leia_Organa Avatar asked Aug 02 '26 18:08

Leia_Organa


1 Answers

There are a couple of ways you can do that.

One is to use a file, which you'd add to your gitignore, the other is to use a environment variable, and then refer to that in your config file:

in ~/.bash_profile:

export WEATHER_API_KEY=*api key here*

in config.yml:

WEATHER_API_KEY: <%= ENV['WEATHER_API_KEY'] %>

You'll need to either manually refresh your bash settings (. ~/.bash_profile), or close your terminal window and open a new one for the environment variable to be properly set.

like image 195
magni- Avatar answered Aug 04 '26 07:08

magni-



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!