errors calling /crm/v3/imports via powershell

I am working on settign up a company & contacts import from file using powershell but am running into a problem properly formatting the request and getting it to import both files.

I get this error:

Exception: Cannot bind parameter ‘Form’. Cannot convert the “[the form JSON is here]” value of type “System.String” to type “System.Collections.IDictionary”.

Here is my code:

Clear-Host

$hubSpotheaders = @{
 Authorization = "Bearer pat-xxxxxxxxxxxxxxx"
}

$path = "C:\pathtofile"
$companyupdatefile = "$path\Paragon Companies_20241105.xlsx"
$contactfile = "$path\Paragon Contacts_20241105.xlsx"

$post = 'importRequest={
 "name": "isolved Paragon 20241105",
 "dateFormat": "MONTH_DAY_YEAR",
 "createContactListFromImport": true,
 "marketableContactImport": false,
 "files": [
 {
 "fileName": "Paragon Contacts_20241105.xlsx",
 "fileFormat": "SPREADSHEET",
 "fileImportPage": {
 "hasHeader": true,
 "columnMappings": [
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Company Name",
 "propertyName": "company"
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Client ID",
 "propertyName": "client_id",
 "associationIdentifierColumn": true
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Phone Number",
 "propertyName": "phone"
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Email",
 "propertyName": "email"
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Job Title",
 "propertyName": "jobtitle"
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Type of Contact",
 "propertyName": "type_of_contact"
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Last Name",
 "propertyName": "lastname"
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "First Name",
 "propertyName": "firstname"
 },
 {
 "columnObjectTypeId": "0-1",
 "columnName": "Type",
 "propertyName": "type"
 }
 ]
 }
 },
 {
 "fileName": "Paragon Companies_20241105.xlsx",
 "fileFormat": "SPREADSHEET",
 "fileImportPage": {
 "hasHeader": true,
 "columnMappings": [
 {
 "columnObjectTypeId": "0-2",
 "toColumnObjectTypeId": "0-1",
 "columnName": "Client ID",
 "propertyName": null,
 "foreignKeyType": {
 "associationTypeId": 280,
 "associationCategory": "HUBSPOT_DEFINED"
 }
 },
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Company Name",
 "propertyName": "name"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Legal\Company ID",
 "propertyName": "company_id"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "DBA",
 "propertyName": "dba"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "EIN",
 "propertyName": "ein"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Account Status",
 "propertyName": "account_status"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Record ID",
 "propertyName": "hs_object_id",
 "columnType": "HUBSPOT_OBJECT_ID"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Active Employee Count",
 "propertyName": "active_employee_count"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Payroll System",
 "propertyName": "payroll_system"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Type",
 "propertyName": "type"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Company Legal State",
 "propertyName": "client_legal_state"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Legal Address",
 "propertyName": "legal_address"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Service List",
 "propertyName": "service_list"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Payroll Frequency",
 "propertyName": "frequency"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Parent Company",
 "propertyName": "hs_parent_company_id"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Termination Tax Effective Date",
 "propertyName": "termination_tax_effective_date"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Termination Date Notified",
 "propertyName": "termination_date_notified"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Termination Process Quarterlies Thru",
 "propertyName": "termination_process_quarterlies_thru"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Termination Process Zero Filings Thru",
 "propertyName": "termination_process_zero_filings_thru"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Termination Annual\Process W2",
 "propertyName": "termination_annual_process_w2"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Termination Final Tax Return Date",
 "propertyName": "termination_final_tax_return_date"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Termination Reason",
 "propertyName": "termination_reason"
 }, 
 {
 "columnObjectTypeId": "0-2",
 "columnName": "Term Date",
 "propertyName": "term_date"
 }
 ]
 }
 }
 ]

}'

$URL = "https://api.hubapi.com/crm/v3/imports"
try {
 Invoke-RestMethod -Uri $URL -Headers $hubSpotheaders -Method Post -ContentType "multipart/form-data" -form $post
} catch {
 write-host "Error:" $_.ErrorDetails.Message
 write-host "Exception:" $_.Exception.Message
}

Hi @DanaIrvine :waving_hand:

Unfortunately, I’ve not been able to replicate the issue you’re experiencing and I’m not familiar with the programming language you’ve supplied. Just in case it helps, I’ve converted a test call I was able to make successfully on my end to the “PowerShell - Invoke-RestMethod”. It might be helpful to compare this with the call you’re making to see if there are any differences that could potentially be causing the issue:

$headers = @{
 "Authorization" = "Bearer pat-eu1-abc-xyz"
}
$form = @{
 "importRequest" = "{`"name`":`"test 20241108-1`",`"dateFormat`":`"MONTH_DAY_YEAR`",`"createContactListFromImport`":true,`"marketableContactImport`":false,`"files`":[{`"fileName`":`"test.xlsx`",`"fileFormat`":`"SPREADSHEET`",`"fileImportPage`":{`"hasHeader`":true,`"columnMappings`":[{`"columnObjectTypeId`":`"0-1`",`"columnName`":`"Company Name`",`"propertyName`":`"company`"},{`"columnObjectTypeId`":`"0-1`",`"columnName`":`"Email`",`"propertyName`":`"email`"},{`"columnObjectTypeId`":`"0-1`",`"columnName`":`"Last Name`",`"propertyName`":`"lastname`"},{`"columnObjectTypeId`":`"0-1`",`"columnName`":`"First Name`",`"propertyName`":`"firstname`"}]}}]}"
 "files" = Get-Item "/Users/xyz/test.csv"
}
$response = Invoke-RestMethod -Uri "https://api.hubapi.com/crm/v3/imports" `
 -Method Post `
 -Headers $headers `
 -ContentType "multipart/form-data" `
 -Form $form

If you’re in a position to pivot the programming language you’re using to interact with HubSpot’s APIs, I could offer more support. I hope this proves helpful. Please let me know if you have any follow-up questions.

That helped some. Now I am getting this error which leads me to believe I have an error in the JSON data:

{
“status”: “error”,
“message”: “Unable to process JSON”,
“correlationId”: “8aaed8d5-4aaf-4fd5-a32f-9c9e11eb22d7”
}

THANK YOU!!!

You got me the piece I was missing.