redgem
redgem

Reputation: 1463

Stripe "Invalid API Key Provided"

I have followed Stripe's Rails tutorial (https://stripe.com/docs/checkout/guides/rails) exactly (copy and pasted code), but when I run rails

PUBLISHABLE_KEY=pk_foo SECRET_KEY=sk_bar rails s 

and go to localhost:3000/charges/new and fill out the fields with the test card data (card number "4242 4242 4242 4242"), but I get an

Invalid API Key provided: ***********_***

Any ideas why this is happening?

Upvotes: 7

Views: 27333

Answers (2)

Tycholiz
Tycholiz

Reputation: 1190

another thing you might check is that the API keys you are using are actually the right ones. What happened to me is that I was scanning the keys in Stripe Dashboard and the ones in my .env file, and made a snap judgement that they were the same based on how they started and ended. They both looked like this, with every character identical, except for the 3rd character:

sk_test_******************************D6D

For whatever reason, when Stripe rolls a new key, they keep it almost the same.

In short, don't trust your eyes, and make sure the keys are actually the same.

Upvotes: 2

colinm
colinm

Reputation: 4288

You need to plug in your publishable key and secret key; pk_foo and sk_bar are placeholders. (Unlike the API docs, the Checkout tutorial doesn't use information from your account.)

You can get them from the API Keys tab of Your Account.

i.e. for a secret key of Sk123456 and a publishable key of pk_987654, you'd issue:

PUBLISHABLE_KEY=pk_987654 SECRET_KEY=Sk123456 rails s

If that still doesn't work there are a couple things to check:

  • Are both keys from the same environment (test or live)? Occasionally people mix the two together.
  • If you load a Rails console instead of a Rails server, can you access those environment variables with ENV['PUBLISHABLE_KEY'] and ENV['SECRET_KEY']?
  • If you're using multiple APIs, it's possible you have some kind of collision occurring; you might try adjusting the command-line and the code to STRIPE_PUBLISHABLE_KEY and STRIPE_SECRET_KEY.

Upvotes: 8

Related Questions