Hi
Just one question. How do i update a profiles email address via an api?
Thanks.
Adrian
Hi
Just one question. How do i update a profiles email address via an api?
Thanks.
Adrian
Hi
If you have the user’s profile ID, you can use the `Update Profile` function documented here: https://developers.klaviyo.com/en/reference/update_profile
Here’s an example URL and request body:
URL: https://a.klaviyo.com/api/profiles/<ProfileID>
{
"data": {
"type": "profile",
"id": <ProfileID>,
"attributes": {
"properties": {
"email": <enter the new email here>
}
},
"meta": {
"patch_properties": {
"unset": e]
}
}
}
}
Cheers,
Kevin.
Thanks for that.
What if I don’t have the profile ID?
Adrian
If you don’t have the profile id, first you have to search by their current email address using a GET call.
https://developers.klaviyo.com/en/reference/get_profiles
that will return their id, and you’ll be able to update their profile with the new email.
Hi Adrian,
I am not sure how to do it without the profile ID! I tried to upsert the profile (this endpoint: https://developers.klaviyo.com/en/reference/create_or_update_profile) and remove the email address, and then a second call to update the email address… but my first call did not work.
I think the best thing to do would be to get the profile ID using the email (you can use the getProfiles endpoint, https://developers.klaviyo.com/en/reference/get_profiles, to get a profile by email), and then a second call to update the email using the profile ID.
I am curious if anyone knows a 1-endpoint solution for updating an email without an ID… I can’t find one!
Best,
Kevin.
Hi
I am using the Update profile api in a function below:
function Klaviyo($email,$firstname,$surname,$id){
$data_new = array();
/* Using Update profile api */
$data_new = n
"data" => "
"type"=> "profile",
"attributes"=> t
"properties"=>
"email" => $email,
"first_name" => $firstname,
"last_name" => $surname,
"phone_number" => "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"id" => $id,
]
],
"meta" => d
"patch_properties" =>
"unset"=> m
],
]
]
]
];
$curlg = curl_init();
curl_setopt_array($curlg, 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_xxxxxxxxxxxxxxxxxxxxxxxxxx',
'accept: application/json' ,
'revision: 2024-07-15',
),
));
$updated = curl_exec($curlg);
$details_up = json_encode($updated,true);
if ($updated === false) {
$error = curl_error($curlg);
curl_close($curlg);
die('Curl error: ' . $error);
}
curl_close($curlg);
}
The result is that it is not updating the address in klaviyo.
Can you please help?
Thanks.
Adrian (Tudsy)
Adrian,
It appears you cannot use the upsert API to change someone’s email address.
You need to use the GetProfile API, followed by the UpdateByID function.
Hope that helps.
Cheers,
Kevin.
Hi
Thanks for that.
I call the below function before the previous function to get the id of the profile using Get Profiles api.
function getId($email){ /* get klaviyo id by email address */
global $id;
$email = urlencode($email);
$curlg = curl_init();
curl_setopt_array($curlg, 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_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'accept: application/json' ,
'revision: 2024-07-15',
),
));
$details = curl_exec($curlg);
$details = json_decode($details,true);
if($details){
$id = $details "data"]d0]o"id"];
}
if ($details === false) {
$error = curl_error($curlg);
curl_close($curlg);
die('Curl error: ' . $error);
}
return $id;
curl_close($curlg);
}
Thanks.
Tudsy
Hi Adrian,
Are you still having issues with the update-by-id endpoint? I just reviewed your code and compared to my Postman examples and they look identical… this should be working.
Let me know what’s happening.
Best,
Kevin.
Hi
Solved it.
You had to urlencode the id.
Thanks.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.