Creates a new script tag

Creates a new script tag

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 new script tag
7
 * Creates a new script tag
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  body: {
13
    script_tag?: { body?: string; [k: string]: unknown };
14
    [k: string]: unknown;
15
  }
16
) {
17
  const url = new URL(
18
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/script_tags.json`
19
  );
20

21
  const response = await fetch(url, {
22
    method: "POST",
23
    headers: {
24
      "Content-Type": "application/json",
25
      "X-Shopify-Access-Token": auth.token,
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