0

Merge Profiles

by
Published Apr 8, 2025

Merge a given related profile into a profile with the given profile ID.

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Merge Profiles
7
 * Merge a given related profile into a profile with the given profile ID.
8
 */
9
export async function main(
10
  auth: Klaviyo,
11
  revision: string,
12
  body: {
13
    data: {
14
      type: "profile-merge";
15
      id: string;
16
      relationships?: {
17
        profiles?: { data?: { type: "profile"; id: string }[] };
18
      };
19
    };
20
  },
21
) {
22
  const url = new URL(`https://a.klaviyo.com/api/profile-merge`);
23

24
  const response = await fetch(url, {
25
    method: "POST",
26
    headers: {
27
      revision: revision,
28
      "Accept": "application/vnd.api+json",
29
      "Content-Type": "application/vnd.api+json",
30
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
31
    },
32
    body: JSON.stringify(body),
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40