Solved

Access list of segments via API?

  • 24 January 2022
  • 4 replies
  • 196 views

Userlevel 2
Badge +9

Hey guys,

Is there a way to access the list of segments via API? I have tried the GET request for Lists but it only fetches details of lists and not the segments. Is there a specific request to get a list of segments?

The code that I am using for fetching lists: 
 

import requests

url = "https://a.klaviyo.com/api/v2/list/LIST_ID/exclusions/all?api_key=API_KEY"

headers = {"Accept": "application/json"}

response = requests.request("GET", url, headers=headers)

print(response.text)

 

icon

Best answer by retention 24 January 2022, 19:46

View original

4 replies

Userlevel 7
Badge +57

Hi @sourabh - In the example you showed, you specified a specific LIST_ID and I think that’s for getting all the emails/phones excluded for that List - so not sure if you’re using the right API endpoint.  

Did you try the example from the API Reference below? (remember to replace your API_KEY)

import requests

url = "https://a.klaviyo.com/api/v2/lists?api_key=API_KEY"

headers = {"Accept": "application/json"}

response = requests.request("GET", url, headers=headers)

print(response.text)

Also, I can’t remember the API returns Inactive Segments (or not) - so you may want to make sure the Segment is not Inactive too.  Let us know if this works!
 

Userlevel 2
Badge +9

Thank you for flagging my mistake @retention. I actually used the endpoint mentioned in the Get Lists

import requests

url = "https://a.klaviyo.com/api/v2/lists?api_key=API_KEY"

headers = {"Accept": "application/json"}

response = requests.request("GET", url, headers=headers)

print(response.text)

And with this endpoint, I am only receiving a list of all the lists and not segments. 

Userlevel 7
Badge +57

@sourabh - Ah - I figured out why I thought it returns a List of Segments as well as List.  Apparently, in V1 of the API, it use to do that but was removed in V2.  You can see this thread here:

Try changing your Endpoint to V1 (if it’s still available?) and see if it works? Just be caution, if it does, it’s technically a deprecated function.  I assume Klaviyo wants to build a function for just Segments (separate from Lists) at some point in the future?

Since the developer portal updated, I can’t find the V1 documentation, but you can see a list of all functions & deprecated functions here:

https://klaviyo-openapi.s3.amazonaws.com/deprecated.json

https://klaviyo-openapi.s3.amazonaws.com/spec.json

Userlevel 2
Badge +9

That’s really helpful @retention. Thank you very much. 

Reply