0

Create Profile

by
Published Apr 8, 2025

Create a new profile. Use the `additional-fields` parameter to include subscriptions and predictive analytics data in your response.*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 Profile
7
 * Create a new profile.
8

9
Use the `additional-fields` parameter to include subscriptions and predictive analytics data in your response.*Rate limits*:Burst: `75/s`Steady: `700/m`
10

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