Help Using CRM Import endpoints using C# and HttpClient

Hello! I am attempting to import some data using the CRM Imports endpoint to import some contacts, but am not having much success. I have looked at several other posts and the scant documentation, but am still missing something. I always get a 400 error back, but no information indicating what exactly is wrong with the request. Any insight is much appreciated! NOTE: I have also tried this using the RestSharp client and get the same error back (that code is also below). Thanks in advance for any guidance you can provide!

Here’s some additional info -

Sample of data being imported (CSV format):

ContactOwnerId,ContactFirstName,ContactLastName,ContactAddress,ContactCity,ContactState,ContactZip,ContactMobile,ContactPhone,ContactEmail,DateEntered,GeneralNotes

85766999,Nancy,Gzzz,“Bickford, but moving soon”,8/12/2021 15:56,<p>some test notes</p>

85766999,Isador,Lzzz,16361 James Street,City,KS,99999,,913-555-1212,someone@aol.com,8/12/2021 12:59,<p>some test notes</p>

Import Request data:

{

“name”: “test_import”,

“files”: [

{

“fileName”: “importTest1.csv”,

“fileFormat”: “CSV”,

“dateFormat”: “DAY_MONTH_YEAR”,

“fileImportPage”: {

“hasHeader”: true,

“columnMappings”: [

{

“ignored”: false,

“columnName”: “ContactOwnerId”,

“idColumnType”: null,

“propertyName”: “HUBSPOT_OWNER_ID”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “ContactFirstName”,

“idColumnType”: null,

“propertyName”: “firstname”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “ContactLastName”,

“idColumnType”: null,

“propertyName”: “lastname”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “ContactAddress”,

“idColumnType”: null,

“propertyName”: “address”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “ContactCity”,

“idColumnType”: null,

“propertyName”: “city”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “ContactState”,

“idColumnType”: null,

“propertyName”: “state”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “ContactZip”,

“idColumnType”: null,

“propertyName”: “zip”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “ContactMobile”,

“idColumnType”: null,

“propertyName”: “mobilephone”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “ContactPhone”,

“idColumnType”: null,

“propertyName”: “phone”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “ContactEmail”,

“idColumnType”: “HUBSPOT_ALTERNATE_ID”,

“propertyName”: “email”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “DateEntered”,

“idColumnType”: null,

“propertyName”: “createdate”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-1”,

“associationIdentifierColumn”: false

},

{

“ignored”: false,

“columnName”: “GeneralNotes”,

“idColumnType”: null,

“propertyName”: “body”,

“foreignKeyType”: null,

“columnObjectTypeId”: “0-4”,

“associationIdentifierColumn”: false

}

]

}

}

]

}

C# Code (using HttpClient):

 var formData = new MultipartFormDataContent();

 //files
 var importFileBinary = File.ReadAllBytes(@"C:\[path here]\importTest1.csv");
 var fileContent = new ByteArrayContent(importFileBinary);
 fileContent.Headers.Add("Content-Type", "text/csv");
 formData.Add(fileContent, "file", "importTest1.csv");

 //import request
 var importRequest = File.ReadAllText(@"C:\[path here]\ImportInfo.json");
 var importRequestContent = new StringContent(importRequest);
 formData.Add(importRequestContent, "importRequest");

 //post it
 var requestUrl = "https://api.hubapi.com/crm/v3/imports/?hapikey=3...00";
 var response = await client.PostAsync(requestUrl, formData);
 if (response != null)
 {
 Console.WriteLine(response.ToString());
 }

C# Code (using RestSharp Client):

 var importRequest = await File.ReadAllTextAsync(@"C:\[path here]\ImportInfo.json");
 var client = new RestClient("https://api.hubapi.com/crm/v3/imports/?hapikey=d...94");
 client.Timeout = -1;
 var request = new RestRequest(Method.POST);
 request.AddHeader("Content-Type", "multipart/form-data");
 request.AlwaysMultipartFormData = true;
 request.AddParameter("importRequest", importRequest);
 request.AddFile("files", @"C:\[path here]\importTest1.csv");
 IRestResponse response = client.Execute(request);
 if (response != null)
 {
 Console.WriteLine(response.StatusCode);
 Console.WriteLine(response.ErrorMessage);
 Console.WriteLine(response.Content);
 }

@CGriffie

Are you able to use the get information about any import endpoint to get more info? /crm/v3/imports/{importId}

@gotmike , any thoughts here?

@dennisedson

Unfortunately, no import id is returned. Just the 400 error. I’m thinking something is wrong with my import request formatting, but i’m not sure what.

Not sure that an answer is needed any longer, but the following is working for me:

 var formContent = new MultipartFormDataContent();
 formContent.Add(
 new StringContent(
 JsonConvert.SerializeObject(importRequest),
 System.Text.Encoding.UTF8,
 "application/json"),
 "importRequest");

 // importFiles is a string[] of full file paths
 foreach (var importFile in importFiles)
 {
 formContent.Add(
 new StreamContent(File.OpenRead(importFile)),
 "files",
 Path.GetFileName(importFile));
 }

 return new HttpRequestMessage(
 HttpMethod.Post,
 "https://api.hubapi.com/crm/v3/imports?hapikey=..."
 )
 {
 Content = formContent
 };

then the request is sent to the server as:

 request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

 var response = await _httpClient.SendAsync(request, cancellationToken);
 if (response.Content != null)
 {
 var jsonResponse = await response.Content.ReadAsStringAsync();
 return (response.StatusCode, jsonResponse);
 }

I ended up going a different route, but THANK YOU for this information!