Victor L
Victor L

Reputation: 10230

How to use multiple Stripe secret keys with stripe-node?

I'm using stripe-node library. I need to interact with several stripe accounts, each one with their own secret key. What's the best way to do that?

Is there a way to specify the secret key for each request, like:

stripe.charges.create({
  auth: secret_key_1,
  ...
});
stripe.charges.create({
  auth: secret_key_2,
  ...
});

One workaround I thought of is simply instantiating a separate stripe instance for each account.

Upvotes: 1

Views: 2573

Answers (3)

kuna thana
kuna thana

Reputation: 107

You can send apiKey in option object

create(
 params?: ChargeCreateParams,
 options?: RequestOptions
)

You can call

stripe.charges.create({
  {}, // ChargeCreateParams
  {
    apiKey: 'YOUR_API_KEY'
  }
});

Upvotes: 0

benjiman
benjiman

Reputation: 4048

As it states in the documentation you can set the api_key on a per-request basis like this:

stripe.charges.retrieve("ch_1AqjPcJWCk4FIh3mWQ3Hvmvi", {
  api_key: "sk_test_nra5nLtBVqMvXCoYROwWQFRG"
});

The only other alternative is to have multiple instances as already mentioned in other answers.

Upvotes: 3

Ben Fortune
Ben Fortune

Reputation: 32127

You can call stripe.setApiKey before each request.

stripe.setApiKey(secret_key_1);
stripe.charges.create({
  ...
});

stripe.setApiKey(secret_key_2);
stripe.charges.create({
  ...
});

Upvotes: 0

Related Questions