List all folders from ClickUp space

170 Views Asked by At

I'm trying to list all folders created under particular ClickUp space, but it's not giving me desired result. Below is the snippet,

import requests

# Enter your ClickUp API token here
api_token = 'pk_xxxxxxxxxxx'

# Space ID for the desired space
space_id = '1234567'

# API endpoint for retrieving folders in a space
folders_url = f'https://api.clickup.com/api/v2/space/{space_id}/folder'

# Headers with authorization
headers = {
    'Authorization': api_token,
    'Content-Type': 'application/json'
}

def list_all_folders():
    try:
        # Send GET request to retrieve folders in the space
        response = requests.get(folders_url, headers=headers)
        response.raise_for_status()  # Raise exception if request fails

        # Parse the JSON response
        folders_data = response.json()

        # Extract and print folder details
        for folder in folders_data['folders']:
            folder_id = folder['id']
            folder_name = folder['name']
            print(f"Folder ID: {folder_id}, Folder Name: {folder_name}")

    except requests.exceptions.HTTPError as err:
        print(f"Error: {err}")

# Call the function to list all folders in the space
list_all_folders()

Though I have passed correct token, I'm getting this error while running this code,

Error: 401 Client Error: Unauthorized for url: https://api.clickup.com/api/v2/space/1234567/folder

1

There are 1 best solutions below

0
On

I copied your code without change and it runs correctly.

your API key maybe was the issue??

or space ID. only thoughts.

cheers