question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change how API versions are set

See original GitHub issue

Pinning all stripe requests to a specific API version is a good idea and recommended, yet the way this is currently done using the SDK is a bit strange and, I would argue, even a bit dangerous.

const Stripe = require('stripe');
const stripe = new Stripe('my-secret-key');
stripe.setApiVersion('2019-02-19');

The setApiVersion() method is strange because it is setting an option that really belongs in either the constructor or each API method call. As far as I am aware, it is the only option that uses a method to set it. Currently, you cannot do new Stripe({ apiVersion : '2019-02-19' }), which makes it inconsistent with how the secret key is set. ~You also cannot do stripe.customers.retrieve(customerId, { apiVersion : '2019-02-19' }), which makes it inconsistent with how the stripe_account option is set.~ The fact that it’s done with a method also forces me to add an extra if statement to one of my modules, which is a small but annoying inconvenience.

Additionally, the setApiVersion() method is dangerous because it gives the impression that it is okay to set the API version at any time before calling a Stripe API. This is problematic because if a single instance of the SDK is used across multiple routes on a server (very common use case), then we are prone to one route accidentally influencing the API version used by other, separate routes. You have to be very careful to first read the existing API version, then set the new API version, then call the desired Stripe API, and finally set the API version back to what it was originally. Yet even then, you are prone to race conditions between routes.

I think moving this option to the constructor would be a great improvement to the SDK and that alone is probably sufficient. However, if people are relying on the ability to override the version for individual API calls, then this option should be supported on each API method.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
paulasjes-stripecommented, Jun 6, 2019

We talked about this internally and we’ve split it into a few tasks:

  1. Move setApiVersion to be set via a config object in the Stripe object initialization, with the intention of deprecating stripe.setApiVersion
  2. Move other setters like setMaxNetworkRetires to be set via the config object
  3. Change maxNetworkRetries so it can be set on a per-request basis as well as a global setting

The config object in task 1 will need to be quite strict about what it accepts, throwing errors if it contains something unexpected.

0reactions
paulasjes-stripecommented, Nov 7, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices For Your API Versioning Strategy - Akana
API versioning is the process of iterating different versions of your API. Learn how to approach your API versioning strategy and adopt API...
Read more >
API Versioning: A Marketer's Guide - HubSpot Blog
API versioning is the practice of managing changes to an API and ensuring that these changes are made without disrupting clients. A good...
Read more >
Four REST API Versioning Strategies - xMatters
1. Versioning through URI Path. One way to version a REST API is to include the version number in the URI path. xMatters...
Read more >
How to Version a REST API - freeCodeCamp
API versioning is the practice of transparently managing changes to your API. Versioning is effective communication around changes to your API, ...
Read more >
API Versioning: When and How to Do It Successfully
API versioning is a way of differentiating points in time where the API changes in a way that requires the consumers of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found