Creates a custom collection

Creates a custom collection

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 custom collection
7
 * Creates a custom collection
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  body: {
13
    custom_collection?: {
14
      image?: { alt?: string; src?: string; [k: string]: unknown };
15
      title?: string;
16
      [k: string]: unknown;
17
    };
18
    [k: string]: unknown;
19
  }
20
) {
21
  const url = new URL(
22
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/custom_collections.json`
23
  );
24

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