0

Get Profiles

by
Published Apr 8, 2025

Get all profiles in an account.

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Get Profiles
7
 * Get all profiles in an account.
8
 */
9
export async function main(
10
  auth: Klaviyo,
11
  additional_fields_profile_: string | undefined,
12
  fields_profile_: string | undefined,
13
  filter: string | undefined,
14
  page_cursor_: string | undefined,
15
  page_size_: string | undefined,
16
  sort:
17
    | "created"
18
    | "-created"
19
    | "email"
20
    | "-email"
21
    | "id"
22
    | "-id"
23
    | "subscriptions.email.marketing.list_suppressions.timestamp"
24
    | "-subscriptions.email.marketing.list_suppressions.timestamp"
25
    | "subscriptions.email.marketing.suppression.timestamp"
26
    | "-subscriptions.email.marketing.suppression.timestamp"
27
    | "updated"
28
    | "-updated"
29
    | undefined,
30
  revision: string,
31
) {
32
  const url = new URL(`https://a.klaviyo.com/api/profiles`);
33
  for (const [k, v] of [
34
    ["additional-fields[profile]", additional_fields_profile_],
35
    ["fields[profile]", fields_profile_],
36
    ["filter", filter],
37
    ["page[cursor]", page_cursor_],
38
    ["page[size]", page_size_],
39
    ["sort", sort],
40
  ]) {
41
    if (v !== undefined && v !== "" && k !== undefined) {
42
      url.searchParams.append(k, v);
43
    }
44
  }
45
  const response = await fetch(url, {
46
    method: "GET",
47
    headers: {
48
      revision: revision,
49
      "Accept": "application/vnd.api+json",
50
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
51
    },
52
    body: undefined,
53
  });
54
  if (!response.ok) {
55
    const text = await response.text();
56
    throw new Error(`${response.status} ${text}`);
57
  }
58
  return await response.json();
59
}
60