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