Getting 400 Bad Request

SOLVE
YGhimire
Member

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,phone
Test,test.com,test@test.com,Test,GB,Test Test,0123456

 

0 Upvotes
1 Accepted solution

Accepted Solutions
YGhimire
Solution
Member

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

View solution in original post

5 Replies 5
natsumimori
Community Manager

Thank you for your post @YGhimire .

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

0 Upvotes
webdew
Top Contributor | Diamond Partner

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. 

0 Upvotes
YGhimire
Member

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 

0 Upvotes
YGhimire
Solution
Member

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

View solution in original post

natsumimori
Community Manager

Thank you for sharing the solution! 

0 Upvotes