0

Create or Update Profile

by
Published Apr 8, 2025

Given a set of profile attributes and optionally an ID, create or update a profile. Returns 201 if a new profile was created, 200 if an existing profile was updated. 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
 * Create or Update Profile
7
 * Given a set of profile attributes and optionally an ID, create or update a profile.
8

9
Returns 201 if a new profile was created, 200 if an existing profile was updated.
10

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

13
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`
14

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