Skip to main content
July 13, 2024
Solved

writing to a list

  • July 13, 2024
  • 5 replies
  • 160 views

Hi

I am trying to add profiles to a list using the Add Profiles to List api. I get the following error :

Array ( [errors] => Array ( [0] => Array ( [id] => fc86f1de-4e38-4922-b52c-4756939fc34f [status] => 401 [code] => not_authenticated [title] => Authentication credentials were not provided. [detail] => Missing or invalid access token. [source] => Array ( [pointer] => /data/ ) ) ) )

 

Here is the code:

 

<?php


/* Testing */


$data = array();

  
  
  
/* Add Profile to List - ECOVIB2Ds Master List */   
 
$email_add = "ecovib2d@live.com";
$apiKey="pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$id = "xxxxxxxx";
$first_name = "Adrian";
$last_name = "Tudini";

 $data =  [
    [
      "type" => "profile",
      "api_key" => $apiKey,
      "id" => $id,
      "profiles"  => array ('0' => array('first_name' => $first_name,'last_name' => $last_name,'email' => $email_add))
    ]
]; 

$curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://a.klaviyo.com/api/lists/xxxxxx/relationships/profiles/?api_key=".$apiKey,         
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($data),
        CURLOPT_HTTPHEADER => array(
            
            "Authorization: Bearer $apiKey",
            "accept: application/json",
            "content-type: application/json",
            "revision: 2024-06-15",
        ),
    ));
    
    $response = curl_exec($curl);
    
    
    
    $response_details = json_decode($response,true);
    
    print_r($response_details);
    
    
    if ($response === false) {
    $error = curl_error($curl);
    curl_close($curl);
    die('Curl error: ' . $error);
}
    
    
    
    curl_close($curl);
    
    


?>

    This topic has been closed for replies.
    Best answer by

    Hi

     

    Problem solved.

    5 replies

    Klaviyo Employee
    July 13, 2024

    Hi Tudsy,

     

    Please review this section of our API docs around authentication: https://developers.klaviyo.com/en/reference/api_overview#private-key-authentication

     

    In particular, the private key is not passed as a query param but as a header using the format shown in that doc

     

    Thanks,

    Jason

    Partner
    July 14, 2024

    Hello @Tudsy 

     

    Try replacing

     "Authorization: Bearer $apiKey",

    with

    “Authorization: Klaviyo-API-Key $apiKey'”

    https://theforms.pro
    July 14, 2024

    HI

     

    I now get this error:

     

    Array ( [errors] => Array ( [0] => Array ( [id] => b6170c7d-126f-4070-a5c7-dba5e9b98218 [status] => 400 [code] => invalid [title] => Invalid input. [detail] => An object with data is required [source] => Array ( [pointer] => / ) [links] => Array ( ) [meta] => Array ( ) ) ) )

     

    Here is the code:

    <?php


    /* Testing */


    $data = array();

      
      
      
    /* Add Profile to List - ECOVIB2Ds Master List */   
     
    $email_add = "ecovib2d@live.com";
    $apiKey="pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $id = "xxxxxxx";
    $first_name = "Adrian";
    $last_name = "Tudini";

     $data =  [
        [
          "type" => "profile",
          "api_key" => $apiKey,
          “id” => $id,
          "attributes" => [
              
              'first_name' => $first_name,
              'last_name' => $last_name,
              'email' => $email_add
              
              ],
        ],
    ]; 

    $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://a.klaviyo.com/api/lists/xxxxxxx/relationships/profiles/",         
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($data),
            CURLOPT_HTTPHEADER => array(
                
                'Authorization: Klaviyo-API-Key pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',            
                'content-type: application/json',
                'accept: application/json',
                'revision: 2024-06-15',
            ),
        ));
        
        $response = curl_exec($curl);
        
        
        
        $response_details = json_decode($response,true);
        
        print_r($response_details);
        
        
        if ($response === false) {
        $error = curl_error($curl);
        curl_close($curl);
        die('Curl error: ' . $error);
    }
        
        
        
        curl_close($curl);

    ?>

    Problem Solver III
    July 17, 2024

    Hi @Tudsy,

    On checking this I understand that you are using the “Add Profile To List” API. In this API we are only able to add already created contacts to the List based on ids. But in the code that is attached I could see first_name last_name and email in the data attributes.

    So if the profile already exists then the id of the profile will be more than sufficient to add it to the List.

    You can refer this screenshot for the same.

     

    And this is the sample payload and the URL:

    URL: POST /api/lists/{id}/relationships/profiles/

    PAYLOAD:
     

    {
    "data": [
    {
    "type": "profile",
    "id": "01J2PMXX280CM2DXKB73PEABSN"
    },
    {
    "type": "profile",
    "id": "01J2PMXX280BH2DXKB73PEABSN"
    }
    ]
    }

     


     

    And if you want to create a profile and Add it to a list you can combine these two APIs and achieve the same:

    Create a Profile: https://developers.klaviyo.com/en/reference/create_profile

    Add Profile to List: https://developers.klaviyo.com/en/reference/create_list_relationships

     

    Please let me know incase you need any other information as well.

     

    Thanks

     

    Best,

    Abhijith

    Answer
    July 17, 2024

    Hi

     

    Problem solved.