klaviyo-api-node icon indicating copy to clipboard operation
klaviyo-api-node copied to clipboard

Calling `profilesApi.updateProfile` returns a `409` error `duplicate_profile`

Open clockelliptic opened this issue 1 year ago • 1 comments

I'm working in typescript.

  1. Attempting to update a profile returns a duplicate profile error.
  2. The updateProfile method takes profileId as 2 different arguments for no good reason; enforced by typescript.
import {
  ApiKeySession,
  ProfilesApi,
  ProfilePartialUpdateQuery,
  ProfilePartialUpdateQueryResourceObjectAttributes,
} from 'klaviyo-api';

const session = new ApiKeySession(process.env.KLAVIYO_PRIVATE_TOKEN);
const profilesApi = new ProfilesApi(session);

const profileAttributes: ProfilePartialUpdateQueryResourceObjectAttributes =  {
    email: '[email protected]',
    firstName: 'test',
    lastName: 'user',
    phoneNumber: '+15551230987'
};

const profileResponse = await profilesApi.getProfiles({
  filter: `equals(email,"${profileAttributes.email}")`,
});
const profileId = profileResponse.body.data[0].id;

if (!profileId) {
  throw new Error('This profile doesn't exist.');
}

const partialUpdateQuery: ProfilePartialUpdateQuery = {
  data: {
    id: profileId, // why is this required if the updateProfile method takes profile ID as an argument?
    type: 'profile',
    attributes: profileAttributes,
  },
};
await profilesApi.updateProfile(profileId, partialUpdateQuery);

this is the error that's returned:

{
  "errors": [
    {
      "id": "853cbd75-0a3b-40ab-a13f-d729ff826844",
      "status": 409,
      "code": "duplicate_profile",
      "title": "Conflict.",
      "detail": "A profile already exists with one of these identifiers.",
      "source": {
        "pointer": "/data/attributes"
      },
      "links": {},
      "meta": {
        "duplicate_profile_id": "< profile id here >"
      }
    }
  ]
}

clockelliptic avatar Jul 21 '24 02:07 clockelliptic

E-mail and phone numbers for profiles are unique properties, so the API won't allow you to update a profile with a value that already exists. Could you double check that the values you're passing for those two fields are unique values? I believe the same applies for your other open issue here.

nat-klaviyo avatar Jul 23 '24 15:07 nat-klaviyo