1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Profile IDs for List |
7 | * Get profile membership [relationships](https://developers. |
8 | */ |
9 | export async function main( |
10 | auth: Klaviyo, |
11 | id: string, |
12 | filter: string | undefined, |
13 | page_cursor_: string | undefined, |
14 | page_size_: string | undefined, |
15 | sort: "joined_group_at" | "-joined_group_at" | undefined, |
16 | revision: string, |
17 | ) { |
18 | const url = new URL( |
19 | `https://a.klaviyo.com/api/lists/${id}/relationships/profiles`, |
20 | ); |
21 | for (const [k, v] of [ |
22 | ["filter", filter], |
23 | ["page[cursor]", page_cursor_], |
24 | ["page[size]", page_size_], |
25 | ["sort", sort], |
26 | ]) { |
27 | if (v !== undefined && v !== "" && k !== undefined) { |
28 | url.searchParams.append(k, v); |
29 | } |
30 | } |
31 | const response = await fetch(url, { |
32 | method: "GET", |
33 | headers: { |
34 | revision: revision, |
35 | "Accept": "application/vnd.api+json", |
36 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
37 | }, |
38 | body: undefined, |
39 | }); |
40 | if (!response.ok) { |
41 | const text = await response.text(); |
42 | throw new Error(`${response.status} ${text}`); |
43 | } |
44 | return await response.json(); |
45 | } |
46 |
|