Skip to main content
Solved

Updating the email address via an api

  • August 13, 2024
  • 9 replies
  • 155 views

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

Hi

Just one question. How do i update a profiles email address via an api?

 

Thanks.

 

Adrian

Best answer by Tudsy

Thanks for that.

 

What if I don’t have the profile ID?

 

Adrian

9 replies

KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 160 replies
  • August 13, 2024

Hi @Tudsy,

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": []
}
}
}
}

Cheers,

Kevin.


Forum|alt.badge.img+3
  • Author
  • Problem Solver III
  • 21 replies
  • Answer
  • August 13, 2024

Thanks for that.

 

What if I don’t have the profile ID?

 

Adrian


DavidSandel
Partner - Silver
Forum|alt.badge.img+21
  • Partner - Silver
  • 157 replies
  • August 13, 2024

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. 


KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 160 replies
  • August 13, 2024

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.


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

    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 = [
    "data" => [
        "type"=> "profile",
        "attributes"=> [
            "properties"=> [
                "email" => $email,
                "first_name" => $firstname,
                "last_name"  => $surname,
                "phone_number" => "xxxxxxxxxxxxxxxxxxxxxxxxxx", 
                "id" => $id,
            ]
        ],
        "meta" => [
      "patch_properties" => [
        "unset"=> [
          
        ],
        
      ]
   
   ]
   ]
];

 


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


KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 160 replies
  • August 15, 2024

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.


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

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"][0]["id"]; 
}


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


return $id;
    
curl_close($curlg);  
    
    
    
}    
    

 Thanks.

Tudsy


KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 160 replies
  • August 19, 2024

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.


Forum|alt.badge.img+3
  • Author
  • Problem Solver III
  • 21 replies
  • August 20, 2024

Hi

 

Solved it.

 

You had to urlencode the id.

 

Thanks.