10

I'm struggling with this error No API key provided. Set your API key using "Stripe.api_key = ". You can generate API keys from the Stripe web interface in a Rails app after following step by step Stripe's guide.

From what I see, everything looks fine, but it keeps returning that notice. Any advice?

Charges Controller:

  class ChargesController < ApplicationController

    def new
    end

    def create
      # Amount in cents
      @amount = 500

      customer = Stripe::Customer.create(
        :email => '[email protected]',
        :card  => params[:stripeToken]
      )

      charge = Stripe::Charge.create(
        :customer    => customer.id,
        :amount      => @amount,
        :description => 'Rails Stripe customer',
        :currency    => 'usd'
      )

    rescue Stripe::CardError => e
      flash[:error] = e.message
      redirect_to charges_path
    end

  end

config/initializers/stripe.rb

    Rails.configuration.stripe = {
      :publishable_key => ENV['pk_test_KEY'],
      :secret_key      => ENV['sk_test_KEY']
    }

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

Terminal trace Started POST "/charges" for 127.0.0.1 at 2014-12-12 22:15:08 +0100

    Processing by ChargesController#create as HTML

      Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXX", "stripeToken"=>"tok_1590kf2NNSl5uX0kXE9XXX", "stripeTokenType"=>"card", "stripeEmail"=>"[email protected]"}

    Completed 500 Internal Server Error in 2ms



    Stripe::AuthenticationError - No API key provided. Set your API key using "Stripe.api_key = <API-KEY>". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email [email protected] if you have any questions.:

       () Users/javier/.rvm/gems/ruby-2.1.2/bundler/gems/stripe-ruby-9c7ebd21c973/lib/stripe.rb:71:in `request'

       () Users/javier/.rvm/gems/ruby-2.1.2/bundler/gems/stripe-ruby-9c7ebd21c973/lib/stripe/api_operations/create.rb:6:in `create'

       () Users/javier/Desktop/definitive/app/controllers/charges_controller.rb:10:in `create'

Tested including the keys in secrets.yml as @sealocal suggests in comments, but still the same issue:

development:
 secret_key_base: key
 publishable_key: anotherkey
 secret_key: anotherkey

test:
 secret_key_base:key
production:
 secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
 publishable_key: <%= ENV["publishable_key"] %>
 secret_key: <%= ENV["secret_key"] %>
7
  • Are those your real keys? I don't think you want ENV['pk_test_NYW7gGgMYnx4HznUXLyti5r3']. If you are hard coding in the values, just put the 'pk_test_NYW7gGgMYnx4HznUXLyti5r3'.
    – ptd
    Dec 12, 2014 at 22:15
  • Former keys, not in use anymore. Tested but it keeps returning the same notice. Dec 12, 2014 at 22:37
  • Where are you storing your Stripe keys? You should have a file that sets your ENV variables. What does it look like?
    – sealocal
    Dec 13, 2014 at 0:20
  • @sealocal in config/initializers/stripe.rb, is that not right? Dec 13, 2014 at 8:58
  • @malditojavi I don't think you're doing it the right way. I think my answer might help you out.
    – sealocal
    Dec 13, 2014 at 20:55

3 Answers 3

7

You need to store your Stripe keys in environment variables so that config/initializers/stripe.rb can read them.

In Rails 4.1+ you can use secrets.yml:

development:
  secret_key_base: key
  publishable_key: pk_test_lkasjdflkajsd
  secret_key: sk_test_lkasjdflkajsd

NOTE: Use exactly two spaces when defining nested key-value pairs in YAML. That is, the keys under development should be indented by two spaces, and not with a tab character. This is because YAML files strictly depend on indentation.

In config/initializers/stripe.rb:

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]
6
  • Tested, updated in the comments with the code I'm including, but still the same issue. Dec 14, 2014 at 10:54
  • If you're using secrets.yml you have to access your secrets with this syntax: Rails.application.secrets.publishable_key. Updated answer to show what your stripe.rb should look like. Your code in the question doesn't show that you've used that syntax. Does that help?
    – sealocal
    Dec 14, 2014 at 19:54
  • If I understood it well, you suggest to include publishable_key: Rails.application.secrets.publishable_key in my secrets.yml? That does not work neither. Dec 14, 2014 at 21:49
  • No, that call should go in your stripe.rb file. The point I was trying to make is that in your stripe.rb you have a line that looks like this: :publishable_key => ENV['pk_test_KEY'], but you should be using Rails.application.secrets.pk_test_key in place of ENV['pk_test_key']. It doesn't matter if you use the name pk_test_key or publishable_key, as long they match between your stripe.rb and secrets.yml.
    – sealocal
    Dec 14, 2014 at 22:41
  • 1
    If that still does not solve the problem, try stopping your local server, run $ spring stop, and then $ rails s to start again.
    – sealocal
    Dec 14, 2014 at 22:51
2

In Rails 7, open your credentials file from your terminal:

$ EDITOR="code --wait" rails credentials:edit --environment=development

Feel free to remove --enviroment=development if you want to use the credentials in all your environments, or pass specific ones for the ones you need: --environment=production...

You can also swap 'code' for 'vim' or 'mate' in EDITOR.

In your credentials file:

stripe:
  publishable_key: pk_whateverYourPublishableKeyFromStripeDashboard
  secret_key: sk_whateverYourSecretKeyFromStripeDashboard

Now close this file and head to:

# config/initializers/stripe.rb
Stripe.api_key = Rails.application.credentials.dig(:stripe, :secret_key)

# create the file if you don't have one.

Restart your server, you are good to go.

1

Stripe support replied my email and their solution is working fine:

config/initializers/stripe.rb

 Rails.configuration.stripe = { 
  :publishable_key => 'pk_test_thekey', 
  :secret_key => 'sk_test_thekey'
}   

Also part of their answer It looks like you are trying to set your API key to an ENV variable named after your key, which is likely not a valid key. You will want to change it to ENV['NAME_OF_ENV_VAR_HERE'], or simply access the key directly, like the lines included above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.