0

Get Tag Groups

by
Published Apr 8, 2025

List all tag groups in an account. Every account has one default tag group. Tag groups can be filtered by `name`, `exclusive`, and `default`, and sorted by `name` or `id` in ascending or descending order. Returns a maximum of 25 tag groups per request, which can be paginated with [cursor-based pagination](https://developers.klaviyo.com/en/v2022-10-17/reference/api_overview#pagination).*Rate limits*:Burst: `3/s`Steady: `60/m` **Scopes:** `tags:read`

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 Tag Groups
7
 * List all tag groups in an account. Every account has one default tag group.
8

9
Tag groups can be filtered by `name`, `exclusive`, and `default`, and sorted by `name` or `id` in ascending or descending order.
10

11
Returns a maximum of 25 tag groups 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
  filter: string | undefined,
19
  page_cursor_: string | undefined,
20
  sort: "id" | "-id" | "name" | "-name" | undefined,
21
  revision: string,
22
) {
23
  const url = new URL(`https://a.klaviyo.com/api/tag-groups`);
24
  for (const [k, v] of [
25
    ["fields[tag-group]", fields_tag_group_],
26
    ["filter", filter],
27
    ["page[cursor]", page_cursor_],
28
    ["sort", sort],
29
  ]) {
30
    if (v !== undefined && v !== "" && k !== undefined) {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      revision: revision,
38
      "Accept": "application/vnd.api+json",
39
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
40
    },
41
    body: undefined,
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49