0

Update Profile

by
Published Apr 8, 2025

Update the profile with the given profile ID. Use the `additional-fields` parameter to include subscriptions and predictive analytics data in your response. Note that setting a field to `null` will clear out the field, whereas not including a field in your request will leave it unchanged.*Rate limits*:Burst: `75/s`Steady: `700/m` **Scopes:** `profiles:write`

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Update Profile
7
 * Update the profile with the given profile ID.
8

9
Use the `additional-fields` parameter to include subscriptions and predictive analytics data in your response.
10

11
Note that setting a field to `null` will clear out the field, whereas not including a field in your request will leave it unchanged.*Rate limits*:Burst: `75/s`Steady: `700/m`
12

13
 */
14
export async function main(
15
  auth: Klaviyo,
16
  id: string,
17
  additional_fields_profile_: string | undefined,
18
  revision: string,
19
  body: {
20
    data: {
21
      type: "profile";
22
      id: string;
23
      attributes: {
24
        email?: string;
25
        phone_number?: string;
26
        external_id?: string;
27
        anonymous_id?: string;
28
        first_name?: string;
29
        last_name?: string;
30
        organization?: string;
31
        locale?: string;
32
        title?: string;
33
        image?: string;
34
        location?: {
35
          address1?: string;
36
          address2?: string;
37
          city?: string;
38
          country?: string;
39
          latitude?: string | number;
40
          longitude?: string | number;
41
          region?: string;
42
          zip?: string;
43
          timezone?: string;
44
          ip?: string;
45
        };
46
        properties?: {};
47
      };
48
      meta?: {
49
        patch_properties?: {
50
          append?: {};
51
          unappend?: {};
52
          unset?: string | string[];
53
        };
54
      };
55
    };
56
  },
57
) {
58
  const url = new URL(`https://a.klaviyo.com/api/profiles/${id}`);
59
  for (const [k, v] of [
60
    ["additional-fields[profile]", additional_fields_profile_],
61
  ]) {
62
    if (v !== undefined && v !== "" && k !== undefined) {
63
      url.searchParams.append(k, v);
64
    }
65
  }
66
  const response = await fetch(url, {
67
    method: "PATCH",
68
    headers: {
69
      revision: revision,
70
      "Accept": "application/vnd.api+json",
71
      "Content-Type": "application/vnd.api+json",
72
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
73
    },
74
    body: JSON.stringify(body),
75
  });
76
  if (!response.ok) {
77
    const text = await response.text();
78
    throw new Error(`${response.status} ${text}`);
79
  }
80
  return await response.json();
81
}
82