How create task with membership via API?

Hello, fellas!
How create task in a session, with membership I guess, via API?

When I try creating via API i got this error:
Exception: Request failed for https://app.asana.com/api/1.0/tasks returned code 400. Truncated server response: {"errors":[{"message":"memberships: [0]: project: Missing required field","help":"For more information on API status codes and how to handle them, ... (use muteHttpExceptions option to examine full response) (Yeah, I’m trying via Google Script)

And getting the task (using: ``), I got this response:
{ "data": { "id": 464905951759477, "assignee": null, "assignee_status": "upcoming", "completed": false, "completed_at": null, "created_at": "2017-10-27T17:54:24.723Z", "due_at": null, "due_on": null, "collaborators": [ { "id": 351591353269822 } ], "hearted": false, "hearts": [], "memberships": [ {} ], "modified_at": "2017-10-27T17:54:24.879Z", "name": "test", "notes": "", "num_hearts": 0, "parent": null, "projects": [ { "id": 356330895638626 } ], "workspace": { "id": 12905399818656 } } } → important part: Empty membership (“memberships”: [{}],")

So, what I’m doing wrong? The API or the documentation is wrong?

Hi @Ariel_Morelli,

You’re on the right track - to create a task in a section, you need to use the memberships property. My guess as to what’s going on here is that you’re getting caught by a very common area of confusion with our API that I’ve got a task to clarify in our documentation “someday hopefully soon”.

Our JSON payloads look different on reads vs. writes, which is something that catches pretty much everyone at some point or other. For memberships, if you were to see this on read:

"memberships": [
    { "project": {"id": 12345, "name": "MyProject"}, "section": {"id": 09876, "name":"My Section"} }
]

what you need to send on POST or PUT is

"memberships": [
    {"project": 12345, "section": 09876}
]

that is, the values for project and section are just the IDs, not objects with ID / name pairs.

One other thing that could cause that error is that in setting the membership, section is optional but project is always required. That error could be telling you that you only set the section property and are missing the project

The reason for this is what we call “multi-homing” - the ability of a task (including a section) to be in multiple projects. If this is the case, we need to know which project/section pair you’re working with in order to put everything in the right place.

Hopefully this helps!

4 Likes

Hi @Matt_Bramlage,

I received the same error, so I tried all of the permutations I could think of to get this working. The best I’ve been able to manage is getting the task posted to the right project, but not in the right section. The following (in PHP) seems like it should work…

    ...
    'memberships' => array('project' => 1234, 'section' => 9876),
    'name' => 'A nifty keen task',
    ...

…but throws this error:

Invalid field: memberships[project]

Can you help?

Hey. I am writing from my phone so I am not able to test the solution.

Try to send the array of project and section in an array to memberships.

Memberships => array(array(‘project’ => id, ‘section’ => id ))

Nice one, @Diakoptis, nailed it!

@Ryan_Burney, I had forgotten that key/value pairs like {"project": 12345, "section": 09876}, that is, associative arrays, are constructed in PHP with the same array function, i.e. array('key'=>value) format. So we need to wrap this associative array with another array to get the outer [ ] square braces on serialization.

When testing this out, I also noted that there was one further error message: you have to supply a workspace parameter OR a 'project'=>array(project_id) parameter (which we use to infer a workspace). If you add the workspace parameter and do as @Diakoptis suggests with the double-wrapped array it should work!

@Matt_Bramlage thank you for getting back; your answer kind of helps. The problem I’ve been having is that the API docs are often inaccurate (or I don’t know how to read them); for instance, the docs say I should pass the project ID as a parameter within an array, like so:

...
'memberships' => array(
    'project' => array(
        'id' => 1234, 
        'name' => 'My Project'
    ), 
    ...
),
...

But if I omit the id and name parameters for the project and just pass in the project ID, it works fine:

'memberships' => array('project' => array(1234)),

This means I do a lot of guessing until my code works.

That being said, the following ended up mostly working:

'memberships' => array(
    'project' => array(1234), 
    'section' => array(9876)
),

This code adds the task to the correct project, but does not add the task to any section. When I look at my console output, it is showing the section as null. Here is the relevant section of the JSON payload:

"memberships": [ {"project": {"id": 472994535047972, "name": "Creative Requests"}, "section": null } ]

I’ve verified that the section ID is correct. If I try to pass in the section ID as a parameter, it gives me this error:

Invalid field: memberships[section][id]

Can you help me pinpoint what I am doing wrong?

@Matt_Bramlage I should also add that I tried @Diakoptis’ solution but it did not work; here is the code I tried:

'memberships' => array(array('project' => 1234, 'section' => 9876)),

With that, I receive the following error:

Invalid field: memberships[0][project]

@Matt_Bramlage can you take a look at my previous replies? I am still stuck in the mud on this one. Thank you in advance for any help you can provide.

Hey Ryan —

Here’s how I did it in Python: Helper functions to create a task in a specific section in Asana · GitHub

Sorry to revive this old thread, but I am having the same problem with no resolution so far.

The following is returning with memberships: [0]: project: Missing required field. I’ve tried many other things mentioned in this thread and those didn’t work either.

memberships=[{“project”: 661019183058495, “section”: 711156881122790}]

Can someone tell me exactly what the post for the memberships field should look like to create a task in that project/section?

While I use Memberships to query the projects/sections that an existing task is in, I don’t use Memberships to add a task to a project/section; instead I use the /tasks/{task_gid}/addProject
endpoint
. You can specify the section ID as a parameter. Give that a try.

Based on earlier comments in this thread it should work during task creation, unless that information was incorrect.

Agreed - I wasn’t saying that using Memberships wouldn’t work; I was just providing you with an alternate solution that I know first-hand is easy to implement and works.

Understood. I’m hoping I can get this working in a single request since the code will probably be doing a lot of this. Even if someone has a CURL example that would be very helpful.

By any chance is the problem that I am using URL Encoded form fields to do the post? It was my understanding posts could be done with URL encoded fields or with JSON content. Does the Memberships field not work with a form fields post?

Hi, I just had the same problem…
and after few attempts I found out that if you are trying to set the ‘memberships’ property with ‘project’ / ‘section’…
you also have to set the property ‘projects’, otherwise it won’t work.

This is my object:

$data                  = $request->all();
$data[ 'projects' ]    = 'MY_PROJECT_ID';
$data[ 'memberships' ] = array( [
				                  'project' => 'MY_PROJECT_ID',
				                  'section' => 'MY_SECTION_ID'
			                    ] );

Hope this helps :slight_smile:

1 Like

Hi, I’m trying to make this work for some time now but no luck. Here is my final data:

{"assignee":"120270550628501","name":"Website maintenance ","notes":"Website backup and plugins update","projects":"1202705989979672","workspace":"1202705527229478","memberships":[{"project":"1202705989979672","section":"1202712656838297"}]}

I get the following error:

 [errors] => Array
        (
            [0] => stdClass Object
                (
                    [message] => Could not interpret memberships[0][section] as an identifier in memberships[0][section].
                    [help] => For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors
                )

        )

From the error message, it kind of sounds like the gid you’re sending for the section is not a valid section gid in that project. How exactly did you derive that section gid?

I got it via “projects/”.$_POST[‘project_gid’].“/sections” endpoint. I can send you a real ID’s if that helps?

That certainly looks like a reasonable way to get the section.

No, actual IDs won’t be meaningful to anyone who doesn’t have access to your account.

It’s hard to tell just from the small snippet whether it’s just a formatting/syntax issue or it’s data-related. What I would recommend is using Postman or equivalent to do the task creation to help sort out the root cause.