Solved

Query Metric Aggregates Bad Request for URL

  • 27 October 2023
  • 7 replies
  • 101 views

Badge +3

I’ve been using the python sdk to query metrics and it was working well up to last month.
This week I have been receiving ‘None” responses. I think tried querying using the requests module as the examples shown in the docs, that returns a  “Bad Request for url: https://a.klaviyo.com/api/metric-aggregates/”.

Between last month and this month, the only thing that changed in my query are the date filters.
What could be the cause of this?

 

import requests
from klaviyo_api import KlaviyoAPI
import connections.keys.klaviyo_keys as klaviyo_keys

payload = { "data": {
"type": "metric-aggregate",
"attributes": {
"metric_id": "HKCzM3",
"measurements": ["count", "sum_value", "unique"],
"interval": "day",
"by": ["$message"],
"filter": ["greater-or-equal(datetime,2023-09-01)",
"less-than(datetime,2023-10-01)"],
"timezone": "America/New_York"
}
}
}


klaviyo = KlaviyoAPI(klaviyo_keys.api_key)
metrics = klaviyo.Metrics.query_metric_aggregates(payload)
print(metrics)

# RETURNS NONE
# ----------------------------------------------------------------
# --------------------------------------------------------------
input()


url = "https://a.klaviyo.com/api/metric-aggregates/"
headers = {
'revision': '2023-10-15',
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Klaviyo-API-Key {klaviyo_keys.api_key}'
}

response = requests.post(url, headers=headers, data=payload)
response.raise_for_status()
print(response.text)


# RETURNS 400 Client Error: Bad Request for url

 

icon

Best answer by Brian Turcotte 9 November 2023, 20:19

View original

7 replies

Userlevel 7
Badge +60

Hey @edwinv 

Thanks for reaching out for help with this. Im going to have one of our experts take a look at this and see if they can figure out what is going on for you. As soon as they have an answer, we will update you here!

Userlevel 7
Badge +36

Hi @edwinv!

Would it be possible to import this same payload into a testing tool like Postman via CurL request, and see if you’re getting the same error/response?

 

This will help us understand if it’s an issue with our SDK or your own data/integration.

 

Best,
Brian

Badge +3

Hi!

I was able to get a response through Postman and this is the below code.

curl --location 'https://a.klaviyo.com/api/metric-aggregates/' \
--header 'revision: 2023-10-15' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Klaviyo-API-Key APIKEY' \
--data '{ "data": {
"type": "metric-aggregate",
"attributes": {
"metric_id": "HKCzM3",
"measurements": ["count", "sum_value", "unique"],
"interval": "day",
"by": ["$message"],
"filter": ["greater-or-equal(datetime,2023-09-01)", "less-than(datetime,2023-10-01)"],
"timezone": "America/New_York"
}
}
}'

While I was at it, I took a look at the Python version and I was missing converting the payload to json.

import requests
import json

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

payload = json.dumps({
"data": {
"type": "metric-aggregate",
"attributes": {
"metric_id": "HKCzM3",
"measurements": [
"count",
"sum_value",
"unique"
],
"interval": "day",
"by": [
"$message"
],
"filter": [
"greater-or-equal(datetime,2023-09-01)",
"less-than(datetime,2023-10-01)"
],
"timezone": "America/New_York"
}
}
})
headers = {
'revision': '2023-10-15',
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Klaviyo-API-Key APIKEY'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

I tested query in my environment using requests and it worked perfectly. Unfortunately the SDK did not work regardless of the payload being converted to json or as a dict.

Userlevel 7
Badge +36

Hi @edwinv!

May I ask if you’d be willing to attempt a request using an older revision than the 2023-10-15 one you are using in the call above? Ideally a revision from the time when the call was working correctly?

This will help us determine if there is an issue with the latest revision for this use case.

 

Also, if that doesn’t work and you’re still struggling with the issue, it may be advantageous to post this as a Github issue here:

 

Best,

Brian

Badge +3

Hi Brian!

I ended up switching to using the requests module for the time being. If I get a chance I’ll test out an older version and post on the Github if necessary. Thank you so much for your help!

Best regards, 

Edwin 

Badge +1

Hey,


We just released a fix for this, in the latest version of our python sdk, v6.0.1.

Apologies for the delay in getting this fix out.

 

Cheers!
Jon

Badge +3

Hi,

 

No problem at all, thank you so much! 

Reply