malditojavi
malditojavi

Reputation: 1114

Stripe - No API key provided?

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"] %>

Upvotes: 10

Views: 13832

Answers (3)

epicrato
epicrato

Reputation: 8438

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.

Upvotes: 3

sealocal
sealocal

Reputation: 12477

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]

Upvotes: 7

malditojavi
malditojavi

Reputation: 1114

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

Upvotes: 1

Related Questions