I’m trying to retreive raw data for each email a contact has been sent. This would result in a large table with millions of rows, since we send millions of emails.
I thought that email activities would be the end point that need for this and wrote a python script (below). This ran for a while and when it concluded I had only tens of thousands of rows, not millions as expected.
Have I misunderstood what email activities is? How can I retreive send level data per contact_id? One record per instance of an email being sent across any and all campaigns.
import requests
import pandas as pd
import sys
if len(sys.argv) < 2:
print("Usage: python email_activities.py <API_KEY>")
sys.exit(1)
API_KEY = sys.argv[1]
BASE_URL = "https://<our org name>.api-us1.com/api/3"
headers = {
"Api-Token": API_KEY,
"Accept": "application/json"
}
all_activities = []
offset = 0
limit = 100
while True:
response = requests.get(
f"{BASE_URL}/emailActivities",
headers=headers,
params={"limit": limit, "offset": offset}
)
data = response.json()
activities = data.get("emailActivities", [])
if not activities:
break
all_activities.extend(activities)
print(f"Fetched {len(all_activities)} records so far...")
offset += limit
print(f"\nTotal records: {len(all_activities)}")
df = pd.DataFrame(all_activities)
df.to_csv("email_activities.csv", index=False)
print("Saved to email_activities.csv")
print(f"\nColumns: {list(df.columns)}")
print(f"\n{df.head()}")
