Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 432
Gabriel Rivera Avatar asked Dec 04 '25 13:12

Gabriel Rivera


1 Answers

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

like image 88
Anthony Simmon Avatar answered Dec 07 '25 02:12

Anthony Simmon