0

Get Profiles for List

by
Published Apr 8, 2025

Get all profiles within a list with the given list 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
 * Get Profiles for List
7
 * Get all profiles within a list with the given list ID.
8
 */
9
export async function main(
10
  auth: Klaviyo,
11
  id: string,
12
  additional_fields_profile_: string | undefined,
13
  fields_profile_: string | undefined,
14
  filter: string | undefined,
15
  page_cursor_: string | undefined,
16
  page_size_: string | undefined,
17
  sort: "joined_group_at" | "-joined_group_at" | undefined,
18
  revision: string,
19
) {
20
  const url = new URL(`https://a.klaviyo.com/api/lists/${id}/profiles`);
21
  for (const [k, v] of [
22
    ["additional-fields[profile]", additional_fields_profile_],
23
    ["fields[profile]", fields_profile_],
24
    ["filter", filter],
25
    ["page[cursor]", page_cursor_],
26
    ["page[size]", page_size_],
27
    ["sort", sort],
28
  ]) {
29
    if (v !== undefined && v !== "" && k !== undefined) {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "GET",
35
    headers: {
36
      revision: revision,
37
      "Accept": "application/vnd.api+json",
38
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
39
    },
40
    body: undefined,
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48