0

Add Categories to Catalog Item

by
Published Apr 8, 2025

Create a new catalog category relationship for the given item ID.*Rate limits*:Burst: `75/s`Steady: `700/m` **Scopes:** `catalogs: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
 * Add Categories to Catalog Item
7
 * Create a new catalog category relationship for the given item ID.*Rate limits*:Burst: `75/s`Steady: `700/m`
8
 */
9
export async function main(
10
  auth: Klaviyo,
11
  id: string,
12
  revision: string,
13
  body: { data: { type: "catalog-category"; id: string }[] },
14
) {
15
  const url = new URL(
16
    `https://a.klaviyo.com/api/catalog-items/${id}/relationships/categories`,
17
  );
18

19
  const response = await fetch(url, {
20
    method: "POST",
21
    headers: {
22
      revision: revision,
23
      "Accept": "application/vnd.api+json",
24
      "Content-Type": "application/vnd.api+json",
25
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
26
    },
27
    body: JSON.stringify(body),
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.json();
34
}
35