400 Error on POST to Contacts API

Hello!

We are trying to use the Contacts API to bulk import contacts from a CSV but we are having a bit of trouble. I created a test .csv file to try in a sandbox account with and followed the Contacts API article. Below I’ve included my request:

function submitHubSpotImport() {
 const url = 'https://api.hubapi.com/crm/v3/imports?hapikey=xxx'; 

 const body = {
 "name": "test_import",
 "files": [{
 "filename": "test-contact-import-new.csv",
 "fileFormat": "CSV",
 "dateFormat": "DAY_MONTH_YEAR",
 "fileImportPage": {
 "hasHeader": true,
 "columnMappings": [
 {
 "columnObjectTypeId": "0-1",
 "columnName": "First Name",
 "propertyName": "firstname",
 "idColumnType": null
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Last Name",
 "propertyName": "lastname",
 "idColumnType": null
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Email",
 "propertyName": "email",
 "idColumnType": null
 },
 ]
 }
 }]
 };

 var request = {
 method: 'POST',
 headers: {
 'Content-Type': 'multipart/form-data'
 },
 body: JSON.stringify(body)
 };

 fetch(url, request)
 .then(function() {
 console.log('Success: ' + request);
 })
 .catch(function(err) {
 console.log('Error: ' + err);
 });
}

This returns a 400 error.

In postman I get the same 400 error but a bit more info:

{
 "status": "error",
 "message": "Import cannot be processed without file data!",
 "correlationId": "xxx",
 "category": "VALIDATION_ERROR"
}

I do have the file key in postman pointing to the file I’m trying to use. Seems as if for some reason the file isn’t coming through? I also tried including the full url to the file in the filename after I uploaded it to our file manager but that didn’t seem to help. My test csv looks like this:

I can provide any further information needed. Any help would be greatly appreciated! Thank you.

Hey, @DMesser :waving_hand: Thanks for the great question. The bad news, I cannot reproduce your error. The good news is I followed the same example you provided and received a successful response using Postman.

Here’s what I did:

  • I used this example — Start an import CRM Bulk Import API

  • I used this endpointPOST /crm/v3/imports/​

  • I set my headers includedContent-Type multipart/form-data​
    content-type.png

  • I set my Body using the JSON from the example and the CSV from my desktop

  • Which returns a HTTP 200 status and a"state":"DONE"​
  • UsingGET crm/v3/imports/{importId}​I can verify the import as done

The one issue I had was needing to set the “type” for my Key “files” to “file” instead of “text” so the file would attach correctly.

I hope this helps get you moving forward.

Best,

Jaycee

Im working with REST API to perform a posting import using a csv file
Ive got alot of columns but im posting 2 of them below just for a sample as im facing the same issue and i tried your instructions but i couldnt get a positive response and still getting this error : “{“status”:“error”,“message”:“Import cannot be processed without file data!”,“correlationId”:“5ccb9a7e-c439-4a87-b4dc-729e7ee3b4ca”,“category”:“VALIDATION_ERROR”}”
Here is my sample json object :

payload = {‘importRequest’: '{

“name”: “Opportunity Posting Import”,

“files”: [

{

“filename”: “temp_opportunity_data.csv”,

“fileFormat”: “CSV”,

“dateFormat”: “DAY_MONTH_YEAR”,

“fileImportPage”: {

“hasHeader”: true,

“columnMappings”: [

{

“columnName”: “opportunities.id”,

“propertyName”: “opportunities.id”,

“idColumnType”: “Postings”

},

{

“columnName”: “Posting_Locations”,

“propertyName”: “Posting_Locations”,

“idColumnType”: “Postings”

}

]

}

}

]

}

'}

Hey @Jaycee_Lewis,

Once I changed the file keyname to files this is all working in postman now. Thank you for your help!

Thanks for letting us know, @DMesser :raising_hands: — Jaycee