Issue with Retool sending API requests too quickly

I wrote an automation that will use results from a SQL query to update inventory counts in Shopify using their APIs. Roughly 600 items are updated, so around 600 API POSTs are sent.

I have tried putting sleeps (I wrote a small sleep function to make the code wait a few seconds between queries). Problem is Retool seems to ignore this and batch all the API POST calls to be sent at the end.

Shopify's API then promptly freaks out because their 4 requests per second limit gets surpassed. Is there some way to slow down the requests or prevent Retool from batching these requests?

In the code below I tried splitting the large array of items into smaller item arrays hoping it would slow down and process each chunk in real-time, but it still displays the same behavior of batching the requests at the end.

`while (oracArray.length > 0) {
var smallArray = oracArray.splice(0,20) 
smallArray.forEach((row) => fetchInvId.trigger({
    additionalScope: {
        passSKU: row.ITEM_NUM
    },
    onSuccess: function(data) {
      	startingNum = startingNum + 1
        progressText.setValue("Rows processed: " + startingNum)
        sleep(1000) 
        console.log(row.ITEM_NUM)
        //If statement checking if the inventory item is present within the response JSON
        if (Object.values(data)[0].productVariants.edges.length == 0) {
            errors += "Item " + row.ITEM_NUM + " does not exist in Shopify \n\n"
          	errorText.setValue(errors);
        } else {
            var shopifyInvId = Object.values(data)[0].productVariants.edges[0].node.inventoryItem.id
            shopifyInvId = /[^/]*$/.exec(shopifyInvId)[0]
            console.log(shopifyInvId)
            console.log(row.ONHAND_QTY)
            setInventoryLevels.trigger({
                additionalScope: {
                    passLoc: locationId,
                    passInvId: shopifyInvId,
                    passQuantity: row.ONHAND_QTY
                },
              onSuccess: function(data) {
              },
              onFailure: function(data) {
                errors += 'Unable to update inventory SKU: '+ row.ITEM_NUM + ' ItemDescription: '+ row.DESCRIPTION +'\n\n';
      					errorText.setValue(errors);
              }
            })
        }
    }
}))`

Check out this post

2 Likes

Hi Team,

I have a similar task as JayC mentioned. I want to sync my user details from Shopify API. Since, Shopify API takes 2 seconds to create a user detail. I want to know if it is possible to do 1000 requests or 1000 loops and it needs to await a response before triggering second response.

Kindly let me know your thoughts.

Thanks,
Aditya Saligram Singh