How can I create a new project with tasks in Asana using Python?

2.8k views Asked by At

I see some questions that have been asked of how to create tasks using an API, but I want to know how to create a new project.

I have predetermined format of how I want to create a new project every time that I want to create a new one. There are specific tasks that I have and each one has its own subtasks. Right now I have a template on Asana and every time I want to create a new project I go to that project and make a copy of it and rename it to what my current project is.

Does anybody know of a way to automate this using Python, this way I can just run the script and put in the details of the name of the project and it will automatically create a new project on Asana (this way if I need to make 5 projects at once I can just make a list of all project names and loop through all of them)?

I know that you need to have a key and I have something which is called API_KEY and is 32 characters long.

ADD ON: Here is the code that I use in Python to access all of the tasks and subtasks in my workspace in Asana:

import asana
api_key = '################################' //my private key goes here
client = asana.Client.basic_auth(api_key)
me = client.users.me()
all_projects = next(workspace for workspace in me['workspaces'])
projects = client.projects.find_by_workspace(all_projects['id'], iterator_type=None)

for project in projects:
    if 'Template' not in project['name']:
        continue
    print(project['name'])
    project_id = project['id']
    tasks = client.tasks.find_by_project(project_id, iterator_type=None)

    for task in tasks:
        print("    " + task['name'])
        task_id = task['id']
        task_subtasks = client.tasks.subtasks(task_id, full_payload=True)

        for subtask in task_subtasks:
            print("        " + subtask['name'])

When I run this I get all of my tasks and subtasks for the projects that have the word 'template' in their titles. So, this is how to read, is there that if I save all of this in a JSON format, then every time I want to create a new project I can just upload that JSON and get a new project?

2

There are 2 answers

11
shivams On BEST ANSWER

To create a project in asana you need to get workspace or you need to get team id. I created one using workspace. These are steps :-

  1. You go here after logging into asana. This will have workspace ids.
  2. Download python-asana client library from here
  3. In python this is code

    import asana
    client = asana.Client.basic_auth('ASANA_KEY')
    project = {'name':'test','workspace':'WORKSPACE_ID'}
    client.projects.create(project)
    

This will create the project on your account. It did in my case. To create a task follow this procedure.

  1. Get project id which will be returned by asana when you will create a project or go here to get one.
  2. Run this code then

    a={'name':'abc','projects':'PROJECT_ID','workspace':'WORKSPACE_ID'}
    client.tasks.create(a)
    

This will create task under the project whose id you are providing.

1
Andrew Noonan On

It looks like you are using the python-asana client library. This library provide a nice wrapper that implements best practices for accessing the Asana API. I would encourage you to read the documentation to fully understand it's design and capabilities.

Creating a project requires the context of an workspace or organization and in the case of an organization additionally requires the context of a team for the project to belong to.

The following code uses the same library to create a project in the "Moon Landing" workspace, if the workspace is an organization it puts the project in the "Astronauts" team.

import asana

client = asana.Client.basic_auth('ASANA_API_KEY')

workspaces = client.workspaces.find_all({"opt_fields": "is_organization, name"})

workspace = next(workspace for workspace in workspaces if workspace['name'] == 'Moon Landing')

project = {'name':'Training','workspace': workspace['id']}

if workspace['is_organization'] :
    teams = client.teams.find_by_organization(workspace['id'])
    team = next(team for team in teams if team['name'] == 'Astronauts')
    project['team'] = team['id']

training = client.projects.create(project)

Once you have created the "Training" project, you can then add tasks to that project like this.

task = client.tasks.create_in_workspace(workspace['id'], {'projects': [training['id']], 'name': 'Learn to fly space craft'})

Then, adding subtasks

client.tasks.add_subtask(task['id'], {'name': 'Turning it on'})