Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set different api keys for my development and production environments in Ruby on Rails?

I am doing some development involving the Stripe API. In my development environment I am using my stripe test key. In production I am using the real api key so that I can process real transactions of course.

I am currently just changing out the test api key immediately before I deploy to my production environment; this does not feel very good. A strategy that I am pretty sure would work is to just make a development branch with a gitignore (ignoring my initializer that loads the api key) and then just merging it with the master branch before I deploy; this way the api keys would always be correct in their respective environments. I don't really like this approach though. Is there some sort of way to configure these api keys somewhere so that the app just knows which one to use when in dev/prod?

like image 268
Spencer Cooley Avatar asked Dec 21 '25 11:12

Spencer Cooley


2 Answers

In rails 4.1 we have the config/secrets.yml file so you can set the api keys there like so:

development:
  secret_key_base: 'xxx'
  publishable_key: xxx
  secret_key: xxx
production:
  secret_key_base: 'xxx'
  publishable_key: xxx
  secret_key: xxx

And in your stripe.rb file you can do this:

Rails.configuration.stripe = {
  :publishable_key => Rails.application.secrets.publishable_key,
  :secret_key      => Rails.application.secrets.secret_key
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]
like image 179
alexsmn Avatar answered Dec 23 '25 00:12

alexsmn


I prefer using yetting.

Though adding API keys in to the environments files gets the job done, adding them to a seperate file seems a whole lot of a cleaner and abstracted way to me.

Once the gem is installed. you can make a yetting.yml file in the config directory.

development:
  facebook_app_id: xxxxxx
staging:
  facebook_app_id: xxxxxx
production:
  facebook_app_id: xxxxxx
like image 44
sohaibbbhatti Avatar answered Dec 23 '25 00:12

sohaibbbhatti



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!