Creates a smart collection

Creates a new smart collection using the specified rules.

Script shopify Verified

by hugo697 ยท 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Creates a smart collection
7
 * Creates a new smart collection using the specified rules.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  body: {
13
    smart_collection?: {
14
      published?: boolean;
15
      rules?: {
16
        column?: string;
17
        condition?: string;
18
        relation?: string;
19
        [k: string]: unknown;
20
      }[];
21
      title?: string;
22
      [k: string]: unknown;
23
    };
24
    [k: string]: unknown;
25
  }
26
) {
27
  const url = new URL(
28
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/smart_collections.json`
29
  );
30

31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Content-Type": "application/json",
35
      "X-Shopify-Access-Token": auth.token,
36
    },
37
    body: JSON.stringify(body),
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45