Skip to main content
Problem Solver I
December 19, 2023
Solved

Unable to patch/update the profile data

  • December 19, 2023
  • 3 replies
  • 428 views

Hi

I have run into an issue, when trying to update the profile thru the api PATCH (https://developers.klaviyo.com/en/reference/update_profile

 

I am trying to add some custom attributes on the profile, the data i send is build like this

 

$checkKlaviyo = json_decode($system->Klaviyo($env['klaviyo_api'], 'GET', $env['klaviyo_url'] . 'profiles/?filter=equals(email,"'.urlencode($orderData['recipient-email']).'")', false, false));if ( $checkKlaviyo->code == 200 && $checkKlaviyo->status == 'success' ) {    $profileID = $checkKlaviyo->data[0]->id;    $packageData = [        'body' => [            'data' => [                'type' => 'profile',                'id' => $profileID,                'attributes' => [                    'properties' => [                        'PackageAmount' => $orderData['amount'],                        'Packages' => $orderData['packages'] // an Array                    ]                ]            ]        ]    ];    $system->Klaviyo($env['klaviyo_api'], 'PATCH', $env['klaviyo_url'] . 'profiles/'.$profileID.'/', false, json_encode($packageData));}

 

the function 

 

public function Klaviyo($appId, $method, $url, $data, $profileData){    $curl = curl_init();    switch ($method){        case "POST":            curl_setopt($curl, CURLOPT_POST, 1);            if ($data)                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);            break;        case "PUT":            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");            if ($data)                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);            break;        case "PATCH":            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');            if ($profileData)                curl_setopt($curl, CURLOPT_POSTFIELDS, $profileData);            break;        default:            if ($data)                $url = sprintf("%s?%s", $url, http_build_query($data));    }    // OPTIONS:    curl_setopt($curl, CURLOPT_URL, $url);    if ( $method == 'PATCH' ) {        curl_setopt($curl, CURLOPT_HTTPHEADER, array(            'Authorization: Klaviyo-API-Key ' . $appId,            'accept: application/json',            'revision: 2023-12-15'        ));    } else {        curl_setopt($curl, CURLOPT_HTTPHEADER, array(            'Authorization: Klaviyo-API-Key ' . $appId,            'accept: application/json',            'revision: 2023-12-15'        ));    }    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);    // EXECUTE:    $result = curl_exec($curl);    curl_close($curl);    if(!$result) {        $content = [];        $content['code'] = '000';        $content['status'] = 'error';        $content['message'] = 'Connection Failure';        $content['data'] = '';    } else {        $result = json_decode($result, true);        if ( !empty($result['data']) ) {            $content = [];            $content['code'] = '200';            $content['status'] = 'success';            $content['message'] = 'Connection Accepted';            $content['data'] = $result['data'];        } else {            $content = [];            $content['code'] = '404';            $content['status'] = 'error';            $content['message'] = 'Could not fetch data';            $content['data'] = '';        }    }    return json_encode($content);}


but i recieve the following error
string(195) "{"errors":[{"id":"ac432953-de83-4bfa-95cb-2b0d1cc51aa4","status":400,"code":"invalid","title":"Invalid input.","detail":"An object with data is required","source":{"pointer":"/data"},"meta":{}}]}"


anyone who can point me on how to get my custom data over in Klaviyo? must be the dataset i create at

$packageData = [ 'body' => [ 'data' => [ 'type' => 'profile', 'id' => $profileID, 'attributes' => [ 'properties' => [ 'PackageAmount' => $orderData['amount'], 'Packages' => $orderData['packages'] // an Array ] ] ] ] ];

Due to the API can find the ID to update

    This topic has been closed for replies.
    Best answer by KeviSunshine

    Hi @RetouchedDK

    Do you need to specify the wrapping `body` parameter here? That should be automatically inserted by the cURL request (I would think...)

    3 replies

    KeviSunshine
    Expert Problem Solver III
    Expert Problem Solver III
    December 19, 2023

    Hi @RetouchedDK,

    I can’t tell exactly what the issue is, but I am almost positive the data structure you’re passing is incorrect. 

    Check out this thread where a similar issue was encountered: 

     

    {
    "data": {
    "profile": {
    "data": {
    "type": "profile",
    "attributes": {
    "email": "matt@emails.com",
    "properties": {
    "Existing Property": 500,
    "Totally New Property": "some_value"
    }
    }
    }
    }
    }
    }
    }

    If you review your exact structure and compare it to the working examples, I think you’ll find the problem.

     

    Cheers,

    Kevin.

    Problem Solver I
    December 19, 2023

    @KeviSunshine - well yeah i am pretty sure its the dataset i send, that causes the error…

     

    $packageData = [
    'body' => [
    'data' => [
    'type' => 'profile',
    'id' => $profileID,
    'attributes' => [
    'properties' => [
    'PackageAmount' => $orderData['amount'],
    'Packages' => $orderData['packages']
    ]
    ]
    ]
    ]
    ];

     

    and i have added my properties inside the attributes

    KeviSunshine
    Expert Problem Solver III
    Expert Problem Solver III
    December 19, 2023

    Hi @RetouchedDK

    Do you need to specify the wrapping `body` parameter here? That should be automatically inserted by the cURL request (I would think...)