API call limit
The API call limit operates using a "leaky bucket" algorithm as a controller. This allows for infrequent bursts of calls, and allows your app to continue to make an unlimited amount of calls over time. The bucket size is 40 calls (which cannot be exceeded at any given time), with a "leak rate" of 2 calls per second that continually empties the bucket. If your app averages 2 calls per second, it will never trip a 429 error ("bucket overflow"). To learn more about the algorithm in general, click here.
Your API calls will be processed almost instantly if there is room in your "bucket". Unlike some integrations of the leaky bucket algorithm that aim to "smooth out" (slow down) operations, you can make quick bursts of API calls that exceed the leak rate. The bucket analogy is still a limit that we are tracking, but your processing speed for API calls is not directly limited to the leak rate of 2 calls per second.
Staying within the API call limit
Automated tasks that pause and resume are the best way to stay within the API call limit because you don't need to wait while things get done.
This article will show you how to tell your program to take small pauses to keep your app a few API calls shy of the API call limit and to guard you against a 429 Too Many Requests error.
Avoiding 429 Too Many Requests errors
Some things to remember:
You can check how many calls you've already made using the Shopify header that was sent in response to your API call.
HTTP_X_SHOPIFY_SHOP_API_CALL_LIMITlists how many calls you've made for that particular shop:
Keep in mind that X will decrease over time. If you see you're at 39/40 calls, and wait 10 seconds, you'll be down to 19/40 calls.
In general, you can only get a maximum of 250 objects with one API call.
You can only update one order or product with one API call.
Creating an example throttling program
Let's use a real-world example (in this case, deleting products that are out of stock) to show how to make your program pause and resume.
The scenario in which you would use this specific program is an edge case: unless the products in your shop are all out of stock, you likely won't need to use a throttling mechanism in the way shown below to delete out of stock products.
In your code editor of choice, create a new Ruby file. Call it
Add to that blank file the code below. Then copy your API key, password and myshopify sub-domain in the relevant placeholders:
Below, add your engine. This one fetches all products and deletes the ones that are not available.
To run the program, open a Ruby console or launch your Terminal. Browse to your program and run it:
The code above is available on GitHub: https://github.com/carolineschnapp/delete-out-of-stock-products/blob/master/cleanup.rb