![]() |
VOOZH | about |
The CData Cmdlets for Klaviyo are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Klaviyo.
The Cmdlets are not only a PowerShell interface to Klaviyo, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Klaviyo data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Klaviyo. To access Klaviyo data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Klaviyo.
Once you have acquired the necessary connection properties, accessing Klaviyo data in PowerShell can be enabled in three steps.
To authenticate to Klaviyo, provide an API Key. You can generate or view your API keys under 'My Account'
To connect in your CData solutions, set API Key to your Klaviyo API key.
If you wish to use OAuth authentication, refer to the Help documenation.
Install the module:
Install-Module KlaviyoCmdlets
Connect:
$klaviyo = Connect-Klaviyo -APIKey "$APIKey"
Search for and retrieve data:
$status = "draft" $campaigns = Select-Klaviyo -Connection $klaviyo -Table "Campaigns" -Where "Status = `'$Status`'" $campaigns
You can also use the Invoke-Klaviyo cmdlet to execute SQL commands:
$campaigns = Invoke-Klaviyo -Connection $klaviyo -Query 'SELECT * FROM Campaigns WHERE Status = @Status' -Params @{'@Status'='draft'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Klaviyo\lib\System.Data.CData.Klaviyo.dll")
Connect to Klaviyo:
$conn= New-Object System.Data.CData.Klaviyo.KlaviyoConnection("APIKey=my_api_key;")
$conn.Open()
Instantiate the KlaviyoDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, Name from Campaigns"
$da= New-Object System.Data.CData.Klaviyo.KlaviyoDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.id $_.name
}
Update-Klaviyo -Connection $Klaviyo -Columns @('Id','Name') -Values @('MyId', 'MyName') -Table Campaigns -Id "MyId"
$cmd = New-Object System.Data.CData.Klaviyo.KlaviyoCommand("UPDATE Campaigns SET Status='draft' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Klaviyo.KlaviyoParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-Klaviyo -Connection $Klaviyo -Table Campaigns -Columns @("Id", "Name") -Values @("MyId", "MyName")
$cmd = New-Object System.Data.CData.Klaviyo.KlaviyoCommand("INSERT INTO Campaigns (Status) VALUES (@myStatus)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Klaviyo.KlaviyoParameter("@myStatus","draft")))
$cmd.ExecuteNonQuery()
Remove-Klaviyo -Connection $Klaviyo -Table "Campaigns" -Id "MyId"
$cmd = New-Object System.Data.CData.Klaviyo.KlaviyoCommand("DELETE FROM Campaigns WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Klaviyo.KlaviyoParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Klaviyo Data Provider to get started:
Download NowLearn more:
👁 Klaviyo IconRapidly create and deploy powerful .NET applications that integrate with Klaviyo.