VOOZH about

URL: https://www.cdata.com/kb/tech/klaviyo-ado-powershell.rst

⇱ Automate Klaviyo Integration Tasks from PowerShell


Automate Klaviyo Integration Tasks from PowerShell

👁 Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access Klaviyo data from PowerShell? This article demonstrates how to utilize the Klaviyo Cmdlets for tasks like connecting to Klaviyo data, automating operations, downloading data, and more.

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.

PowerShell Cmdlets or ADO.NET Provider?

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'

  1. Navigate to 'Settings' > 'API Keys'
  2. Click 'Create API Key'.
  3. Name your API key and choose the desired scopes.

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.

PowerShell

  1. Install the module:

    Install-Module KlaviyoCmdlets
  2. Connect:

    $klaviyo = Connect-Klaviyo -APIKey "$APIKey"
    
  3. 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'}
    

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Klaviyo\lib\System.Data.CData.Klaviyo.dll")
     
  2. Connect to Klaviyo:

     
    $conn= New-Object System.Data.CData.Klaviyo.KlaviyoConnection("APIKey=my_api_key;")
    $conn.Open()
    
  3. 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 Data

PowerShell

Update-Klaviyo -Connection $Klaviyo -Columns @('Id','Name') -Values @('MyId', 'MyName') -Table Campaigns -Id "MyId"

ADO.NET

$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()

Insert Klaviyo Data

PowerShell

Add-Klaviyo -Connection $Klaviyo -Table Campaigns -Columns @("Id", "Name") -Values @("MyId", "MyName") 

ADO.NET

$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()

Delete Klaviyo Data

PowerShell

Remove-Klaviyo -Connection $Klaviyo -Table "Campaigns" -Id "MyId"

ADO.NET

$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

Ready to get started?

Download a free trial of the Klaviyo Data Provider to get started:

 Download Now

Learn more:

👁 Klaviyo Icon
Klaviyo ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Klaviyo.