Created
December 22, 2022 14:52
-
-
Save chrisoklepke/188317c2d496edc55ca8097bee76f2db to your computer and use it in GitHub Desktop.
Import a CSV file in Python with HubSpot API Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First Name | Last Name | Email Address | |
---|---|---|---|
Lorelai | Gilmore | lorelai@thedragonfly.com | |
Leslie | Knope | leslie@pawneeparks.com | |
Eleanor | Shellstrop | eleanor@thegoodplace.com | |
Luke | Danes | luke@lukesdiner.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
from pprint import pprint | |
from hubspot import HubSpot | |
from dotenv import load_dotenv | |
# load_dotenv might not be necessary depending how you store/load access tokens | |
load_dotenv() | |
hubspot = HubSpot(access_token=os.getenv("access_token")) | |
# The name of the CSV file | |
file_name = "example.csv" | |
# Assuming the import file is in the same directory as the code | |
current_dir = os.path.dirname(__file__) | |
# Piecing together the absolute file path | |
absolute_file_path = os.path.join(current_dir, file_name) | |
# First argument in method needs to be a list as you could import multiple files | |
files = [absolute_file_path] | |
# Second argument in method needs to be JSON | |
import_data = json.dumps( | |
{ | |
"name": "Example Import 01", # Name of the import that shows in the HubSpot Import Tool | |
"files": [ | |
{ | |
"fileName": file_name, # The file name pulled from the variable | |
"fileFormat": "CSV", | |
"dateFormat": "DAY_MONTH_YEAR", | |
"marketableContactImport": False, # Set to True if rows need to be Marketing Contacts | |
"fileImportPage": { | |
"hasHeader": True, | |
"columnMappings": [ | |
{ | |
"columnObjectTypeId": "0-1", # The respective object you want to map to | |
"columnName": "First Name", # Header row from CSV | |
"propertyName": "firstname", # Internal property name | |
}, | |
{ | |
"columnObjectTypeId": "0-1", | |
"columnName": "Last Name", | |
"propertyName": "lastname", | |
}, | |
{ | |
"columnObjectTypeId": "0-1", | |
"columnName": "Email", | |
"propertyName": "email", | |
"idColumnType": "HUBSPOT_ALTERNATE_ID", # Applies only to ID properties | |
}, | |
], | |
}, | |
} | |
], | |
} | |
) | |
# API call with HubSpot API Library | |
csv_import = hubspot.crm.imports.core_api.create( | |
files=files, import_request=import_data | |
) | |
pprint(csv_import) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For more information, check out the official docs.