4

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?

Zachary Weixelbaum
  • 904
  • 1
  • 11
  • 24

2 Answers2

5

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'})
Andrew Noonan
  • 848
  • 6
  • 13
  • Thank you very much. The first two bits of code seem to do the same thing that the first answer does, but that last line of code was a life-saver for me. The documentation is very hard to navigate and this shows how to do it. I would give you a vote up, but I'm new so I don't have reputation to vote you up (I'm at 13, but need 15) – Zachary Weixelbaum Jun 03 '15 at 02:16
3

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.

shivams
  • 2,597
  • 6
  • 25
  • 47
  • I did check there, except the lines from asana import asana and asana_api = asana.AsanaAPI('YourAsanaAPIKey', debug=True) don't work. There is no folder called asana within the official asana directory and even when I just try importing asana, AsanaAPI then fails to execute because the function doesn't exist. The only thing I have found that even comes close to what I want is the asana.Client.basic_auth(api_key) method, but in there I can't seem to find how to upload a new project – Zachary Weixelbaum Jun 02 '15 at 17:31
  • If you see, I added on an addendum and in it I am using Client. I see how to read, but I can't seem to find documentation to write – Zachary Weixelbaum Jun 02 '15 at 17:44
  • @ZacharyWeixelbaum I have edited my answer i hope this will help you. Now i have included how to create project using python client library. – shivams Jun 02 '15 at 18:07
  • To your edit: thank you very much, I was able to create a new project, but how can I create tasks and subtasks while I am making this new project? – Zachary Weixelbaum Jun 02 '15 at 18:17
  • @ZacharyWeixelbaum So what you are asking is to create project and its task in one go? – shivams Jun 02 '15 at 18:23
  • Yes, I would prefer to do them both at once, because I have a template that I want to follow, for example I want the name to be Birthday Party and then there to be the tasks of Cake and Balloons. Cake should have subtasks of bake and eat and balloons should have subtasks of blow up and pop. Every time I want to create a new project, I just want to make it more specific, like Zach's birthday, but the tasks and subtasks should remain the same. (All of this hypotheticaly of course) – Zachary Weixelbaum Jun 02 '15 at 18:26
  • @ZacharyWeixelbaum I have done edit on how to create task under a project. Please see. Hope will help you. – shivams Jun 02 '15 at 18:39
  • @ZacharyWeixelbaum Also if you think that this is solution to your problem. Please mark this as accepted answer by ticking the arrow. – shivams Jun 02 '15 at 18:48
  • One last thing, do you know how to add subtasks to the tasks? I was able to upload all the tasks, but not the subtasks – Zachary Weixelbaum Jun 02 '15 at 20:03
  • @ZacharyWeixelbaum please take a look at my answer below. It should explain clearly how to create a project and add tasks to it. – Andrew Noonan Jun 02 '15 at 21:16
  • @shivams Would you mind either deleting this answer or voting mine up above this one? You can get workspace IDs via the API using the Python library and the readthedocs link is actually for a different library. – Andrew Noonan Jun 04 '15 at 21:37