Skip to main content
Solved

First time API call from Python

  • September 30, 2024
  • 5 replies
  • 31 views

Forum|alt.badge.img

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)

 

Best answer by amitxenai

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)

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

5 replies

KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 158 replies
  • September 30, 2024

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.


Forum|alt.badge.img
  • Author
  • Problem Solver I
  • 3 replies
  • September 30, 2024

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)


KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 158 replies
  • September 30, 2024

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?


Forum|alt.badge.img
  • Author
  • Problem Solver I
  • 3 replies
  • Answer
  • September 30, 2024

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)


KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 158 replies
  • September 30, 2024

No problem! Glad you got it sorted!