# Help Using CRM Import endpoints using C# and HttpClient

**URL:** <https://community.hubspot.com/t/help-using-crm-import-endpoints-using-c-and-httpclient/42526>\
**Category:** APIs & Integrations\
**Tags:** contacts\
**Created:** [August 18, 2021, 9:08pm UTC](https://community.hubspot.com/t/help-using-crm-import-endpoints-using-c-and-httpclient/42526 "2021-08-18T21:08:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CGriffie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cgriffie/32/31201_2.png) [@CGriffie](https://community.hubspot.com/u/CGriffie)\
**Post date:** [August 18, 2021, 9:08pm UTC](https://community.hubspot.com/t/help-using-crm-import-endpoints-using-c-and-httpclient/42526/1 "2021-08-18T21:08:27Z")

</div>

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](mailto: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):**

```lia-code-sample
 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):**

```lia-code-sample
 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);
 }

```

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [August 20, 2021, 3:36pm UTC](https://community.hubspot.com/t/help-using-crm-import-endpoints-using-c-and-httpclient/42526/2 "2021-08-20T15:36:46Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![CGriffie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cgriffie/32/31201_2.png) [@CGriffie](https://community.hubspot.com/u/CGriffie)\
**Post date:** [August 20, 2021, 4:17pm UTC](https://community.hubspot.com/t/help-using-crm-import-endpoints-using-c-and-httpclient/42526/3 "2021-08-20T16:17:27Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![bengavin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bengavin/32/191590_2.png) [@bengavin](https://community.hubspot.com/u/bengavin)\
**Post date:** [October 26, 2021, 6:04pm UTC](https://community.hubspot.com/t/help-using-crm-import-endpoints-using-c-and-httpclient/42526/4 "2021-10-26T18:04:01Z")

</div>

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

```lia-code-sample
 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:

```lia-code-sample
 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);
 }

```

---

<div class="post-metadata">

**Author:** ![CGriffie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cgriffie/32/31201_2.png) [@CGriffie](https://community.hubspot.com/u/CGriffie)\
**Post date:** [October 26, 2021, 6:41pm UTC](https://community.hubspot.com/t/help-using-crm-import-endpoints-using-c-and-httpclient/42526/5 "2021-10-26T18:41:39Z")

</div>

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