Skip to content

Klaviyo API Integration


What It Is

Klaviyo powers our email and SMS marketing. It holds user profiles and events such as:
Email opens, link clicks
Checkout started, placed order
Viewed product, added to cart
The Klaviyo API gives us access to these signals, which are key for behavioral modeling, churn risk, and high-LTV segmentation.

Why It Matters

Once integrated, we can:
Sync Klaviyo behavior with Recharge data by email
Predict outcomes based on user actions
Create training data from engagement (ex: “opened 2+ emails in first 7 days”)
This gives us a behavioral layer on top of transactional data.

✅ Step-by-Step Setup

1. Get Your Private API Key

Go to
Create a Private API Key with scopes:
Profiles: Read
Events: Read
Save the key securely (starts with pk_)

2. Verify Your API Works (2025-04-15 Revision)

Use this test request to fetch a profile by email. The new API uses JSON:API format and strict revision control.
library(httr)
library(jsonlite)

# 🔐 Replace with your private key
klaviyo_api_key <- "pk_XXXXXXXXXXXXXXXXXXXXXXXXXXXX"

# 👤 Replace with real customer email to test
email_to_search <- "example@example.com"

# 📦 Build API call
res <- GET(
url = "https://a.klaviyo.com/api/profiles/",
query = list(filter = paste0("equals(email,'", email_to_search, "')")),
add_headers(
`Authorization` = paste("Klaviyo-API-Key", klaviyo_api_key),
`Accept` = "application/json",
`revision` = "2025-04-15"
)
)

# ✅ Check response
print(status_code(res)) # Should return 200
parsed <- content(res, "text", encoding = "UTF-8")
profile_data <- fromJSON(parsed)

# 🔍 Inspect results
print(profile_data$data)

3. Get Recent Events (Per Profile)

This fetches recent email clicks, opens, checkout started, etc.
# Get events for a specific profile ID (you’ll get this from the `/profiles` response)
profile_id <- "01HG82WZBPQK4XN3DTV4ABY3Z8"

events_url <- paste0("https://a.klaviyo.com/api/events/?filter=equals(profile_id,'", profile_id, "')")

res <- GET(
url = events_url,
add_headers(
`Authorization` = paste("Klaviyo-API-Key", klaviyo_api_key),
`Accept` = "application/json",
`revision` = "2025-04-15"
)
)

event_data <- fromJSON(content(res, "text", encoding = "UTF-8"))
print(event_data$data)

🧠 Notes for Interns


1. Common Event Types to Track
# Examples of typical event "type" values from Klaviyo
event_types <- c("Opened Email", "Clicked Email", "Started Checkout", "Viewed Product")
2. Matching to Recharge or Shopify Customers by Email
# Match on email to link Klaviyo to Recharge
matched_df <- merge(customers_df, profile_data$data$attributes, by.x = "email", by.y = "email", all.x = TRUE)
3. Things You’ll Build
Feature columns: email_open_count_7_days, viewed_product_last_30d, etc.
Engagement score: Weighted metric based on recent Klaviyo activity
Model inputs that combine behavior + transaction data
Load content from share.geckoboard.com?
Loading external content may reveal information to 3rd parties. Learn more
Allow
Load content from share.geckoboard.com?
Loading external content may reveal information to 3rd parties. Learn more
Allow
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.