Ask A Question


You’re not receiving notifications from this thread.

What is wrong with my stripe configuration?

Zanger02 asked in Gems / Libraries

Stripe isn't working anymore. When trying to create a new charge returns
the server responded with status 401

I followed stripes checkout docs to set up stripe. Stripe checkout

I have the stripe PUBLISHABLE_KEY: and SECRET_KEY: set up in application.yml

resources: charges

charges ctrl

def new

def create
  # Amount in cents
  @amount = 500

  customer = Stripe::Customer.create(
    :email => params[:stripeEmail],
    :source  => params[:stripeToken]

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

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


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

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


<%= form_tag charges_path do %>
    <% if flash[:error].present? %>
      <div id="error_explanation">
        <p><%= flash[:error] %></p>
    <% end %>
    <label class="amount">
      <span>Amount: $5.00</span>

  <script src="" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description="A month's subscription"
<% end %>

401 errors are authorization errors, see:

Do you mean you store your keys in secrets.yml? If so then you don't read those values from the environment, instead you'd do something like Rails.application.secrets.publishable_key

So in secrets.yml

publishable_key: pk_your_publishable_key
secret_key: sk_your_secret_key

then in 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]

Env variables are setup in application.yml. All my applications use this set up and have worked. All of a sudden it's giving me trouble.


  STRIPE_PUBLISHABLE_KEY: pk_test_publishable_key
  STRIPE_SECRET_KEY: sk_test_secret_key

On your Rails box where you're getting the error, if you go to the console and execute printenv, does STRIPE_PUBLISHABLE_KEY and STRIPE_SECRET_KEY show up?

Unless I'm missing something here, when you put key/values into a yml file, that doesn't mean they're available via the ENV['foo'] method. The only way I know to convert your variables from a yml file into ENV variables is to put this into your config/application.rb:

  config.before_configuration do
    env_file = File.join(Rails.root, 'config', 'custom_yml_file.yml')
    YAML.load( do |key, value|
      ENV[key.to_s] = value.to_s
    end if File.exists?(env_file)

There could be a gem that does this as well, so maybe that's what you're using to get your yml entries into an ENV variable?

In any case, your app isn't authenticating... so have you verified all the keys are correct and they haven't been rolled over? Try hard coding your keys in your stripe.rb and charges/new.html.erb file and see if it authenticates:


Rails.configuration.stripe = {
  :publishable_key => pk_test_asdfasdfasdfasdf,
  :secret_key => sk_test_asdfadsfadsfadsf

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


<script src="" class="stripe-button"
  data-description="A month's subscription"

Thank you. I'm using figaro gem. The ENV var are in application.yml

There was an issue with the key. The key they provided worked for weeks then stopped working. They said the key was correct after chatting back and forth they generated a new one (without telling me) and it works now. Very frustrating. I rolled the keys previously hoping would fix the issue.


Ahh yeah, Figaro - I forgot all about that one

Glad you were able to get it sorted out - too many cooks in the kitchen is always a headache!


Thanks! Figaro is pretty simple to use.

"too many cooks in the kitchen is always a headache!" I couldn't have said it better.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 83,038+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.