How can I create a new project with tasks in Asana using Python?
I see some questions that have been asked about how to create tasks using the API, but I want to know how to create a new project.
I have a predefined format for how I want to create a new project every time I want to create a new one. There are specific tasks that I have, and each has its own sub-tasks. I now have a template for 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 my current project.
Does anyone know a way to automate this with Python, this way I can just run the script and paste in the details of the project name, and it will automatically create a new project in Asana (this way, if I need to do 5 projects at once, I can just list all project names and skip them all)?
I know you need a key and I have something called API_KEY and is 32 characters long.
ADD ON: Here is the code I am using in Python to access all 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 my tasks and subtasks for projects that have the word "template" in their names. So here's how to read, is there, if I save the whole thing in JSON format, then every time I want to create a new project, I can just load that JSON and get a new project?
source to share
To create a project in asana you need to get the workspace or you need to get the command id. I created one using a workspace. These are the following steps: -
- You go here after entering the asana. This will have workspace IDs.
- Download python-asana client library from here
-
In python this is the code
import asana client = asana.Client.basic_auth('ASANA_KEY') project = {'name':'test','workspace':'WORKSPACE_ID'} client.projects.create(project)
This will create a project in your account. This was my case. Follow the procedure below to create a task.
- Get the project ID that will be returned by asana when you create the project, or go here to get it.
-
Run this code then
a={'name':'abc','projects':'PROJECT_ID','workspace':'WORKSPACE_ID'} client.tasks.create(a)
This will create a task in the project whose ID you provide.
source to share
It looks like you are using the python-asana client library . This library provides a nice wrapper that implements best practices for accessing the Asana API . I would suggest that you read the documentation to fully understand its design and capabilities.
Creating a project requires a workspace or organization context, and in the case of an organization additionally requires a team context for the project to belong.
The following code uses the same library to create a project in the Lunar Landing workspace if the workspace is the organization it puts 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 a Training project, you can add tasks to this 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'})
source to share