Skip to main content
Solved

Strange empty response from api query

  • December 29, 2023
  • 1 reply
  • 177 views

Forum|alt.badge.img+5

Hello, I am very confused on why I am getting a weird empty response using the query metric aggregates endpoint.
 

import requests


api_key = "MY_API_KEY"
metric_id = "R3rQ9W"
page_size = 500
interval = "month"
start_date = "2022-12-30T00:00:00"
end_date = "2023-12-29T23:59:59"
measurement = "count"
by = "$message_send_cohort"


url = "https://a.klaviyo.com/api/metric-aggregates/"

payload = {
    "data": {
        "type": "metric-aggregate",
        "attributes": {
            "metric_id": metric_id,
            "interval": interval,
            "page_size": page_size,
            "timezone": "US/Eastern",
            "measurements": [measurement],
            "filter": [
                f"greater-or-equal(datetime,{start_date})",
                f"less-than(datetime,{end_date})",
            ],
            "by": [by],
        },
    }
}

headers = {
    "accept": "application/json",
    "revision": "2023-02-22",
    "content-type": "application/json",
    "Authorization": f"Klaviyo-API-Key {api_key}",
}

response = requests.post(url, json=payload, headers=headers)

response_json = response.json()
results_data = response_json["data"]

 

This is returning a response with no data:

 

{'type': 'metric-aggregate',
 'id': '-6647815704978276286',
 'attributes': {'dates': ['2022-12-01T05:00:00+00:00',
   '2023-01-01T05:00:00+00:00',
   '2023-02-01T05:00:00+00:00',
   '2023-03-01T05:00:00+00:00',
   '2023-04-01T04:00:00+00:00',
   '2023-05-01T04:00:00+00:00',
   '2023-06-01T04:00:00+00:00',
   '2023-07-01T04:00:00+00:00',
   '2023-08-01T04:00:00+00:00',
   '2023-09-01T04:00:00+00:00',
   '2023-10-01T04:00:00+00:00',
   '2023-11-01T04:00:00+00:00',
   '2023-12-01T05:00:00+00:00'],
  'data': []},
 'links': {'self': 'https://a.klaviyo.com/api/metric-aggregates/'}}

 

Strangely, if I change the time zone to US/Pacific, or if I change the metric, it works fine. If I change month to week instead, it works fine. Some date changes work, but not all. If I change the by to $message it works. I am a bit lost to how this could happen. It seems like somehow this is the perfect set of parameters that doesn’t work lol. Does anyone have any idea? The metric is received email. 

Best answer by saulblum

Could you try it without the by parameter in the request body, to see if you get the raw Received Email counts you see in the metrics page in the Klaviyo admin?

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

1 reply

Forum|alt.badge.img+7
  • Klaviyo Employee
  • 168 replies
  • Answer
  • December 29, 2023

Could you try it without the by parameter in the request body, to see if you get the raw Received Email counts you see in the metrics page in the Klaviyo admin?