Satchel
Satchel

Reputation: 16724

How do I store keys for API's in Rails?

I have several api's that I am integrating with and need to call in various parts of my application.

What is the way to store the keys, the user/password, or token information, say, a configuration file and then how do I call them for use in other parts of the application?

Thanks.

Upvotes: 16

Views: 10893

Answers (5)

Buck3000
Buck3000

Reputation: 371

Updated for Rails 6.1+ - you can leverage Rails encrypted credentials. You'll need to set your RAILS_MASTER_KEY env variable where you are running your app (Heroku, Hatchbox, Digital Ocean, etc.) You can leverage Rails credentials by using the following command:

EDITOR="nano" rails credentials:edit

Then set your creds

aws_secret_key: whatever-your-key-is
other_key: some-other-key

Then access the credentials in your app using:

Rails.application.credentials.aws_secret_key

or

Rails.application.credentials.other_key

Upvotes: 0

benjaminjosephw
benjaminjosephw

Reputation: 4417

Just to keep this question up-to-date, there is a new way to do this in Rails 4.1:

From the Rails guides:

Rails 4.1 generates a new secrets.yml file in the config folder. By default, this file contains the application's secret_key_base, but it could also be used to store other secrets such as access keys for external APIs.

Upvotes: 21

Dan Healy
Dan Healy

Reputation: 757

Check out Configatron, it's pretty awesome and can be used exactly for this purpose.

Upvotes: 0

Larry K
Larry K

Reputation: 49104

Easiest is to store the info as constants in your various environment files. That way you can use different accounts for development, production, etc.

# Eg
# development/environment.rb
....
API_1_USER = "user101"
API_1_PW = "secret!"

Alternative is to create a yaml file, then read it when your app signs in to an api. This is the style used by rails itself with the config/databse.yml file

ADDED

You can also store as a constant using a hash or nested hash.

# Eg
# development/environment.rb
....
API_1 = {"user" => "user101", "pw" => "secret!"}
API_2 = {"user" => "user102", "pw" => "double_secret"}

# or nested hashes
API_KEYS = {
             "api_1" => {"user" => "user101", "pw" => "secret!"},
             "api_2" => {"user" => "user102", "pw" => "double_secret"}}

# using them in another file:
foo.signin(API_1['user'], API_1['pw'])
# or
foo.signin(API_KEYS["api_1"]['user'], API_KEYS["api_1"]['pw'])

# note, I use string constants instead of symbols to save vm (since the hash is
# not referenced more than once or twice). You could also use
# symbols as the keys, especially if the hash will be referenced often:
API_1 = {:user => "user101", :pw => "secret!"}

Upvotes: 4

sarnold
sarnold

Reputation: 104020

You can store usernames/passwords and similar configuration information in mechanisms that rails already uses; you can either stuff the configuration data right into your environment configuration files (where production, testing, and development are configured), or you could use your own mechanism and:

require "yaml"
config_hash = YAML::load_file("/path/to/your/config.yaml")

Upvotes: 7

Related Questions