0

Create Tag

by
Published Apr 8, 2025

Create a tag. An account cannot have more than **500** unique tags. A tag belongs to a single tag group. If the `tag_group_id` is not specified, the tag is added to the account's default tag group.*Rate limits*:Burst: `3/s`Steady: `60/m` **Scopes:** `tags:read` `tags:write`

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Create Tag
7
 * Create a tag. An account cannot have more than **500** unique tags.
8

9
A tag belongs to a single tag group. If the `tag_group_id` is not specified,
10
the tag is added to the account's default tag group.*Rate limits*:Burst: `3/s`Steady: `60/m`
11

12
 */
13
export async function main(
14
  auth: Klaviyo,
15
  revision: string,
16
  body: {
17
    data: {
18
      type: "tag";
19
      attributes: { name: string };
20
      relationships?: {
21
        "tag-group"?: { data?: { type: "tag-group"; id: string } };
22
      };
23
    };
24
  },
25
) {
26
  const url = new URL(`https://a.klaviyo.com/api/tags`);
27

28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      revision: revision,
32
      "Accept": "application/vnd.api+json",
33
      "Content-Type": "application/vnd.api+json",
34
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
35
    },
36
    body: JSON.stringify(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44