Create task under section in a project

Hi there.

I have a form that when submitting it will create a task in a project in Asana.
I want this to be able to create the task under the section “billing” in the project called “work”.

Right now I create a task doing it like this:

curl_setopt_array($curl, array(
CURLOPT_URL => ‘https://app.asana.com/api/1.0/tasks’,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => ‘’,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => ‘POST’,
CURLOPT_POSTFIELDS => array(‘name’ => $orderNumber . ’ - ’ . $clientName . ’ - ’ . $clientId, ‘projects’ => ‘1202451678035938’, ‘notes’ => ‘’ . $manus . ‘’, ‘assignee’ => ‘1198834232038500’,),
CURLOPT_HTTPHEADER => array(
',
'

),

    ));

How do I get this task to be created in that project, but under a specific section ?
I have different options in my form that should make this be put under the correct section in the project.

@Joakim_Paulsen1,

I think you need to look at https://developers.asana.com/docs/sections and consider using Get sections in a project and Add task to section.

Larry

3 Likes

Thanks Larry.
That works like a charm

1 Like

As @lpb said, you can add your task to a section after creating it, using 2 separate calls.

You can also add it directly to the section in 1 single POST, by using the “memberships” property.

POST https://app.asana.com/api/1.1/tasks
{"data":{
"name":"création tâche dans section",
 "projects":["269451171754166"],
 "memberships":[
  {"project":"269451171754166","section":"1143617431313814"}
 ]
}}

I think you need to repeat the project in “memberships” AND in projects array.

Note that you can’t update a task using the memberships property, but you can create it with it.

4 Likes

hi @Frederic_Malenfant I’m having trouble creating a task below a section in a single posting, I was searching the forum and came across your post, I tried using your suggestion but it didn’t work!

I created a script in python as below

import requests
import json

# replace with your actual Asana token
asana_token = 'insert token'
headers = {
    'Authorization': f'Bearer {asana_token}',
    'Content-Type': 'application/json'
}

# Define the data payload
data = {
    "assignee": "132064102866",  # replace with the actual assignee ID
    "completed": False,
    "followers": [
        "209184463753"  # replace with actual follower IDs
    ],
    "name": "Teste API Section",
    "notes": "teste api",
    # Adding project to projects array as well
    "projects": ["705582290015520"],
    "memberships": [
        {
            "project": "705582290015520",  # replace with the actual project ID
            "section": "1138893254883204"  # replace with the actual section ID
        }
    ],
    "resource_subtype": "default_task",
    "start_on": "2023-08-06",
    "workspace": "132064102865"  # replace with the actual workspace ID
}

# Make the POST request
response = requests.post(
    'https://app.asana.com/api/1.0/tasks', headers=headers, json=data)

# Check if the request was successful
if response.status_code == 201:
    print(
        f"Task created successfully. Task ID: {response.json()['data']['gid']}")
else:
    print(f"Failed to create task. Response: {response.text}")

@RZ_T,

Please post the error message that you get back; thanks.

@Phil_Seeman i found somewhere here in forum one posting from this github - Helper functions to create a task in a specific section in Asana · GitHub so than i adjust my code and now worked, if it might help someone eles check below:

import requests
import json

asana_token = ‘insert token’
headers = {
‘Authorization’: f’Bearer {asana_token}',
‘Content-Type’: ‘application/json’
}

base_url = ‘https://app.asana.com/api/1.0/

def createTask(name, workspaceID, projectID, sectionID, assignee=None, followers=None, due_on=None):
memberships = [{‘project’: projectID, ‘section’: sectionID}]
data = {
‘data’: {
‘name’: name,
‘workspace’: workspaceID,
‘memberships’: memberships,
‘assignee’: assignee,
# If followers is None, pass an empty list
‘followers’: followers if followers else ,
‘due_on’: due_on
}
}
r = requests.post(base_url + ‘tasks’, headers=headers, json=data)
if r.status_code != 201:
s = ‘Creation of task with name ’ + name + ’ failed:’
raise ValueError(s, r.json())
else:
print(f"Task created successfully. Task ID: {r.json()[‘data’][‘gid’]}")

Call the function with your parameters

createTask(
name=“Teste API Section”,
workspaceID=“132064102865”,
projectID=“705582290015520”,
sectionID=“1138893254883204”,
assignee=“132064102866”,
followers=[“209184463753”], # This can be a list of followers
due_on=“2023-08-06”
)

i hope this might help someone else