How to upload DataFrame to HubSpot CRM via API

I’m trying to transfer data from a pandas DataFrame into HubSpot CRM using their API but running into issues. Here’s what I’ve tried:

from io import StringIO
import requests
import json

# Convert DataFrame to CSV format
csv_buffer = StringIO()
my_dataframe.to_csv(csv_buffer, index=False)
csv_buffer.seek(0)

# HubSpot credentials and endpoint
hubspot_token = 'your_token_here'
import_url = 'https://api.hubapi.com/crm/v3/imports'

# Request payload
request_data = {
    'name': 'my_import',
    'files': [
        {
            'file_name': 'contacts.csv',
            'fileData': csv_buffer.read()
        }
    ]
}

request_headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {hubspot_token}'
}

# Send request
api_response = requests.post(import_url, json=request_data, headers=request_headers)

if api_response.status_code == 200:
    print('Upload completed successfully!')
else:
    print(f'Upload failed: {api_response.status_code} - {api_response.text}')

I keep getting a 415 error saying “Unsupported Media Type”. What’s the correct way to send DataFrame data to HubSpot? Any suggestions would be helpful.

You can’t embed file data directly in JSON - HubSpot’s imports endpoint needs actual file uploads through multipart form data. Hit this same issue migrating customer data from our analytics platform. Use the requests library’s file upload the right way: prep your CSV as a file-like object and send it through the files parameter, not JSON. Double-check your DataFrame columns match HubSpot’s field names. I spent hours debugging ‘successful’ uploads that just created empty records because the fields didn’t map right. Test with a few rows first to nail down your field mappings before uploading everything.

the 415 error means ur mixing up content types. hubspot wants multipart form data for file uploads, not json. use the files parameter in requests instead of json, and don’t set the content-type header - requests handles that automatically. also check that ur dataframe columns match hubspot’s property names exactly, or you’ll get blank records even if the import succeeds.

APIs work but turn into a nightmare once you add data validation, error handling, and column mapping. Trust me, I’ve been there.

I used to write these scripts constantly until I got tired of rebuilding the same pipeline logic every time. Now I just automate everything.

Build a workflow that reads your DataFrame, maps fields automatically, and pushes to HubSpot without manual API calls. Throw in data validation and error notifications too.

Best part? Build it once, reuse it everywhere. I’ve got workflows syncing data across multiple systems while I sleep.

You get retry logic and monitoring built-in, so you’ll know when things break. Beats debugging 415 errors at 2am.

Check it out: https://latenode.com

The Problem: You’re attempting to import data into HubSpot CRM using their API, but receive a 415 Unsupported Media Type error. Your current code uses application/json, which is incorrect for file uploads.

:thinking: Understanding the “Why” (The Root Cause):

The HubSpot CRM import API expects data to be sent using multipart/form-data encoding for file uploads. Your code incorrectly attempts to send the CSV data within a JSON payload. The 415 Unsupported Media Type error indicates the server doesn’t recognize the application/json content type when it expects a file upload. Even if the content type were correct, embedding the CSV data directly into the JSON body isn’t how the HubSpot API handles file imports. The API requires both the import configuration (in JSON format) and the actual CSV file to be sent as separate parts of a single multipart request.

:gear: Step-by-Step Guide:

Step 1: Correct the Request and File Handling: The following code demonstrates the correct way to upload your CSV data to HubSpot using the requests library. It uses the files parameter to send both the CSV file and the JSON import request configuration in a single multipart/form-data request. Crucially, it reads the CSV file’s contents into memory before sending it, ensuring the API receives the data correctly. We also use a more robust error handling mechanism to provide clear feedback based on the server response.

from io import StringIO
import requests
import json
import pandas as pd

#Load your CSV into a pandas DataFrame
my_dataframe = pd.read_csv("contact_upload.csv")

#Convert the DataFrame to CSV in memory
csv_buffer = StringIO()
my_dataframe.to_csv(csv_buffer, index=False)
csv_content = csv_buffer.getvalue()

hubspot_token = 'your_hubspot_token' #Replace with your actual token
import_url = 'https://api.hubapi.com/crm/v3/imports' #Note: HTTPS

files = {
    'files': ('contacts.csv', csv_content, 'text/csv'),
    'importRequest': (None, json.dumps({
        "name": "bulk_contact_import",
        "columnMappings": [
            {"ignored": False, "columnName": "Name", "propertyName": "firstname"},
            {"ignored": False, "columnName": "Website", "propertyName": "website"},
            {"ignored": False, "columnName": "Age", "propertyName": "age_field"}
            # Add more mappings as needed...
        ]
    }), 'application/json')
}

headers = {
    'Authorization': f'Bearer {hubspot_token}'
}

response = requests.post(import_url, files=files, headers=headers)

if response.status_code == 200:
    print('Upload completed successfully!')
    print(response.json()) # Inspect the response for success details.
elif response.status_code == 400: # Example specific error handling
    print(f"Bad Request: {response.json()}")
elif response.status_code == 401: # Example specific error handling
    print("Unauthorized: Check your HubSpot token")
else:
    print(f'Upload failed: {response.status_code} - {response.text}')
    print(response.headers) #Inspect headers for any clues.

Step 2: Verify DataFrame Columns: Ensure that the column names in your pandas DataFrame (my_dataframe) precisely match the property names in your HubSpot CRM. Even minor discrepancies (case sensitivity, extra spaces) will cause import failures. Test with a small subset of your data first to pinpoint mapping issues.

Step 3: Authenticate Correctly: Double-check that your_hubspot_token is a valid and correctly formatted HubSpot API key with the necessary permissions for contact imports.

Step 4: Handle Potential Errors: Implement more robust error handling to address various HTTP status codes and API responses. The example shows basic handling of 400 and 401 errors but you should add more sophisticated error checks for various HubSpot API error codes.

:mag: Common Pitfalls & What to Check Next:

  • Authentication: Incorrect or missing hubspot_token.
  • Rate Limits: HubSpot API has rate limits. For large datasets, consider pagination or batch processing.
  • Data Validation: Thoroughly validate your data before importing. Check for data types, missing values, and inconsistencies.
  • HubSpot Property Names: Confirm that all property names in your CSV match the exact names of your HubSpot properties. Use the HubSpot API to retrieve a list of your contact properties if needed.
  • Character Encoding: Ensure your CSV file is encoded as UTF-8 without a Byte Order Mark (BOM).

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.