Skip to main content
Problem Solver I
September 30, 2024
Solved

First time API call from Python

  • September 30, 2024
  • 5 replies
  • 100 views

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)

 

    This topic has been closed for replies.
    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)

    5 replies

    KeviSunshine
    Expert Problem Solver III
    Expert Problem Solver III
    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.

    amitxenaiAuthor
    Problem Solver I
    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
    Expert Problem Solver III
    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?

    amitxenaiAuthorAnswer
    Problem Solver I
    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
    Expert Problem Solver III
    September 30, 2024

    No problem! Glad you got it sorted!