Reputation: 8851
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?
Upvotes: 2
Views: 5503
Reputation: 1746
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]
Upvotes: 9
Reputation: 5237
This is how I do it :
In config/initializers/stripe.rb
if(Rails.env == 'development' || Rails.env == 'staging')
Stripe.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
STRIPE_PUBLIC_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
elsif(Rails.env == 'production')
Stripe.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
STRIPE_PUBLIC_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
end
Upvotes: 1
Reputation: 2682
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
Upvotes: 2
Reputation: 22258
There are many ways to skin this cat.
Quick and simple, look at Rails.env
.
It will return the environment the server is running in. You just need to set the key accordingly.
The way I would really recommend is to create application config variables with a yaml
file or using Rails.application.config
config/environments/development.rb
config.api_key = "my dev api key"
config/environments/production.rb
config.api_key = "my prod api key"
To access your key
MyApp::Application.config.api_key
Check out this question for other examples.
Upvotes: 1