1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Lists |
7 | * Get all lists in an account. |
8 |
|
9 | Filter to request a subset of all lists. Lists can be filtered by `id`, `name`, `created`, and `updated` fields. |
10 |
|
11 | Returns a maximum of 10 results per page.*Rate limits*:Burst: `75/s`Steady: `700/m` |
12 |
|
13 | */ |
14 | export async function main( |
15 | auth: Klaviyo, |
16 | fields_flow_: string | undefined, |
17 | fields_list_: string | undefined, |
18 | fields_tag_: string | undefined, |
19 | filter: string | undefined, |
20 | include: string | undefined, |
21 | page_cursor_: string | undefined, |
22 | sort: |
23 | | "created" |
24 | | "-created" |
25 | | "id" |
26 | | "-id" |
27 | | "name" |
28 | | "-name" |
29 | | "updated" |
30 | | "-updated" |
31 | | undefined, |
32 | revision: string, |
33 | ) { |
34 | const url = new URL(`https://a.klaviyo.com/api/lists`); |
35 | for (const [k, v] of [ |
36 | ["fields[flow]", fields_flow_], |
37 | ["fields[list]", fields_list_], |
38 | ["fields[tag]", fields_tag_], |
39 | ["filter", filter], |
40 | ["include", include], |
41 | ["page[cursor]", page_cursor_], |
42 | ["sort", sort], |
43 | ]) { |
44 | if (v !== undefined && v !== "" && k !== undefined) { |
45 | url.searchParams.append(k, v); |
46 | } |
47 | } |
48 | const response = await fetch(url, { |
49 | method: "GET", |
50 | headers: { |
51 | revision: revision, |
52 | "Accept": "application/vnd.api+json", |
53 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
54 | }, |
55 | body: undefined, |
56 | }); |
57 | if (!response.ok) { |
58 | const text = await response.text(); |
59 | throw new Error(`${response.status} ${text}`); |
60 | } |
61 | return await response.json(); |
62 | } |
63 |
|