Skip to main content
Solved

API filtering


Forum|alt.badge.img+5

Hi,

Hopefully a quick one. I’m using the https://a.klaviyo.com/api/events endpoint to replace the old /v1/person/<person_id>/metric/<metric_id>/timeline endpoint (so I’m on a deadline here!)

The objective is to find out if a profile has ever unsubscribed. I know the profile ID.

So I’m trying to use filtering to narrow the results down to just one profile and just the unsubbing event.

https://a.klaviyo.com/api/events?filter=equals(profile_id,'PROFILE_ID_HERE') works fine and gets me a block of json related to the profile with data on events etc.

But I also want to filter by the event - I’m only interested in the unsubbing event for the purposes of this call.

I’ve tried a bunch of variations of: https://a.klaviyo.com/api/events?filter=equals(profile_id,'PROFILE_ID_HERE'),equals(metric_id, 'X9cV7Q') where ‘X9cV7Q’ is the metric ID for unsubscribing. I’ve tried that with and() around the two filters. I’ve tried filter=equals() twice separated with &.

However I always get an ‘Invalid filter provided’ error: {"errors":[{"id":"3dc44d8c-e241-4090-9d85-643184919ba9","status":400,"code":"invalid","title":"Invalid input.","detail":"Invalid filter provided. Verify your parentheses match.","source":{"parameter":"filter"},"links":{},"meta":{}}]}

Anyone with a practised eye able to see where I’m going splat?

Many thanks,

Gareth

Best answer by dartacus

So, the answer:

  1. you must use a valid metric_id, check your klaviyo dashboard to get this, it’s part of the URL when viewing a metric
  2. You cannot have an any() filter for metric_id, you can only use equals()
  3. therefore, you’ll have to iterate over any metric IDs you want to test for in your own code, issue a single API call per metric ID, and aggregate the results yourself
  4. the working filter format is this: https://a.klaviyo.com/api/events?include=metric&filter=equals(profile_id,'PROFILE_ID_HERE'),equals(metric_id,'RFe5Su')
View original
Did this topic or the replies in the thread help you find an answer to your question?

4 replies

Forum|alt.badge.img+5
  • Author
  • Problem Solver III
  • 58 replies
  • June 14, 2024

‘X9cV7Q’ turned out to be an invalid metric ID for my test account, but even accounting for that this updated API call:

https://a.klaviyo.com/api/events?include=metric&filter=equals(profile_id,'PROFILE_ID_HERE'),any(metric_id,['RFe5Su', 'SeUc96'])

still gives an invalid filter provided error


Forum|alt.badge.img+5
  • Author
  • Problem Solver III
  • 58 replies
  • June 14, 2024

https://a.klaviyo.com/api/events?include=metric&filter=equals(profile_id,'PROFILE_ID_HERE'),equals(metric_id,'RFe5Su') works, though.

OK, issue found:

{"errors":[{"id":"f87eba95-26f4-4238-9af2-3f9fd0f06ee3","status":400,"code":"invalid","title":"Invalid input.","detail":"'any' is not an allowed filter operator for metric_id.  Allowed operators are equals","source":{"parameter":"filter"},"links":{},"meta":{}}]}

I was only able to get this helpful error message when I tried a single-item array in the any() filter: https://a.klaviyo.com/api/events?include=metric&filter=and(equals(profile_id,'PROFILE_ID_HERE'),any(metric_id,['RFe5Su']))

Then it gives a specific error message.


Forum|alt.badge.img+5
  • Author
  • Problem Solver III
  • 58 replies
  • Answer
  • June 14, 2024

So, the answer:

  1. you must use a valid metric_id, check your klaviyo dashboard to get this, it’s part of the URL when viewing a metric
  2. You cannot have an any() filter for metric_id, you can only use equals()
  3. therefore, you’ll have to iterate over any metric IDs you want to test for in your own code, issue a single API call per metric ID, and aggregate the results yourself
  4. the working filter format is this: https://a.klaviyo.com/api/events?include=metric&filter=equals(profile_id,'PROFILE_ID_HERE'),equals(metric_id,'RFe5Su')

Kim Strauch
Klaviyo Employee
Forum|alt.badge.img+9
  • Klaviyo Employee
  • 91 replies
  • June 14, 2024

Glad you were able to solve this, @dartacus

The docs for each endpoint list out which filter operators are supported on each field. As you noted, only the equals operator is supported for the metric_id and profile_id fields for this endpoint. 

 

Also, metric_ids used must be valid for the account. A given metric, e.g. “Opened Email”, will have a unique metric_id for each Klaviyo account. Metric IDs can be retrieved via the Get Metrics API or are visible in the Klaviyo UI.