APIs & Integrations

kw2020
Member

Is there an example of how to access the import api endpoint using c# and .net

SOLVE

Hi, is there an example of how to access the import api endpoint using c# and .net?

I am getting this error:

{
"status": "error",
"message": "Unable to process JSON",
"correlationId": "d828a9cc-643c-4262-9e5a-a6beff2d3b8e"
}

I've tried a few different things. This is what I have at the moment. Thanks.

string importURL = "https://api.hubapi.com/crm/v3/imports/";

csvCustomers = "\"bob@taplogic.com\",\"bob\",\"bobbers\"";

byte[] bytes = Encoding.ASCII.GetBytes(csvCustomers);

string urlParameters = "?hapikey=409b5c5d-56b0-4d0b-9449-7eb7378503be";

JArray columnMappingsJSONArray = new JArray();

columnMappingsJSONArray.Add(new JObject(
new JProperty("ignored", false),
new JProperty("columnName", "email"),
new JProperty("idColumnType", "HUBSPOT_ALTERNATE_ID"),
new JProperty("propertyName", "email"),
new JProperty("foreignKeyType", null),
new JProperty("columnObjectType", "CONTACT"),
new JProperty("associationIdentifierColumn", false)
));

columnMappingsJSONArray.Add(new JObject(
new JProperty("ignored", false),
new JProperty("columnName", "firstname"),
new JProperty("idColumnType", "null"),
new JProperty("propertyName", "firstname"),
new JProperty("foreignKeyType", null),
new JProperty("columnObjectType", "CONTACT"),
new JProperty("associationIdentifierColumn", false)
));

columnMappingsJSONArray.Add(new JObject(
new JProperty("ignored", false),
new JProperty("columnName", "lastname"),
new JProperty("idColumnType", "null"),
new JProperty("propertyName", "lastname"),
new JProperty("foreignKeyType", null),
new JProperty("columnObjectType", "CONTACT"),
new JProperty("associationIdentifierColumn", false)
));

JArray filesJSONArray = new JArray();

filesJSONArray.Add(new JObject(
new JProperty("fileName", "API_Import_1.csv"),
new JProperty("fileImportPage",
new JObject(
new JProperty("hasHeader", false),
new JProperty("columnMappings", columnMappingsJSONArray)
)
)
)
);

JObject importRequestJSON = new JObject(
new JObject(
new JProperty("name", "API_Import_1"),
new JProperty("files", filesJSONArray)
)
);

var uriRequest = new Uri(importURL + urlParameters);

var myHttpClient = new HttpClient();

ByteArrayContent byteContent = new ByteArrayContent(bytes);
byteContent.Headers.Add("Content-Type", "application/octet-stream");

StringContent stringContentImportRequest = new StringContent(
importRequestJSON.ToString(),
System.Text.Encoding.UTF8, "application/json");

StringContent stringContentFiles1 = new StringContent(
filesJSON.ToString());

MultipartFormDataContent multipartContent = new MultipartFormDataContent();
multipartContent.Add(stringContentImportRequest, "importRequest");
multipartContent.Add(byteContent, "files", "API_Import_1.csv");

var response = await myHttpClient.PostAsync(uriRequest.ToString(), multipartContent);

writeToLog(response.Content.ToString());

myHttpClient.Dispose();

 

0 Upvotes
1 Accepted solution
WendyGoh
Solution
HubSpot Employee
HubSpot Employee

Is there an example of how to access the import api endpoint using c# and .net

SOLVE

Hey @kw2020,

 

A generic C# code like this:

 

var client = new RestClient("https://api.hubapi.com/crm/v3/imports");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("Cookie", "__cfduid=d09cae7faeb5960760427f1cf3f5bfd7a1597311822");
request.AddParameter("importRequest", "{
  \"name\": \"test_import\",
  \"files\": [
    {
      \"fileName\": \"final_emails.csv\",
      \"fileFormat\": \"CSV\",
      \"fileImportPage\": {
        \"hasHeader\": true,
        \"columnMappings\": [
          {
            \"ignored\": false,
            \"columnName\": \"First Name\",
            \"idColumnType\": null,
            \"propertyName\": \"firstname\",
            \"foreignKeyType\": null,
            \"columnObjectType\": \"CONTACT\",
            \"associationIdentifierColumn\": false
          },
          {
            \"ignored\": false,
            \"columnName\": \"Email\",
            \"idColumnType\": \"HUBSPOT_ALTERNATE_ID\",
            \"propertyName\": \"email\",
            \"foreignKeyType\": null,
            \"columnObjectType\": \"CONTACT\",
            \"associationIdentifierColumn\": false
          },
          {
            \"ignored\": false,
            \"columnName\": \"Company ID\",
            \"idColumnType\": \"HUBSPOT_OBJECT_ID\",
            \"propertyName\": null,
            \"columnObjectType\": \"COMPANY\",
            \"associationIdentifierColumn\": false
          }
        ]
      }
    }
  ]
}");
request.AddFile("files", "/path/to/file");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

 

Should work. One thing to note though, you should change the file path -- for now, I didn't select a file. 

View solution in original post

0 Upvotes
2 Replies 2
WendyGoh
Solution
HubSpot Employee
HubSpot Employee

Is there an example of how to access the import api endpoint using c# and .net

SOLVE

Hey @kw2020,

 

A generic C# code like this:

 

var client = new RestClient("https://api.hubapi.com/crm/v3/imports");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("Cookie", "__cfduid=d09cae7faeb5960760427f1cf3f5bfd7a1597311822");
request.AddParameter("importRequest", "{
  \"name\": \"test_import\",
  \"files\": [
    {
      \"fileName\": \"final_emails.csv\",
      \"fileFormat\": \"CSV\",
      \"fileImportPage\": {
        \"hasHeader\": true,
        \"columnMappings\": [
          {
            \"ignored\": false,
            \"columnName\": \"First Name\",
            \"idColumnType\": null,
            \"propertyName\": \"firstname\",
            \"foreignKeyType\": null,
            \"columnObjectType\": \"CONTACT\",
            \"associationIdentifierColumn\": false
          },
          {
            \"ignored\": false,
            \"columnName\": \"Email\",
            \"idColumnType\": \"HUBSPOT_ALTERNATE_ID\",
            \"propertyName\": \"email\",
            \"foreignKeyType\": null,
            \"columnObjectType\": \"CONTACT\",
            \"associationIdentifierColumn\": false
          },
          {
            \"ignored\": false,
            \"columnName\": \"Company ID\",
            \"idColumnType\": \"HUBSPOT_OBJECT_ID\",
            \"propertyName\": null,
            \"columnObjectType\": \"COMPANY\",
            \"associationIdentifierColumn\": false
          }
        ]
      }
    }
  ]
}");
request.AddFile("files", "/path/to/file");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

 

Should work. One thing to note though, you should change the file path -- for now, I didn't select a file. 

0 Upvotes
kw2020
Member

Is there an example of how to access the import api endpoint using c# and .net

SOLVE

Thanks for the help. I've made some progress, I may have more questions.

0 Upvotes