stripe / stripe-node

Node.js library for the Stripe API.
https://stripe.com
MIT License
3.89k stars 753 forks source link

Rate Limit returns HTTP 400 status code #2223

Closed ljrahn closed 1 week ago

ljrahn commented 2 weeks ago

Describe the bug

When i am creating connected accounts too quickly, i get an error with an HTTP status code of 400:

logger.error(error as Stripe.errors.StripeAPIError);
logger.error(`${(error as Stripe.errors.StripeAPIError).statusCode}`);
ERROR: Error: Sorry, you're creating accounts too quickly. You should limit your requests to less than 5 creation attempts per second with a test key, or less than 30 with a live key.
    at Function.generate (file:///Users/lucasrahn/batcave/projects/lendus/lendus-app/packages/backend/node_modules/stripe/esm/Error.js:7:20)
    at res.toJSON.then.StripeAPIError.message (file:///Users/lucasrahn/batcave/projects/lendus/lendus-app/packages/backend/node_modules/stripe/esm/RequestSender.js:102:43)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
ERROR: 400

To Reproduce

Simply attempt to create more than 5 accounts within 1 second using a test API key.

Expected behavior

It should either return a StripeAPIError with a 429 status code, or a StripeRateLimitError. Currently theres no way to detect if im hitting a rate limit error on this endpoint (without checking the message) so i can retry after backing off for some amount of time.

Code snippets

No response

OS

macOS

Node version

v20.12.2

Library version

stripe v15.12.0

API version

2023-10-16

Additional context

No response

seanzhang-stripe commented 2 weeks ago

Hi @ljrahn can you share with me the ID of the request that returned 400 so that I can investigate further?

ljrahn commented 2 weeks ago

@seanzhang-stripe sure thing. you could use this one.

"requestId": "req_SBKgR0ZWLrckkG"

is this what you're looking for?

seanzhang-stripe commented 2 weeks ago

Thanks for the ID. I confirmed that the response code is indeed 400 and you are using Stripe API version > 2015-09-08. I'll flag this internally to the relevant team.

Just out of curiosity, why do you need to create a large number of connected accounts in a short period time?

ljrahn commented 2 weeks ago

Great! thanks. And hmm, i just verified the stripe version from the request is 2023-10-16, not sure where 2015 came from, but no big deal.

And its not a huge deal, i won't be creating this many accounts in prod. Im using vitest for testing, and they make it hard to run tests sequentially, because their single threaded/single process modes put tests in the same global context, and i end up getting some conflicts, so the parallelism of the tests cause me to create more than 5 accounts in a second. Unless im missing something or im approaching this wrong 🤷

seanzhang-stripe commented 2 weeks ago

I mean you are using a Stripe API version that is greater than 2015-09-08, because Stripe returns 400 for rate limit errors before API Version 2015-09-08. I should have included this context in my earlier answer. Sorry about the confusion.

ljrahn commented 2 weeks ago

Ohh lol i missed the '>' haha. All good, thanks a lot!!

seanzhang-stripe commented 1 week ago

Hi @ljrahn The 400 status code is coming from the API endpoint, and the SDK is simply reflecting that. I will close this ticket and follow up internally with the team that manages this endpoint. Please don't hesitate to reopen this ticket if you have any further questions.