Skip to main content
Solved

Updating profiles based on an email


Forum|alt.badge.img+2
  • Problem Solver III
  • 21 replies

Hi

I am trying to update my name in a profile in klaviyo (testing). When I run the php code in the browser it produces the following error:

{"errors":[{"id":"07caca98-4257-473c-8284-a79947dfa253","status":400,"code":"invalid","title":"Invalid input.","detail":"An object with data is required","source":{"pointer":"/data"},"links":{},"meta":{}}]}

 

Below is the file containing the code:

 

Text file containing the php code

 

Just one question: Do I need to create an event?

 

Thanks.

Tudsy

Best answer by michaela.fooksa

Hi Tudsy, 

The error you are running into indicates that your Klaviyo private API key was not properly included in the API call. Please make sure your Authorization header is: ‘Authorization’ => ‘Klaviyo-API-Key privatekey’ where “privatekey” is your private API key.

This guide provides detailed information on how to create a private API key. Make your private key has write access to profiles. You can also refer to this guide for reference on authenticating your server-side API calls. 

 

If you are still running into any errors, please provide your exact API payload and error message, making sure to remove your private API key for security reasons.

Finally, in response to your initial question, you do not need to create an event, and you are now using the correct endpoint to update your name in Klaviyo!

Michaela

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

9 replies

Forum|alt.badge.img+2
  • Author
  • Problem Solver III
  • 21 replies
  • June 30, 2024

Hi

I am now using Create or Update profile api to update my first name (testing) from Adrian to Adriano based on an email.

 

Here is the curl version of the api:

 

curl --request POST \
     --url https://a.klaviyo.com/api/profile-import/ \
     --header 'Authorization: Klaviyo-API-Key your-private-api-key' \
     --header 'accept: application/json' \
     --header 'content-type: application/json' \
     --header 'revision: 2024-06-15' \
     --data '
{
  "data": {
    "type": "profile",
    "attributes": {
      "email": "sarah.mason@klaviyo-demo.com",
      "phone_number": "+15005550006",
      "anonymous_id": "01GDDKASAP8TKDDA2GRZDSVP4H",
      "first_name": "Sarah",
      "last_name": "Mason",
      "organization": "Example Corporation",
      "title": "Regional Manager",
      "image": "https://images.pexels.com/photos/3760854/pexels-photo-3760854.jpeg",
      "location": {
        "address1": "89 E 42nd St",
        "address2": "1st floor",
        "city": "New York",
        "country": "United States",
        "region": "NY",
        "zip": "10017",
        "timezone": "America/New_York",
        "ip": "127.0.0.1"
      },
      "properties": {
        "newKey": "New Value"
      }
    },
    "meta": {
      "patch_properties": {
        "append": {
          "newKey": "New Value"
        },
        "unappend": {
          "newKey": "New Value"
        },
        "unset": "skus"
      }
    }
  }
}
'

 

When I execute the php file, I get this error:

{"errors":[{"id":"cdf328c6-a0f8-4525-9e28-47bd3ef8ebdf","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/"}}]}

 

Any help would be greatly appreciated.

Thanks.

Tudsy


michaela.fooksa
Community Manager
Forum|alt.badge.img+6

Hi Tudsy, 

The error you are running into indicates that your Klaviyo private API key was not properly included in the API call. Please make sure your Authorization header is: ‘Authorization’ => ‘Klaviyo-API-Key privatekey’ where “privatekey” is your private API key.

This guide provides detailed information on how to create a private API key. Make your private key has write access to profiles. You can also refer to this guide for reference on authenticating your server-side API calls. 

 

If you are still running into any errors, please provide your exact API payload and error message, making sure to remove your private API key for security reasons.

Finally, in response to your initial question, you do not need to create an event, and you are now using the correct endpoint to update your name in Klaviyo!

Michaela


Forum|alt.badge.img+2
  • Author
  • Problem Solver III
  • 21 replies
  • July 2, 2024

Thanks.


Forum|alt.badge.img+2
  • Author
  • Problem Solver III
  • 21 replies
  • July 4, 2024

Hi

 

Its working now but I am waiting for my shared IP to be whitelisted. Its still not updating the first name in Klaviyo.

 

Thanks.


Forum|alt.badge.img+2
  • Author
  • Problem Solver III
  • 21 replies
  • July 15, 2024

Hi

I am trying to update my first name (testing) by id using the create and update api.

When i run the script, i get this error:

 

Array ( [errors] => Array ( [0] => Array ( [id] => 27833ea4-7314-418c-bed9-915bc75dc595 [status] => 400 [code] => invalid [title] => Invalid input. [detail] => An object with data is required [source] => Array ( [pointer] => /data ) [links] => Array ( ) [meta] => Array ( ) ) ) )

 

 

 

Here is the code:

<?php

/* Using Update profile to update the first_name based on a profile id- Testing */

$id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;

$email ='ecovib2d@live.com';

$data = array();
 

  $data = [
      
    "type" => "profile",
    "id" => $id,
    
    "attributes" => [
        
       
      "email" => $email,
      "first_name" => "Adriano",
      "last_name" => "Tudini",  
      
   ],
 ];


        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://a.klaviyo.com/api/profile-import/',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($data),
        CURLOPT_HTTPHEADER => array(
         'Authorization: Klaviyo-API-Key pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',  
         'accept: application/json' ,
         'content-type: application/json',
         'revision: 2024-06-15',
         
        ),
    ));

$updated_or_written_profile = curl_exec($curl);

print_r(json_decode($updated_or_written_profile,true));

if ($updated_or_written_profile === false) {
    $error = curl_error($curl);
    curl_close($curl);
    die('Curl error: ' . $error);
}
    
curl_close($curl);

    
?>    
    
 Any help is appreciated.

 

