Pipedrive is a CRM that lets you track your sales pipeline, optimize leads, manage deals with AI and automate your entire sales process so you can focus on selling.
This article provides an overview to get started with the Pipedrive connector in Peliqan. Please contact support if you have any additional questions or remarks.
Contents
- Contents
- Connect Pipedrive
- Writeback to Pipedrive from Python scripts
- Basic writeback functions
- Working with custom fields in Pipedrive
- Read data from Pipedrive
- Writing to Pipedrive
Connect Pipedrive
In Peliqan, go to Connections > Add Connection > Select Pipedrive in the list > Enter the details of your Pipedrive instance.
Steps to fetch your API Key in Pipedrive:
- Login to your Pipedrive Instance
- Navigate to MyAccount > Personal Preference
- Click on "API" tab.
- Copy the Personal API Token.
Writeback to Pipedrive from Python scripts
Basic writeback functions
Example to add a Person to Pipedrive:
pipedrive_api = pq.connect('Pipedrive')
person = {
'name': "Lee James",
'emails': [{'label': 'work', 'value': 'lee@gmail.com', 'primary': True}],
'org_id': 2,
}
result = pipedrive_api.add('person', person)
st.json(result)
Working with custom fields in Pipedrive
Read data from Pipedrive
Example function to convert custom fields from a Pipedrive object to readable field names:
import json
def pipedrive_add_custom_field_labels(obj, type):
dbconn = pq.dbconnect(pq.DW_NAME)
custom_field_defs = dbconn.fetch(pq.DW_NAME, 'pipedrive', type + 'fields')
if "custom_fields" in obj:
custom_fields = obj["custom_fields"]
else:
custom_fields = obj.copy() # when fetching contacts of a company, the custom fields are just keys, not under "custom_fields"
for key, val in custom_fields.items():
for custom_field_def in custom_field_defs:
if custom_field_def["key"] == key and custom_field_def["created_by_user_id"]:
if custom_field_def["field_type"] == "enum":
for option in json.loads(custom_field_def["options"]):
if val and int(option["id"]) == int(val):
obj[custom_field_def["name"]] = option["label"]
elif custom_field_def["field_type"] == "set": # multiple options
if not val:
val_list = []
elif isinstance(val, list):
val_list = val
else:
val_list = val.split(',')
l = []
for v in val_list:
for option in json.loads(custom_field_def["options"]):
if int(option["id"]) == int(v):
l.append(option["label"])
obj[custom_field_def["name"]] = ", ".join(l)
else:
obj[custom_field_def["name"]] = val
return obj
pipedrive_api = pq.connect('Pipedrive')
contacts = pipedrive_api.get('organization_contacts', organization_id = 2)
first_contact = contacts[0]
st.header("Contact before mapping custom fields")
st.json(first_contact)
contact_mapped_custom_fields = pipedrive_add_custom_field_labels(first_contact, "person")
st.header("Contact after mapping custom fields")
st.json(contact_mapped_custom_fields)
Example person from Pipedrive, before mapping custom fields to readable field names:
{
"id": 1,
"name": "John Doe",
"first_name": "John",
"last_name": "Doe",
"8b3515571bcce7dfbaef9613aa48c1d6369c79b6": "VIP", // custom field
"4810cd3354b0caa934ea97a01351ed294f7b8bdb": "APAC", // custom field
...
}
Example after applying the above function:
{
"id": 1,
"name": "John Doe",
"first_name": "John",
"last_name": "Doe",
"Customer category": "VIP", // custom field
"Sales region": "APAC", // custom field
...
}
Writing to Pipedrive
Example function to convert field names to the actual custom field keys from Pipedrive, when writing to Pipedrive (e.g. Adding or Updating a Contact or Organization):
import json
def pipedrive_add_custom_field_keys(obj, type):
custom_field_defs = dw.fetch(pq.DW_NAME, 'pipedrive', type + 'fields')
for key, val in obj.copy().items():
for custom_field_def in custom_field_defs:
if custom_field_def["name"] == key and custom_field_def["created_by_user_id"]:
if not "custom_fields" in obj:
obj["custom_fields"] = {}
obj["custom_fields"][custom_field_def["key"]] = val
obj.pop(key)
return obj
pipedrive_api = pq.connect('Pipedrive')
contact_to_update = {
"id": 1,
"name": "John Doe",
"first_name": "John",
"last_name": "Doe",
"Customer category": "VIP", # custom field
"Sales region": "APAC", # custom field
...
}
st.header("Contact before mapping custom fields")
st.json(contact_to_update)
contact_to_update = pipedrive_add_custom_field_keys(contact_to_update, "person")
st.header("Contact after mapping custom fields")
st.json(contact_to_update)
result = pipedrive_api.update("person", contact_to_update)
Example contact to add/update in Pipedrive, before mapping field names to custom field keys:
{
"id": 1,
"name": "John Doe",
"first_name": "John",
"last_name": "Doe",
"Customer category": "VIP", // custom field
"Sales region": "APAC", // custom field
...
}
Example after applying the above function (object ready to be sent to Pipedrive):
{
"id": 1,
"name": "John Doe",
"first_name": "John",
"last_name": "Doe",
"custom_fields":
"8b3515571bcce7dfbaef9613aa48c1d6369c79b6": "VIP", // custom field
"4810cd3354b0caa934ea97a01351ed294f7b8bdb": "APAC", // custom field
...
}