1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Tags |
7 | * List all tags in an account. |
8 |
|
9 | Tags can be filtered by `name`, and sorted by `name` or `id` in ascending or descending order. |
10 |
|
11 | Returns a maximum of 50 tags per request, which can be paginated with |
12 | [cursor-based pagination](https://developers.klaviyo.com/en/v2022-10-17/reference/api_overview#pagination).*Rate limits*:Burst: `3/s`Steady: `60/m` |
13 |
|
14 | */ |
15 | export async function main( |
16 | auth: Klaviyo, |
17 | fields_tag_group_: string | undefined, |
18 | fields_tag_: string | undefined, |
19 | filter: string | undefined, |
20 | include: string | undefined, |
21 | page_cursor_: string | undefined, |
22 | sort: "id" | "-id" | "name" | "-name" | undefined, |
23 | revision: string, |
24 | ) { |
25 | const url = new URL(`https://a.klaviyo.com/api/tags`); |
26 | for (const [k, v] of [ |
27 | ["fields[tag-group]", fields_tag_group_], |
28 | ["fields[tag]", fields_tag_], |
29 | ["filter", filter], |
30 | ["include", include], |
31 | ["page[cursor]", page_cursor_], |
32 | ["sort", sort], |
33 | ]) { |
34 | if (v !== undefined && v !== "" && k !== undefined) { |
35 | url.searchParams.append(k, v); |
36 | } |
37 | } |
38 | const response = await fetch(url, { |
39 | method: "GET", |
40 | headers: { |
41 | revision: revision, |
42 | "Accept": "application/vnd.api+json", |
43 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
44 | }, |
45 | body: undefined, |
46 | }); |
47 | if (!response.ok) { |
48 | const text = await response.text(); |
49 | throw new Error(`${response.status} ${text}`); |
50 | } |
51 | return await response.json(); |
52 | } |
53 |
|