Thanks,   

     


Kim Strauch
Klaviyo Employee
Forum|alt.badge.img+9
  • Klaviyo Employee
  • 91 replies
  • July 15, 2024

@Tudsy, the data payload needs to be nested under an object with the key “data”. It looks like your payload may not be nested under a top-level “data” key.  

Also, if you’re only trying to update the first_name / last_name (not email), you can just include those keys. The Update Profile API is another option here as well 
 


Forum|alt.badge.img+2
  • Author
  • Problem Solver III
  • 21 replies
  • July 16, 2024

Hi

 

Thanks for that.

 

I am now using the update profile api. I now get the same error as before using the create and update profile api.

 

Error:

string(206) "{"errors":[{"id":"562ff8df-044a-4363-a5e7-eda14860015c","status":400,"code":"invalid","title":"Invalid input.","detail":"An object with data is required","source":{"pointer":"/data"},"links":{},"meta":{}}]}"

 

Here is the code:

 

<?php

/* Using Update profile to update the first_name based on a profile id which is based on an email - Testing */


$email ='ecovib2d@live.com';

$data_new = array(); 
 
$curl = curl_init(); 
curl_setopt_array($curl, array( 
CURLOPT_URL => "https://a.klaviyo.com/api/profiles?filter=equals(email,%22$email%22)", 
CURLOPT_RETURNTRANSFER => true, 
CURLOPT_CUSTOMREQUEST => "GET", 
CURLOPT_HTTPHEADER => array( 
    
'Authorization: Klaviyo-API-Key pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
'accept: application/json', 
'revision: 2024-07-15',
),
));

$response = curl_exec($curl);


if ($response === false) {
    $error = curl_error($curl);
    curl_close($curl);
    die('Curl error: ' . $error);
}

curl_close($curl);

$userDetails = json_decode($response, true);
$id = $userDetails['data'][0]['id'];

$data_new = [
    "type"=> "profile",
    "id" => $userDetails['data'][0]['id'],
    "attributes"=> [
      "email" =>$email,
      "first_name"=> "Adriano",
      "last_name"=> "Tudini",
      "title" => "Self-Employed",
      "properties"=> [
        "newKey"=> "New Value"
      ],
    ],
    "meta"=> [
      "patch_properties"=> [
        "append"=> [
          "newKey"=> "New Value"
        ],
        "unappend"=> [
          "newKey"=> "New Value"
        ],
        "unset"=> "skus"
      ],
    ],
  ];

        

        $curli = curl_init();
        curl_setopt_array($curli, array(
        CURLOPT_URL => "https://a.klaviyo.com/api/profiles/".$id."/",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => "PATCH",
        CURLOPT_POSTFIELDS => json_encode($data_new),
        CURLOPT_HTTPHEADER => array(
         'Authorization: Klaviyo-API-Key pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',  
         'accept: application/json' ,
         'content-type: application/json',
         'revision: 2024-07-15',
         
        ),
    ));

$updated_or_written_profile = curl_exec($curli);

var_dump($updated_or_written_profile);

if ($updated_or_written_profile === false) {
    $error = curl_error($curli);
    curl_close($curli);
    die('Curl error: ' . $error);
}

    
  curl_close($curli);  

    
?>    
    
    

     Thanks.


Kim Strauch
Klaviyo Employee
Forum|alt.badge.img+9
  • Klaviyo Employee
  • 91 replies
  • July 16, 2024

@Tudsy, As I mentioned above you’re not nesting the data properly in your request under a “data” key. 

Updating your code above and removing some extraneous fields you don’t need, if your goal is only to update the first_name and last_name here. 
 

<?php

/* Using Update profile to update the first_name based on a profile id which is based on an email - Testing */


$email ='ecovib2d@live.com';

$data_new = array(); 
 
$curl = curl_init(); 
curl_setopt_array($curl, array( 
CURLOPT_URL => "https://a.klaviyo.com/api/profiles?filter=equals(email,%22$email%22)", 
CURLOPT_RETURNTRANSFER => true, 
CURLOPT_CUSTOMREQUEST => "GET", 
CURLOPT_HTTPHEADER => array( 
    
'Authorization: Klaviyo-API-Key pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx''accept: application/json''revision: 2024-07-15',
),
));

$response = curl_exec($curl);


if ($response === false) {
    $error = curl_error($curl);
    curl_close($curl);
    die('Curl error: ' . $error);
}

curl_close($curl);

$userDetails = json_decode($response, true);
$id = $userDetails['data'][0]['id'];

$data_new = [
    "data" => [
        "type"=> "profile",
        "id" => $userDetails['data'][0]['id'],
        "attributes"=> [
           "first_name"=> "Adriano",
           "last_name"=> "Tudini",
        ]
    ]  
];

        

        $curli = curl_init();
        curl_setopt_array($curli, array(
        CURLOPT_URL => "https://a.klaviyo.com/api/profiles/".$id."/",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => "PATCH",
        CURLOPT_POSTFIELDS => json_encode($data_new),
        CURLOPT_HTTPHEADER => array(
         'Authorization: Klaviyo-API-Key pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',  
         'accept: application/json' ,
         'content-type: application/json',
         'revision: 2024-07-15',
         
        ),
    ));

$updated_or_written_profile = curl_exec($curli);

var_dump($updated_or_written_profile);

if ($updated_or_written_profile === false) {
    $error = curl_error($curli);
    curl_close($curli);
    die('Curl error: ' . $error);
}

    
  curl_close($curli);  

    
?>    

 


Forum|alt.badge.img+2
  • Author
  • Problem Solver III
  • 21 replies
  • July 17, 2024

Hi

Thanks. The problem is now solved.

 

Adrian (Tudsy).


Reply