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, and this can't be exceeded at any given time or a 429 bucket overflow error is returned. The bucket empties at a leak rate of two calls per second, which means that apps averaging two calls per second avoid bucket errors. Learn more about the leaky bucket algorithm.

Your API calls are processed almost instantly if there is room in your bucket. Unlike some integrations of the leaky bucket algorithm that aim to smooth out operations, you can make quick bursts of API calls that exceed the leak rate. The bucket analogy is still a limit that Shopify tracks, but your processing speed for API calls is not directly limited to the leak rate of two calls per second.

Staying within the API call limit

If your app exceeds the 40-call bucket size, then a 429 Too Many Requests error is returned.

Designing your app with API call limits in mind is the best way to avoid these errors. For example, you can stagger API requests in a queue and do other processing tasks while waiting for the next queued job to run.

To avoid call limit errors, keep the following in mind:

  • 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_LIMIT lists how many calls you've made for that particular shop:

      X-Shopify-Shop-Api-Call-Limit: 32/40

    X decreases over time. If you see you're at 39/40 calls, and wait ten seconds, you'll be down to 19/40 calls.

  • In general, you can retrieve a maximum of 250 objects with one API call.

  • You can update only one API resource with one API call.

Creating an example throttling program

This example throttling program pauses and resumes during data processing and does not exceed the API call limit. The program connects to a Shopify store, reads the products, and deletes any products that are out of stock.

To run the example program:

  1. In your code editor of choice, create a new Ruby file called cleanup.rb.

  2. Add the code below and provide your API key, API password, and myshopify sub-domain in the relevant placeholders:

APIKEY   = 'APIKEY'
PASSWORD = 'PASSWORD'
SHOPNAME = 'shopname'

CYCLE = 0.5 seconds     # You can average 2 calls per second

require 'rubygems'    # Need this to use the shopify_api gem.
require 'shopify_api' # Tellement utile to speak to your shop.
  1. Add the engine code. This code fetches all products and deletes the ones that are not available:
# Connect to the shop.
url = "https://#{APIKEY}:#{PASSWORD}@#{SHOPNAME}.myshopify.com/admin"
ShopifyAPI::Base.site = url

# Count the products.
product_count = ShopifyAPI::Product.count
nb_pages      = (product_count / 250.0).ceil

# Are there any products in the shop?
puts "You don't have any product in your shop." if product_count.zero?

# Initializing.
start_time = Time.now

# While we still have products.
1.upto(nb_pages) do |page|
  unless page == 1
    stop_time = Time.now
    puts "Last batch processing started at #{start_time.strftime('%I:%M%p')}"
    puts "The time is now #{stop_time.strftime('%I:%M%p')}"
    processing_duration = stop_time - start_time
    puts "The processing lasted #{processing_duration.to_i} seconds."
    wait_time = (CYCLE - processing_duration).ceil
    puts "We have to wait #{wait_time} seconds then we will resume."
    sleep wait_time if wait_time > 0
    start_time = Time.now
  end
  puts "Doing page #{page}/#{nb_pages}..."
  products = ShopifyAPI::Product.find( :all, :params => { :limit => 250, :page => page } )
  products.each do |product|
    puts product.title
    any_in_stock = product.variants.any? do |variant|
      variant.inventory_management == '' || variant.inventory_policy == 'continue' || variant.inventory_quantity > 0
    end
    if not any_in_stock
      puts "--- Deleting #{product.title}..."
      product.destroy
    end
  end
end

puts "Thank you."
  1. Open a Ruby console or launch Terminal.
  2. Browse to your program and run it:
ruby cleanup.rb

Sign up for a Partner account to get started.

Sign up