1

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 lorelai@thedragonfly.com
Leslie Knope leslie@pawneeparks.com
Eleanor Shellstrop eleanor@thegoodplace.com

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.

Angelin Calu
  • 1,905
  • 8
  • 24
  • 44
  • The offer a Postman collection on that page, and in Postman you can let it create PHP code for you. – CBroe May 30 '23 at 10:55
  • yes, however, that's not working :( – Angelin Calu May 30 '23 at 11:00
  • So what actual error message is the API giving you? (Check what the response body contains, not just the response status code.) – CBroe May 30 '23 at 11:03
  • There is no response body when the `Accept` header is `application/json`. If I change this header with `*/*` I am getting the response as HTML and it looks like this: ` Error 400 Bad Request

    HTTP ERROR 400

    Reason:

        Bad Request
    `
    – Angelin Calu May 30 '23 at 11:06
  • But their documentation clearly states that you should get a response body that contains a JSON structure giving details. – CBroe May 30 '23 at 11:11
  • Well, their documentation is not always reflecting the real situation. I should expect a JSON response when the response is successful. – Angelin Calu May 30 '23 at 11:13

1 Answers1

0

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

Angelin Calu
  • 1,905
  • 8
  • 24
  • 44