Creates a new product
One script reply has been approved by the moderators Verified

Creates a new product. If you want to set the product's SEO information, then you can use the following properties: metafields_global_title_tag: The name of the product used for SEO purposes. Generally added to the <meta name='title'> tag. metafields_global_description_tag: A description of the product used for SEO purposes. Generally added to the <meta name='description'> tag.

Created by hugo697 883 days ago Picked 1 time
Submitted by hugo697 Typescript (fetch-only)
Verified 337 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Creates a new product
7
 * Creates a new product. If you want to set the product's SEO information, then you can use the following properties:  metafields_global_title_tag: The name of the product used for SEO purposes. Generally added to the &lt;meta name='title'&gt; tag. metafields_global_description_tag: A description of the product used for SEO purposes. Generally added to the &lt;meta name='description'&gt; tag.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  body: {
13
    product?: {
14
      body_html?: string;
15
      product_type?: string;
16
      published?: boolean;
17
      title?: string;
18
      vendor?: string;
19
      [k: string]: unknown;
20
    };
21
    [k: string]: unknown;
22
  }
23
) {
24
  const url = new URL(
25
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/products.json`
26
  );
27

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