How to import new companies from CSV file via API by Power Automate Desktop

Hi? I want to make an automated import by PAD. I made a flow with powerShell script. It tuns whit an output this:
API Error: Státuszkód: , Üzenet: A távoli kiszolgáló a következő hibát küldte vissza: (500) Belső kiszolgálóhiba.

Can you help me to fix it? THX Kalman.

Brevo Company Import Script - Tisztított, 500-as hiba fókusz

Define the API endpoint and the file path (közvetlenül beillesztve)

$apiUrl = « https://api.brevo.com/v3/companies/import »
$filePath = « fajl.csv » # Ellenőrizd az elérési utat és a fájlnevet
$apiKey = « xkeysib-APIKEY » # Itt kell a teljes API kulcsod

Ellenőrizzük, hogy létezik-e a CSV fájl

if (-not (Test-Path $filePath)) {
Write-Error « HIBA: A megadott CSV fájl nem található: $filePath »
Write-Output « HIBA: A megadott CSV fájl nem található: $filePath » # Kimenet a PAD számára
exit 1
}

Define the JSON body for import settings, as required by Brevo’s « json_body » parameter

$jsonBodySettings = @{
« link_entities » = $true;
« unlink_entities » = $false;
« update_existing_records » = $true;
« unset_empty_attributes » = $false;
} | ConvertTo-Json -Compress
Write-Output « JSON tartalom: $jsonBody »

$jsonBodyString = $jsonBodySettings

Multipart/form-data body manuális összeállítása

$boundary = [Guid]::NewGuid().ToString()
$crlf = « rn »

$bodyParts = @()

1. rész: A CSV fájl

$fileName = [System.IO.Path]::GetFileName($filePath)
$bodyParts += « –$boundary$crlf »
$bodyParts += « Content-Disposition: form-data; name="file »; filename="$fileName"$crlf"
$bodyParts += « Content-Type: application/octet-stream$crlf » # Vagy « text/csv »
$bodyParts += « $crlf »
$body = [System.Text.Encoding]::UTF8.GetBytes(($bodyParts -join ‹  ›))
$fileContentBytes = [System.IO.File]::ReadAllBytes($filePath)
$body = $body + $fileContentBytes

2. rész: A JSON beállítások (« json_body »)

$bodyParts = @()
$bodyParts += « $crlf–$boundary$crlf »
$bodyParts += « Content-Disposition: form-data; name="json_body »$crlf"
$bodyParts += « Content-Type: application/json$crlf »
$bodyParts += « $crlf »
$bodyParts += $jsonBodyString
$body = $body + [System.Text.Encoding]::UTF8.GetBytes(($bodyParts -join ‹  ›))

Befejező boundary

$body += [System.Text.Encoding]::UTF8.GetBytes(« $crlf–$boundary–$crlf »)

Define the HTTP headers

$headers = @{
« Accept » = « application/json »
« api-key » = $apiKey
« Content-Type » = « multipart/form-data; boundary=$boundary »
}

try {
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body -ContentType « multipart/form-data; boundary=$boundary »

# Sikeres válasz kiírása
$outputForPad = $response | ConvertTo-Json -Compress
Write-Output $outputForPad # Ez kerül a PAD változójába
exit 0

}
catch {
$errorMessage = « API Hiba: Státuszkód: $($.Exception.Response.StatusCode.Value), Üzenet: $($.Exception.Message) »
if ($.Exception.Response -and $.Exception.Response.Content) {
$apiContent = $_.Exception.Response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 10
$errorMessage += « , API Válasz Tartalma: $apiContent »
}

Write-Error $errorMessage
Write-Output $errorMessage # Ez kerül a PAD változójába hiba esetén
exit 1

}