Solution in my latest post. Special thank you to @JBeatty
The goal of my project is to import contacts, to make sure they exist in hubspot. Then to add the contact to a list. Once the list is populated a HubSpot workflow will send an email. After the email is sent, the list will need to be cleared for use again later.
I have been able to successfully import the contacts using the provided python script. I am having difficulty using the same .CSV file to add the contacts to the list in question.
I have included the code below with comments.
Any help is greatly appreciated!
# This file contains a list of contact records to import.
import requests
import json
import os
# insert your api key here
#List import to HubSpot
url = "https://api.hubapi.com/crm/v3/imports?hapikey=KEY"
data = {
"name": "HubSpot import name",
"files": [
{
"fileName": "FileName.csv",
"fileFormat": "CSV",
"fileImportPage": {
"hasHeader": True,
"columnMappings": [
{
"ignored": False,
"columnName": "Last Name",
"idColumnType": None,
"propertyName": "lastname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "First Name",
"idColumnType": None,
"propertyName": "firstname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "Primary Email",
"idColumnType": "HUBSPOT_ALTERNATE_ID",
"propertyName": "email",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "Class",
"idColumnType": None,
"propertyName": "lms_class_full_name",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": True,
"columnName": "Enrollment",
"idColumnType": None,
"propertyName": None,
"foreignKeyType": None,
"columnObjectType": None,
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "Starts",
"idColumnType": None,
"propertyName": "lms_class_date_time",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": True,
"columnName": "Courseware Order Type",
"idColumnType": None,
"propertyName": None,
"foreignKeyType": None,
"columnObjectType": None,
"associationIdentifierColumn": False
},
{
"ignored": True,
"columnName": "Courseware Status",
"idColumnType": None,
"propertyName": None,
"foreignKeyType": None,
"columnObjectType": None,
"associationIdentifierColumn": False
},
]
}
}
]}
datastring = json.dumps(data)
payload = {"importRequest": datastring}
current_dir = os.path.dirname(__file__)
relative_path = "EnrollmentsT.csv"
absolute_file_path = os.path.join(current_dir, relative_path)
files = [
('files', open(absolute_file_path, 'r'))
]
print(files)
response = requests.request("POST", url, data=payload, files=files)
print(response.text.encode('utf8'))
print(response.status_code)
#Everything above this is working
#I tried to modify the import contacts script with no success
#The following is to add the contacts to a list and it is NOT working
AddList = "https://api.hubapi.com/contacts/v1/lists/ListID/add?hapikey=KEY"
data = {
# "name": "6A_import", '''I dont think this is needed to add contacts to a list'''
"files": [ #I am trying to add to the list using the contacts email address
{
"fileName": "EnrollmentsT.csv",
"fileFormat": "CSV",
"fileImportPage": {
"hasHeader": True,
"columnMappings": [
{
"ignored": True,
"columnName": "Last Name",
"idColumnType": None,
"propertyName": "lastname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": True,
"columnName": "First Name",
"idColumnType": None,
"propertyName": "firstname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "Primary Email",
"idColumnType": "HUBSPOT_ALTERNATE_ID",
"propertyName": "email",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": True,
"columnName": "Class",
"idColumnType": None,
"propertyName": "lms_class_full_name",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": True,
"columnName": "Enrollment",
"idColumnType": None,
"propertyName": None,
"foreignKeyType": None,
"columnObjectType": None,
"associationIdentifierColumn": False
},
{
"ignored": True,
"columnName": "Starts",
"idColumnType": None,
"propertyName": "lms_class_date_time",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": True,
"columnName": "Courseware Order Type",
"idColumnType": None,
"propertyName": None,
"foreignKeyType": None,
"columnObjectType": None,
"associationIdentifierColumn": False
},
{
"ignored": True,
"columnName": "Courseware Status",
"idColumnType": None,
"propertyName": None,
"foreignKeyType": None,
"columnObjectType": None,
"associationIdentifierColumn": False
},
]
}
}
]}
datastring = json.dumps(data)
payload = {datastring}
current_dir = os.path.dirname(__file__)
relative_path = "EnrollmentsT.csv"
absolute_file_path = os.path.join(current_dir, relative_path)
files = [
('files', open(absolute_file_path, 'r'))
]
print(files)
response = requests.request("POST", AddList, data=payload, files=files)
print(response.text.encode('utf8'))
print(response.status_code)