Asana Python Client Library
The official Python client library for interacting with the Asana API. It provides a simple interface to access Asana resources like tasks, projects, users, and workspaces. The current version is 5.2.4, and the library receives frequent updates, typically focusing on adding support for new API endpoints.
Warnings
- breaking Major API changes in v4.0.0 altered client instantiation and resource method names. Older versions (pre-v4) used different patterns like `client.Client(api_key='...')` and resource methods such as `find_all` or `find_by_id`.
- gotcha Many collection-fetching methods (e.g., `get_tasks`, `get_projects`) return an `asana.Collection` object. This object is an iterator and does not immediately contain all results. To retrieve all items, you must iterate over it or explicitly request all results.
- gotcha Asana API responses can be verbose. To optimize performance and reduce data transfer, always use the `opt_fields` parameter to specify only the fields you need in the response.
Install
-
pip install asana
Imports
- Client
import asana client = asana.Client.access_token('YOUR_PAT')
Quickstart
import asana
import os
# Get your Personal Access Token from environment variable for security
personal_access_token = os.environ.get('ASANA_ACCESS_TOKEN', 'YOUR_ASANA_PAT')
if not personal_access_token or personal_access_token == 'YOUR_ASANA_PAT':
print("Please set the ASANA_ACCESS_TOKEN environment variable or replace 'YOUR_ASANA_PAT' with your token.")
else:
try:
# Construct an Asana client
client = asana.Client.access_token(personal_access_token)
# Get information about the current user ('me')
me = client.users.get_user('me')
print(f"Connected as: {me['name']} (ID: {me['gid']})")
# List workspaces accessible by the user
workspaces = client.workspaces.get_workspaces(iterator_type=None) # iterator_type=None to get all results directly
print("Workspaces:")
for ws in workspaces:
print(f"- {ws['name']} (ID: {ws['gid']})")
except asana.error.NoAuthorizationError:
print("Error: Invalid or expired Asana Personal Access Token.")
except Exception as e:
print(f"An unexpected error occurred: {e}")