Getting 400 Bad Request

Hi there,

I am trying to do CRM Bulk Import, for that, I am using “hubspot/api-client” and I am authenticating with “api-key”.

But I am always getting “400 Bad Request” as a response.

FYI, this script is working if I use OAuth instead of API key but I want to use API Key

I couldn’t figure out why it’s not working with the API key, any help would be highly appreciated.

Here goes my code:

if(file_exists('src/files/formatted/contact.csv')) { try { $hubSpot = Factory::createWithApiKey($this->key); $request = json_encode([ "name" => "Import (Contact)", "files" => [ [ "fileName" => "contact", "fileImportPage" => [ "hasHeader" => true, "columnMappings" => [ [ "columnName" => "First Name", "propertyName" => "firstname", "columnObjectType" => "CONTACT", ], [ "columnName" => "Contact Email Domain", "propertyName" => "contact_email_domain", "columnObjectType" => "CONTACT", ], [ "columnName" => "Email", "propertyName" => "email", "columnObjectType" => "CONTACT", ], [ "columnName" => "Company name", "propertyName" => "company", "columnObjectType" => "CONTACT", ], [ "columnName" => "Country/Region", "propertyName" => "country", "columnObjectType" => "CONTACT", ], [ "columnName" => "Full Name", "propertyName" => "full_name", "columnObjectType" => "CONTACT", ], [ "columnName" => "Phone number", "propertyName" => "phone", "columnObjectType" => "CONTACT", ], ], ], ], ], ]); $hubSpot->crm()->imports()->coreApi()->create('src/files/formatted/contact.csv', $request); @unlink('src/files/formatted/contact.csv'); } catch (ApiException $e) { echo "Exception when calling core_api->create: ", $e->getMessage(); }}

## contact.csv

firstname,contact_email_domain,email,company,country,full_name,phoneTest,test.com,test@test.com,Test,GB,Test Test,0123456

Thank you for your post @YGhimire .

@himanshurauthan and @MichaelC , would you be able to share your advice for @YGhimire ?

Hi @YGhimire ,
400 Bad Request means your code json invalid something missing wrong in csv file.
Required fields of contacts:
firstname,lastname,email
Company :
country,name, phone

Hope this helps!

If we were able to answer your query, kindly help the community by marking it as a solution.
Thanks and Regards.

Hi @webdew ,

Thanks for your feedback but that didn’t help.

Even adding lastname for contact and name for company objects, I am getting same error.

It seems like something is up with the bulk import especially with api-key, I have tried to import only one object with contacts-basicApi and it works but not with the coreApi

Hey there,

I fixed my issue, it was something to do with the “filename” in the request.

Before:

"fileName" => "contact",

After:

"fileName" => "contact.csv",

That’s how I fixed it. It literally cost of a number of hours before I find out.

I hope this would help somebody out there who faced the same issue.

LG

Yub

Thank you for sharing the solution!

I ran into this as well. My POST /crm/v3/imports request was succeeding in Postman but failing when I tried to send it via the Go http package.

In my case, I forgot to call multipart.Writer.Close() before building my request! So my request body was missing the trailing boundary end line.

It would be helpful if the Hubspot API response contained a detailed error message in the event of a 400.