Skip to main content

Hello, I successfully made an API call from Postman using my API key to the campaign API. However if I try the same with Python I get the following error. What am I missing please?

{"errors"::{"id":"b2bd6ac7-312f-457a-99db-e740f73db593","status":401,"code":"not_authenticated","title":"Authentication credentials were not provided.","detail":"Missing or invalid authorization scheme. Please use Klaviyo-API-Key.","source":{"pointer":"/data/"}}]}

 

Python Code

import requests

url = "https://a.klaviyo.com/api/campaigns?filter=equals(messages.channel,'email')"

 

headers = {

  'Authorization': 'Klaviyo-API-Key pk_………….',

  'revision': '2023-12-15'

}

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

 

Hi @amitxenai,

You have the correct header for the Klaviyo API key, but the error is definitely indicating that it’s not being picked up correctly. How’re you making this API call? Can you share the Python code responsible for setting the header? There must be something there…

Let us know.

Best,

Kevin.


Thanks Kevin. Python code is here:

 

import requests

url = "https://a.klaviyo.com/api/campaigns?filter=equals(messages.channel,'email')"

headers = {

  'Authorization': 'Klaviyo-API-Key pk_………….',

  'revision': '2023-12-15'

}

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


Hmm, this looks exactly like the example code…


url = "https://a.klaviyo.com/api/campaigns/"

headers = {
"accept": "application/json",
"revision": "2024-07-15",
"Authorization": "Klaviyo-API-Key your-private-api-key"
}

response = requests.get(url, headers=headers)

Perhaps try the latest revision, 2024-07-15? And set the `accept` header too just to match the example?


Thanks Kevin.

 

The following code works fine (shown below). The problem - I had an extra character in the key (my bad).

One addition thing learnt regarding the revision parameter is that 2023-12-15 is the stable API version. Hence I am using that for now.  Documentation is provided here.

 

Your help is much appreciated. 

 

url = "https://a.klaviyo.com/api/campaigns?filter=equals(messages.channel,'email')"

headers = {

  'Authorization': 'Klaviyo-API-Key pk_...',

  'accept': "application/json",

  'revision': '2023-12-15'

}

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


No problem! Glad you got it sorted!


Reply