Skip to main content
Solved

How to obtain profiles, filters and metrics via Klaviyo Python SDK


Forum|alt.badge.img

Hello Community!


How do I use the filter functions in the Klaviyo Python SDK? I want to filter the 'created' field by date. Let's say I want to pull all profiles that have a 'created' date after 2023-01-01. Here is the code I use without the filter. What would the filter need to look like so that I can integrate it into this query? The documentation is not very helpful in this respect.

from klaviyo_api import KlaviyoAPI
import pandas as pd
klaviyo = KlaviyoAPI("API Key", max_delay=60, max_retries=3, test_host=None)
fields_profile = ['email', 'created', 'external_id', 'subscriptions', 'properties', 'subscriptions.email']
page_size = 100
profiles = klaviyo.Profiles.get_profiles(
fields_profile=fields_profile,
page_size=page_size
)

 

Also how can I pull all profiles via the Klaviyo Python SDK?

Unfortunately, I can only extract the first 100 profiles via the page_size. However, I would like to obtain all profiles via the API.  I have already tried many variants, but have not yet found a solution. I already have the following code. How would I have to extend it?


from klaviyo_api import KlaviyoAPI
import pandas as pd
klaviyo = KlaviyoAPI("API Key", max_delay=60, max_retries=3, test_host=None)
fields_profile = ['email', 'created', 'external_id', 'subscriptions', 'properties', 'subscriptions.email']
page_size = 100
profiles = klaviyo.Profiles.get_profiles(
fields_profile=fields_profile,
page_size=page_size
)

 

Is there also a way to obtain profiles with the metric 'subscribed to list' via the Klaviyo Python SDK? I've tried different ways but haven't found out yet if there is a way to extract the metric 'subscribed to list' via the profiles. Through which field would this be possible? Does anyone here have any experience? I would be happy to hear from you!

 

Best,

Emanuel

Best answer by Brian Turcotte

HI @OTL_Emanuel!
 

Have you tried f-stringing and quoting the filters (no spaces as well)? For example:

klaviyo.Events.get_events(   filter=f'equals(metric_id,"METRIC_ID")',   fields_event=["event_properties"],   sort="datetime",)

The above uses the Get Events endpoint, but Similar logic could be applied to the Get Profiles endpoint. 

 

Regarding your second point of gathering all profiles at once, the Get Profiles endpoint only allows a maximum return value of 100, but you can you retrieve all profiles by paginating through the responses:

 

Finally, if you are looking for the lists that a profile is subscribed to, you can use the Get Profile Lists endpoint:

 

Let me know if I misunderstood your question, but I hope this helps and thanks for using the Community!

 

Best,

Brian

View original
Did this topic or the replies in the thread help you find an answer to your question?

2 replies

Brian Turcotte
Forum|alt.badge.img+37

Hi @OTL_Emanuel,

 

I’m going to check in with Engineering on this post and I’ll update the thread when I have more information!

 

Best,

Brian


Brian Turcotte
Forum|alt.badge.img+37
  • Klaviyo Alum
  • 1393 replies
  • Answer
  • April 19, 2023

HI @OTL_Emanuel!
 

Have you tried f-stringing and quoting the filters (no spaces as well)? For example:

klaviyo.Events.get_events(   filter=f'equals(metric_id,"METRIC_ID")',   fields_event=["event_properties"],   sort="datetime",)

The above uses the Get Events endpoint, but Similar logic could be applied to the Get Profiles endpoint. 

 

Regarding your second point of gathering all profiles at once, the Get Profiles endpoint only allows a maximum return value of 100, but you can you retrieve all profiles by paginating through the responses:

 

Finally, if you are looking for the lists that a profile is subscribed to, you can use the Get Profile Lists endpoint:

 

Let me know if I misunderstood your question, but I hope this helps and thanks for using the Community!

 

Best,

Brian