Skip to content

Instantly share code, notes, and snippets.

@abhishekmishragithub
Created May 22, 2023 14:27
Show Gist options
  • Save abhishekmishragithub/b35671aa88d9059a13aea829c56edf04 to your computer and use it in GitHub Desktop.
Save abhishekmishragithub/b35671aa88d9059a13aea829c56edf04 to your computer and use it in GitHub Desktop.
Clickup tasks using ClickUp v2 API
import csv
import requests
import pandas as pd
import logging
import json
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Your ClickUp API token
api_token = ""
# The 'list_id' for creating tasks. get list id here --> https://clickup.com/api/clickupreference/operation/GetLists/ (here we are using folderless list)
list_id = "9008xxx"
# URL for creating tasks in a specific list
url = f"https://api.clickup.com/api/v2/list/{list_id}/task"
# Custom Field ID --> https://clickup.com/api/clickupreference/operation/GetAccessibleCustomFields/
custom_field_id = "35860xxx6-xx-47f5-xxx-xxxx"
# Map task types to custom field values ---> Ref: https://clickup.com/api/clickupreference/operation/GetAccessibleCustomFields/
task_type_values = {
"Blog": "0cc73f01-a09f-4c29-beb3-fca89d9e10ce",
"Sample": "e52cdfc3-2be1-46c7-8b5a-440b574cdc3c",
"Podcast": "d7d10d2a-6ccd-4f38-a997-87c2c7618ab6",
"Talk": "ba179dd2-9131-4bbd-814f-58c6176a1769",
"Release": "53608b20-ffd2-4ba9-9e8a-7d9a0fbe6ca9"
}
headers = {
"Content-Type": "application/json",
"Authorization": api_token
}
# Read the CSV file
# "task_name","task_desc","task_owner","task_type"
# "task name","task description","34324234","Sample"
# note : 34324234 is user id
df = pd.read_csv('tasks.csv')
# Iterate through each row in the DataFrame
for index, row in df.iterrows():
# Get the value for the custom field based on the task type
custom_field_value = task_type_values.get(row['task_type'])
if custom_field_value is None:
logging.warning(f"Unknown task type: {row['task_type']}")
continue
# Create a dictionary representing the task
task = {
"name": row['task_name'],
"description": row['task_desc'],
"assignees": [row['task_owner']],
"tags": [
"samples"
],
"custom_fields": [
{
"id": custom_field_id,
"value": custom_field_value
}
]
}
# print(json.dumps(task, indent=4, sort_keys=True))
# import ipdb;ipdb.set_trace()
# Send a POST request to the ClickUp API to create the task
response = requests.post(url, headers=headers, json=task)
# If the request was successful, log the new task's ID
if response.status_code == 200:
logging.info(f"Created task with ID {response.json()['id']}")
else:
logging.error(f"Failed to create task: {response.content}")
@abhishekmishragithub
Copy link
Author

abhishekmishragithub commented May 22, 2023

@abhishekmishragithub
Copy link
Author

For creating subtasks under a parent task:

  • pass the parent field and parent task id in the task dictionary (payload), for example:
task = {
        "name": row['task_name'],
        "description": row['task_desc'],
        "assignees": [row['task_owner']],
        "parent": "85zt3vy9r",
        "tags": ["social-media"]
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment