'How Do I Resolve this 'Too Many Requests' Error in Node.js?

I'm trying to build a JSON file by making successive HTTP requests with Axios:

  1. Get an array of objects (projects)
  2. Create an array property in each project named attachments
  3. Get each project's tasks
  4. Get each task's attachments
  5. Push each project's task's attachments in to the project's attachments array
  6. Create a JSON file out of the modified projects array

Code:

let getProjects = function() {
  try {
    return axios.get('https://app.asana.com/api/1.0/projects/')
  } catch (error) {
    console.error(error)
  }
}

let getTasks = function(project) {
  try {
    return axios.get('https://app.asana.com/api/1.0/projects/'+project+'/tasks')
  } catch (error) {
    console.error(error)
  }
}

let getAttachments = function(task) {
  try {
    return axios.get('https://app.asana.com/api/1.0/tasks/'+task+'/attachments')
  } catch (error) {
    console.error(error)
  }
}

async function getAsanaData() {
  let projects = await getProjects()
  return Promise.all(projects.data.data.map(async (project) => {
      project.attachments = []
      let tasks = await getTasks(project.gid)
      return Promise.all(tasks.data.data.map(async (task) => {
        let attachments = await getAttachments(task.gid)
        project.attachments = !!attachments ? project.attachments.concat(attachments.data.data) : project.attachments 
        return project
      }))
  }))
}

getAsanaData()
.then((projects) => {  
  var asanaData = safeJsonStringify(projects);
  fs.writeFile("thing.json", asanaData);
})
.catch(err=>console.log(err))

But I'm running into this error:

status: 429,
statusText: 'Too Many Requests

I haven't found anything helpful yet for figuring out how to resolve it. What can I do?



Solution 1:[1]

HTTP response status code 429 indicates sending too many requests than what server could handle. It has been documented at https://asana.com/developers/documentation/getting-started/errors too. The maximum allowed is 150 per minute as documented at https://asana.com/developers/documentation/getting-started/rate-limits.

So, yes, as @Randy Casburn commented, you will have to throttle your requests.

Solution 2:[2]

You're getting throttled by Asana for sending too many requests and reaching the maximum rate.

When it happens, you need to check for the Retry-After response header and wait for the specified amount of time before sending another request.

https://asana.com/developers/documentation/getting-started/rate-limits

You can also learn more in the RFC 6585 about HTTP 429

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Raj
Solution 2 Community