Skip to main content
Solved

writing to a list


Forum|alt.badge.img+2

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);
    
    


?>

Best answer by Tudsy

Hi

 

Problem solved.

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

5 replies

Forum|alt.badge.img+1
  • Klaviyo Employee
  • 10 replies
  • 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


Forum|alt.badge.img+31
  • Partner
  • 252 replies
  • July 14, 2024

Hello @Tudsy 

 

Try replacing

 "Authorization: Bearer $apiKey",

with

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


Forum|alt.badge.img+2
  • Author
  • Contributor I
  • 0 replies
  • 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);

?>


Forum|alt.badge.img+2
  • Problem Solver III
  • 10 replies
  • 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


Forum|alt.badge.img+2
  • Author
  • Contributor I
  • 0 replies
  • Answer
  • July 17, 2024

Hi

 

Problem solved.