Search code examples
phppostmanguzzlehubspothubspot-api

What's a correct example of Hubspot API CSV import request?


I'm trying to use the Import API to import some contacts.

I'm using an over-simplified version of their Sample CSV file which looks like this:

First Name Last Name Email Address
Lorelai Gilmore [email protected]
Leslie Knope [email protected]
Eleanor Shellstrop [email protected]

I've tried several request variations using Postman or Guzzle (my implementation is written in PHP). Every time I get a 400 error: POST https://api.hubapi.com/crm/v3/imports/ resulted in a 400 Bad Request response

Here is a simplified version of the request:

<?php
$client = new Client();
$headers = [
  'Content-Type' => 'multipart/form-data',
  'Accept' => 'application/json',
  'Authorization' => 'Bearer xxx-xxx-xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx'
];
$options = [
  'multipart' => [
    [
      'name' => 'files',
      'contents' => Utils::tryFopen('/path/to/file/HubSpot example - Contacts import file.csv', 'r'),
      'filename' => '/path/to/file/HubSpot example - Contacts import file.csv'
    ],
    [
      'name' => 'importRequest',
      'contents' => '{"name":"customers_import","files":[{"fileName":"/path/to/file/HubSpot example - Contacts import file.csv","fileFormat":"CSV","fileImportPage":{"hasHeader":true,"columnMappings":[{"ignored":false,"columnName":"First Name","idColumnType":null,"propertyName":"firstname","columnObjectType":"CONTACT"},{"ignored":false,"columnName":"Last Name","idColumnType":null,"propertyName":"lastname","columnObjectType":"CONTACT"},{"ignored":false,"columnName":"Email Address","idColumnType":null,"propertyName":"email","columnObjectType":"CONTACT"}]}}]}'
    ]
]];
$request = new Request('POST', 'https://api.hubapi.com/crm/v3/imports/', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();

Some other things I've tried:

  • Changing columnObjectType to columnObjectTypeId: "0-1".
  • Double-checked the Authorization token and the permissions required and everything should be fine (I am getting a 401 without the right permissions).
  • Checked that the file is accessible and readable by the PHP script when not using Postman.
  • Various header variations.
  • Trying to import the data from an Excel file instead.
  • Adding the rest of the mentioned parameters to importRequest (seems to make no difference):
//...
"importOperations" => [
     "0-1" => "CREATE"
],
"dateFormat" => "DAY_MONTH_YEAR",
"marketableContactImport" => true,
//...

I'm looking for a simple sample request that just works.


Solution

  • The problem was the fileName property inside importRequest JSON content. That value should not include the path.