Connor
Connor

Reputation: 349

How to stay within 2 GET requests/second per seconds with Axios (Shopify API)

I have about 650 products and each product has a lot of additional information relating to it being stored in metafields. I need all the metafield info to be stored in an array so I can filter through certain bits of info and display it to the user.

In order to get all the metafiled data, you need to make one API call per product using the product id like so: /admin/products/#productid#/metafields.json
So what I have done is got all the product ids then ran a 'for in loop' and made one call at a time. The problem is I run into a '429 error' because I end up making more than 2 requests per second. Is there any way to get around this like with some sort of queuing system?

 
    let products = []

    let requestOne = `/admin/products.json?page=1&limit=250`
    let requestTwo = `/admin/products.json?page=2&limit=250`
    let requestThree = `/admin/products.json?page=3&limit=250`

    // let allProducts will return an array with all products
    let allProducts

    let allMetaFields = []
    let merge
    $(document).ready(function () {
      axios
        .all([
          axios.get(`${requestOne}`),
          axios.get(`${requestTwo}`),
          axios.get(`${requestThree}`),
        ])
        .then(
          axios.spread((firstResponse, secondResponse, thirdResponse) => {
            products.push(
              firstResponse.data.products,
              secondResponse.data.products,
              thirdResponse.data.products
            )
          })
        )
        .then(() => {
          // all 3 responses into one array
          allProducts = [].concat.apply([], products)
        })
        .then(function () {
          for (const element in allProducts) {
            axios
              .get(
                `/admin/products/${allProducts[element].id}/metafields.json`
              )
              .then(function (response) {
                let metafieldsResponse = response.data.metafields
                allMetaFields.push(metafieldsResponse)
              })
          }
        })
        .then(function () {
          console.log("allProducts: " + allProducts)
          console.log("allProducts: " + allMetaFields)
        })
        .catch((error) => console.log(error))
    })

Upvotes: 0

Views: 569

Answers (1)

Vladimir
Vladimir

Reputation: 2559

When you hit 429 error, check for Retry-After header and wait for the number of seconds specified there.

You can also use X-Shopify-Shop-Api-Call-Limit header in each response to understand how many requests left until you exceed the bucket size limit.

See more details here: REST Admin API rate limits


By the way, you're using page-based pagination which is deprecated and will become unavailable soon.

Use cursor-based pagination instead.

Upvotes: 2

Related Questions