How to fetch tasks from Pipedrive using Python

Pipedrive is a comprehensive customer relationship management (CRM) platform that lets you track interactions with potential clients, organize contacts, dissect sales data for business strategy optimization, and more.

The platform collects various types of data that can be valuable to your product, and its tasks are no exception. You can post tasks to your business communications platform (e.g. Slack) to make them more visible; sync them with your ticketing tool when there’s issues at an account and additional stakeholders need to be looped in—and much more.   

To help facilitate these use cases, we’ll walk you through the process of collecting a list of tasks from Pipedrive by connecting to its API. 

Authentication

To access data from the Pipedrive API, authentication is required. This is performed using an API token which can be found in the Pipedrive web app settings. 

Once you have your API token, it needs to be included in every API request you make to Pipedrive. The token is added to the request headers as the value of the 'Authorization' key. 

How to retrieve tasks from Pipedrive

The script below first defines the base URL for the Pipedrive API, using the provided domain and API key. It then sets up the query parameters for the request, including the type of activity (task), the number of items to request (limit), and the starting point for the request (start). The script then enters a loop, where it sends a GET request to the Pipedrive API and processes the response. 

The response is parsed as JSON, and the tasks are extracted from the 'data' field of the response. The script prints out each task, and the loop continues as long as there are more items in the collection, as indicated by the 'more_items_in_collection' field in the 'additional_data'->'pagination' path of the response. For each iteration of the loop, the 'start' parameter is incremented by the 'limit' to fetch the next batch of tasks. 


import requests
import json
# Your Pipedrive domain and API key
DOMAIN = "your_domain"
API_KEY = "your_api_key"
# Base URL for the Pipedrive API
BASE_URL = f"https://{DOMAIN}.pipedrive.com/api/v1/activities?api_token={API_KEY}"
# Define query parameters
params = {
    "type": "task",
    "limit": 50,
    "start": 0
}
while True:
    response = requests.get(BASE_URL, params=params)
    data = response.json()
    tasks = data['data']
    for task in tasks:
        # Process task data here
        print(task)
    # Check for more items
    if not data['additional_data']['pagination']['more_items_in_collection']:
        break
    # Update the 'start' parameter for pagination
    params['start'] += params['limit']
    

Here’s a potential response from this API endpoint:


{
    "id": 1,
    "done": false,
    "type": "Call",
    "subject": "Follow up with client",
    "user_id": 123,
    "add_time": "2022-01-01T10:30:00Z",
    "due_date": "2022-01-02",
    "due_time": "10:00:00",
    "duration": "00:30:00",
    "type_name": "Phone Call",
    "company_id": 456,
    "owner_name": "John Doe",
    "active_flag": true,
    "update_time": "2022-01-01T11:00:00Z",
    "public_description": "Call to discuss project details",
    "assigned_to_user_id": 789,
    "marked_as_done_time": "2022-01-02T10:30:00Z",
    "created_by_user_id": 123
}

Looking to connect to more CRMs?

For those seeking to establish connections with multiple CRM systems, Merge's CRM Unified API provides an efficient solution. 

It allows you to access numerous CRM platforms, Pipedrive included, via a single API, thereby letting you offer multiple integrations while letting your engineers focus on your core product.

To learn more about Merge, you can request a demo.