How create task with membership via API?

tasks
api

#1

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, "followers": [ { "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?


Adding task to board section using Python API
Add Subtask to Task with Python API
#2

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!


#3

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?


#4

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 ))


#5

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!


#6

@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?


#7

@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]

#8

@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.


#9

Hey Ryan —

Here’s how I did it in Python: https://gist.github.com/thedch/4ad472fc615e3b67716bbb4305fc65d9