Hubspot Import API

150 Views Asked by At

I'm unable to get my python script to upload my csv using the import api.

import json

url = "https://api.hubapi.com/crm/v3/imports/"

# Replace with your actual HubSpot access token
YOUR_ACCESS_TOKEN = 'MY_TOKEN'

headers = {
    'Accept': 'application/json',
    'Authorization': f'Bearer {YOUR_ACCESS_TOKEN}'
}

# The dictionary for your import data
data = {
    "name": "Contact Import",
    "files": [
        {
            "fileName": "test.csv",
            "fileFormat": "CSV",
            "fileImportPage": {
                "hasHeader": True,
                "columnMappings": [
                    {
                        "columnObjectTypeId": "0-1",
                        "columnName": "Email",
                        "propertyName": "email",
                        "columnType": "HUBSPOT_ALTERNATE_ID"
                    },
                    {
                        "columnObjectTypeId": "0-1",
                        "columnName": "Subscription_Status",
                        "propertyName": "subscription_status"
                    }
                ]
            }
        }
    ]
}

# Adjust the file path accordingly.
file_path = "File_Location/test.csv"

with open(file_path, 'rb') as f:
    files = [('file', f)]
    response = requests.post(url, headers=headers, files=files, data={'importRequest': json.dumps(data)})


print(response.text.encode('utf8'))
print(response.status_code)

Here's the error I'm getting:

b'{"status":"error","message":"Import cannot be processed without file data!","correlationId":"02e80c57-616d-4c4c-8afc-78f30a627421","category":"VALIDATION_ERROR"}' 400

Process finished with exit code 0

I've looked through the API documentation, but it doesn't seem very clear to me.

0

There are 0 best solutions below