Skip to main content

Pigment OS scripting imports

  • January 14, 2025
  • 0 replies
  • 98 views

Forum|alt.badge.img+3

Here’s a collection of Pigment import scripts for the commonly used ‘scripting’ languages.

Please read these articles to understand the process around generating API keys and the process involved in importing CSV data into Pigment.

 

1. Generate Pigment Import/Export API key

https://community.pigment.com/security-permissions-82/manage-api-keys-226

 

2. For Imports into Pigment (to understand the process)

https://community.pigment.com/importing-and-exporting-data-95/how-to-trigger-an-import-with-apis-230

 

Linux/UNIX bash script

#!/bin/bash

# Set environment variables
export IMPORT_API_KEY="YOUR_API_KEY"
export CONFIGURATION_ID="YOUR_CONFIGURATION_ID"
export FILE_PATH="YOUR_FILE_PATH"

# Function to post the CSV
post_csv() {
response=$(curl -s -o response.json -w "%{http_code}" -X POST \
"https://pigment.app/api/import/push/csv?configurationId=${CONFIGURATION_ID}" \
-H "Authorization: Bearer ${IMPORT_API_KEY}" \
--data-binary "@${FILE_PATH}")

if [ "$response" -eq 200 ]; then
echo "CSV uploaded successfully."
else
echo "Failed to upload CSV. HTTP status code: $response"
cat response.json
fi
}

# Check if required environment variables are set
if [ -z "$IMPORT_API_KEY" ] || [ -z "$CONFIGURATION_ID" ] || [ -z "$FILE_PATH" ]; then
echo "Error: Missing required environment variables."
exit 1
fi

# Check if the file exists
if [ ! -f "$FILE_PATH" ]; then
echo "Error: File not found at $FILE_PATH"
exit 1
fi

# Execute the function
post_csv

Instructions:

  1. Replace YOUR_API_KEY, YOUR_CONFIGURATION_ID, and YOUR_FILE_PATH with your actual API key, configuration ID, and file path.

  2. Save the script to a file, for example, upload_csv.sh.

  3. Make the script executable by running:

    chmod +x upload_csv.sh
  4. Run the script:

    ./upload_csv.sh

 

Windows PowerShell

# Set environment variables
$env:IMPORT_API_KEY = "YOUR_API_KEY"
$env:CONFIGURATION_ID = "YOUR_CONFIGURATION_ID"
$env:FILE_PATH = "YOUR_FILE_PATH"

# Function to post the CSV
function Post-CSV {
$response = Invoke-RestMethod -Uri "https://pigment.app/api/import/push/csv?configurationId=$env:CONFIGURATION_ID" `
-Method Post `
-Headers @{ "Authorization" = "Bearer $env:IMPORT_API_KEY" } `
-InFile $env:FILE_PATH `
-ContentType "application/octet-stream" `
-ErrorAction Stop

if ($response.StatusCode -eq 200) {
Write-Output "CSV uploaded successfully."
} else {
Write-Output "Failed to upload CSV. HTTP status code: $($response.StatusCode)"
$response.Content | ConvertFrom-Json | Format-List
}
}

# Check if required environment variables are set
if (-not $env:IMPORT_API_KEY -or -not $env:CONFIGURATION_ID -or -not $env:FILE_PATH) {
Write-Output "Error: Missing required environment variables."
exit 1
}

# Check if the file exists
if (-not (Test-Path -Path $env:FILE_PATH)) {
Write-Output "Error: File not found at $env:FILE_PATH"
exit 1
}

# Execute the function
Post-CSV

Instructions:

  1. Replace YOUR_API_KEY, YOUR_CONFIGURATION_ID, and YOUR_FILE_PATH with your actual API key, configuration ID, and file path.

  2. Save the script to a file, for example, upload_csv.ps1.

  3. Run the script in PowerShell:

    .\upload_csv.ps1

 

Windows ‘batch’ file

@echo off
setlocal

:: Set environment variables
set "IMPORT_API_KEY=YOUR_API_KEY"
set "CONFIGURATION_ID=YOUR_CONFIGURATION_ID"
set "FILE_PATH=YOUR_FILE_PATH"

:: Function to post the CSV
:post_csv
curl -s -o response.json -w "%%{http_code}" -X POST ^
"https://pigment.app/api/import/push/csv?configurationId=%CONFIGURATION_ID%" ^
-H "Authorization: Bearer %IMPORT_API_KEY%" ^
--data-binary "@%FILE_PATH%"
set response=%errorlevel%
if %response%==200 (
echo CSV uploaded successfully.
) else (
echo Failed to upload CSV. HTTP status code: %response%
type response.json
)
goto :eof

:: Check if required environment variables are set
if "%IMPORT_API_KEY%"=="" (
echo Error: Missing IMPORT_API_KEY.
exit /b 1
)
if "%CONFIGURATION_ID%"=="" (
echo Error: Missing CONFIGURATION_ID.
exit /b 1
)
if "%FILE_PATH%"=="" (
echo Error: Missing FILE_PATH.
exit /b 1
)

:: Check if the file exists
if not exist "%FILE_PATH%" (
echo Error: File not found at %FILE_PATH%.
exit /b 1
)

:: Execute the function
call :post_csv
endlocal

Instructions:

  1. Replace YOUR_API_KEY, YOUR_CONFIGURATION_ID, and YOUR_FILE_PATH with your actual API key, configuration ID, and file path.

  2. Save the script to a file, for example, upload_csv.bat.

  3. Run the script by double-clicking the .bat file or executing it in the Command Prompt.

 

Note: API rate limits in effect: