Search code examples
pythonapirestasana

How to upload an asana task under a project via the python asana api?


The asana documentation states that you can create a task within a workspace with the following code, which works:

workspace_id = '48673284270301'
client = asana.Client.basic_auth('asana_api')
client.tasks.create_in_workspace(workspace_id,{ 'name': 'new task'})

I also know you can create a project with the following code:

client = asana.Client.basic_auth('asana_api')
client.projects.create_in_workspace(workspace_id,{'name': 'new project'} )

However, I can't figure out how to upload a task under a certain project?

I've tried the following:

workspace_id = '48673284270301'
client = asana.Client.basic_auth('asana_api')
client.tasks.create_in_workspace(workspace_id,{ 'name': 'new task','projects':[{'id':48729403436686,'name': u'Larry (2100 Walnut)'}]})

Which sends me the following error:

  File "build/bdist.macosx-10.6-x86_64/egg/asana/resources/_tasks.py", line 78, in create_in_workspace
  File "build/bdist.macosx-10.6-x86_64/egg/asana/client.py", line 100, in post
  File "build/bdist.macosx-10.6-x86_64/egg/asana/client.py", line 60, in request
asana.error.InvalidRequestError: Invalid Request: projects: [0]: Not a recognized ID: [object Object]

Solution

  • When referencing objects anywhere in the API all you need to use is the globally unique identifier for that object.

    If you change your code to just reference the ID of the project when creating the task I think it should work

    client = asana.Client.basic_auth('asana_api')
    client.tasks.create_in_workspace(workspace_id,{ 'name': 'new task','projects':[project_id]})
    

    Additionally, I added another example to the python-asana library that demonstrates creating a task in a specific project.