Add a task to multiple projects via API

Using the addProject API for “Add a project to a task”, is it possible to add the task to multiple projects?

https://developers.asana.com/docs/add-a-project-to-a-task

I see the body parameter expects the dat to include the project ID. But, can I simply add multiple project IDs (comma separated) to add the task to multiple projects?

No, sorry, only one project per call here.

2 Likes

Do you happen to know of a simple way to add a task to multiple projects (other than duplicating the call X amount of times)? I’m using Zapier, and was hoping to do some basic code to handle this.

E.g., https://i.imgur.com/gw8qpMW.png

Is it for existing tasks, or for new tasks?

You can create new tasks to multiple projects in 1 call, like this:

POST https://app.asana.com/api/1.1/tasks
{“data”:{“name”:“my task”,“projects”:[“1132891571239348”,“1132891571239350”]}}

If you want to add a maximum of 10 projects to an existing task, you can do it using one single call to the “batch” endpoint.

https://developers.asana.com/docs/batch-api

This would be for existing tasks. Based on a particular dropdown custom field value, I plan to assign the task to a certain set of projects.

E.g.,

  • If dropdown of “Lead Category” is set to “Low”, add to Project A and Project B
  • if dropdown of “Lead Category” is set to “Medium”, add to Project A and Project C
  • if dropdown of “Lead Category” is set to “High”, add to Project X, Project Y, and Project Z

Regardless of the conditional aspect, do you have any code samples of using the POST /batch endpoint to set multiple projects to a given task ID?

Hey ! In fact I have an application in vuejs. I use axios for ajax. However, when I make a get to the ASana api, here is the error that the server returns:
Access to XMLHttpRequest at ‘https://app.asana.com/api/1.0/projects’ from origin ‘http://localhost:8000’ has been blocked by CORS policy: Request header field x-csrf-token is not allowed by Access-Control-Allow-Headers in preflight response.

Hi, I have not tested it, but it should look like this, to add task “123” to projects “111” and “222”.

POST /batch
payload:

{
"data": {
		"actions": [{
				"method": "POST",
				"relative_path": "/tasks/123/addProject",
				"data": {"project": "111"}
			}, {
				"method": "POST",
				"relative_path": "/tasks/123/addProject",
				"data": {"project": "222"}
			}
		]
	}
}

You can add up to 10 action in one single batch. So you can add your task to 10 projects at a time.

1 Like

Just to make sure it’s clear for @FreshyJon - you’re still doing a separate API method to add to each project, there’s no way around that; but using the batch approach, you only have to go over the wire once (or more accurately, once for every 10 API methods) instead of N times where N is the number of API methods you need to do.

1 Like

Yeah, this is the code I tried using, via @Frederic_Malenfant’s post example, but Zapier is returning a message of Bad Request

You’re missing the outer “data” element.

2 Likes

Excellent. That made it work. :slight_smile:

2 Likes