API call made every 5 seconds still gets 429 response

Hi there! Thanks for this service!

I am using search-api to get reverseGeocoding information about a latitude longitude.

https://api.tomtom.com/search/2/reverseGeocode/crossStreet/${lat},${lng}.jsonlimit=1&spatialKeys=false&radius=10000&allowFreeformNewLine=false&view=Unified&key=${process.env.TOM_TOM_API}

I am making this call in a loop. But what is odd is I am calling this request in a setTimeout:

  topographicData.map((typoObj, index) => {
    setTimeout(function() { ....axios/fetch call }, 5000);
  })

And still getting:

Request failed with status code 429

Isn’t there a 5 second wait time? So shouldn’t this work?

Hi,

The Default QPS limit for Search API is set to five.

At first look your code looks good and should work.
However, this is not enough information for us to investigate the issue.
If possible, please, put your code somewhere where we can work with it (e.g., GitHub).

hmmm… mixing setTimeout() with axios, which is asynchronous can be tricky.
Can you verify if your requests are not made at the same time?

Hi

Thanks for responding! Here this is a link to the component in my repo on github.

Also I tried using this via the tt.services method from Services.services.reverseGeocode

which is awesome! But I got back

GET https://api.tomtom.com/search/2/reverseGeocode/40.73975413403147,-73.76126882945239.json?key=myKey 429

Developer Over Qps

        function callbackFn(response) {
          console.log("response", response);
        }

        tt.services
          .reverseGeocode({
            key: process.env.TOM_TOM_API,
            position: typoObj.latlng,
          })
          .then(callbackFn);

You can check how we resolved similar issue in Geofencing Creator:

Thanks you, for your help! That works up to a a point (about 48 requests)


Then I start getting 429's as you can see.

This is the function I created distilled from the Geofencing Creator you provided.

async function getTopographyData(latLang) {
    const retryTimes = 5;
    let counter = 0;
    var { lat, lng } = latLang;
    var newObj = {};
    newObj.topography = await getTopography(latLang, options);
    newObj.latlng = latLang;

    getReverseGeocoding(counter);

    function getReverseGeocoding(counter) {
      axios
        .get(
          `https://api.tomtom.com/search/2/reverseGeocode/crossStreet/${lat},${lng}.json?limit=1&spatialKeys=false&radius=10000&allowFreeformNewLine=false&view=Unified&key=${process.env.TOM_TOM_API}`
        )
        .then(function (response) {
          console.log("response.data ", response.data);

          var { addresses } = response?.data;
          var { address, position } = addresses[0];

          var [lat, lng] = position.split(",").map((p) => +p);

          newObj = {
            ...newObj,
            latlng: { lat, lng },
            address,
          };

          dispatch({ type: "setTopographyData", payload: newObj });

          return;
        })
        .catch(function (error) {
          if (
            (error.response?.status == 403 || error.response?.status == 429) &&
            counter < retryTimes
          ) {
            counter++;
            return new Promise(function (resolve, reject) {
              setTimeout(function () {
                resolve(getReverseGeocoding(counter));
              }, 5000);
            });
          } else {
            console.log("error", error);
          }
        });
    }
  }

I was reading something somewhere in the docs about batching requests. Does this mean you can get requests/responses in chunks and you get them back in chunks too?