Kelly
Kelly

Reputation: 469

Where to configure development API keys rails 4

I'm having trouble figuring out where to put API keys for the development environment in my rails app. I've read about many different ways to do it, but so far this is what I have (Stripe is API):

config/initializers/stripe.rb:

Rails.configuration.stripe = {
  :publishable_key => ENV['PUBLISHABLE_KEY'],
  :secret_key      => ENV['SECRET_KEY']
  :client_id => ENV['CLIENT_ID']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

I'm using Heroku for production so I know how to configure the production keys. What I'm having trouble figuring out is where to store the keys for development so I can test on my local host. Should they go in the config/secrets.yml file? Or perhaps the config/environments/development.rb file?

Thanks for your help.

Upvotes: 1

Views: 953

Answers (1)

Kelly
Kelly

Reputation: 469

What I ended up doing to solve this problem was put all configuration in the config/secrets.yml folder and will have git ignore this file so that production keys are not stored in any repository.

It looks like this:

development:
  secret_key_base: < secret key base >
  stripe_publishable_key: < publishable key >
  stripe_secret_key: < stripe secret key >
  stripe_client_id: < stripe client id >

test:
  secret_key_base: < secret key base >

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  stripe_publishable_key: < publishable key >
  stripe_secret_key: < stripe secret key >
  stripe_client_id: < stripe client id >

I then changed my config/initializers/stripe.rb to this:

Stripe.api_key = Rails.application.secrets.stripe_secret_key

I'm not sure if this is the absolute best way to solve the problem I was having but it works.

Upvotes: 3

Related Questions