skip to Main Content

I am using Shopify’s shopify_api gem in Ruby.

I am updating each products cost and price from an external source however I am hitting the API limits and receiving 429 Too Many Requests (https://help.shopify.com/en/api/reference/rest-admin-api-rate-limits).

How can I edit the below to respect the API limits?

I would rather use the data supplied in X-Shopify-Shop-Api-Call-Limit and Retry-After rather than adding a fixed sleep.

products = ShopifyAPI::Product.find(:all, :params => {:limit => limit})

products.each do |product|
                variant = ShopifyAPI::Variant.find(product.variants.first.id)

                variant.price = price
                variant.save

                inventoryitem = ShopifyAPI::InventoryItem.find(product.variants.first.inventory_item_id)

                inventoryitem.cost = cost
                inventoryitem.save
        end
end

3

Answers


  1. The simplest approach is to Monkeypatch ActiveResource. Here is a repo that does all the work for you.

    https://github.com/mikeyhew/shopify_api_mixins

    Login or Signup to reply.
  2. There’s a gem from Shopify itself to help with rate limiting: https://github.com/Shopify/limiter.

    Login or Signup to reply.
  3. I would rather use the data supplied in X-Shopify-Shop-Api-Call-Limit
    and Retry-After rather than adding a fixed sleep.

    The Shopify API Retry gem does this. It looks to Retry-After header but does use fixed sleep. No way around sleeping. How else to wait?

    It does not do any “monkey patching”. This is good. Monkey patching likely will cause problem at some point.

    To use with your code

    products = ShopifyAPIRetry.retry { ShopifyAPI::Product.find(:all, :params => {:limit => limit}) }
    products.each do |product|
      variant = ShopifyAPIRetry.retry { ShopifyAPI::Variant.find(product.variants.first.id) }
    
      variant.price = price
      ShopifyAPIRetry.retry { variant.save }
    
      inventoryitem = ShopifyAPIRetry.retry { ShopifyAPI::InventoryItem.find(product.variants.first.inventory_item_id) }
    
      inventoryitem.cost = cost
      ShopifyAPIRetry.retry { inventoryitem.save }
    end
    

    Having to call retry everywhere is not that nice. You could possibly consolidate some places into single call.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search