0

Create Blog

by
Published Nov 8, 2023

Create a blog in the online store.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Create Blog
5
 * Create a blog in the online store. The handle is generated from the title when left empty. Needs the write_content scope.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  title: string,
10
  handle: string | undefined,
11
  comment_policy: "CLOSED" | "MODERATED" | "AUTO_PUBLISHED" | undefined
12
) {
13
  const response = await fetch(
14
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
15
    {
16
      method: "POST",
17
      headers: {
18
        "X-Shopify-Access-Token": auth.token,
19
        "Content-Type": "application/json",
20
        Accept: "application/json",
21
      },
22
      body: JSON.stringify({
23
        query: `mutation ($blog: BlogCreateInput!) { blogCreate(blog: $blog) { blog { id title handle commentPolicy tags templateSuffix createdAt updatedAt } userErrors { field message } } }`,
24
        variables: {
25
          blog: {
26
            title,
27
            handle: handle || undefined,
28
            commentPolicy: comment_policy || undefined,
29
          },
30
        },
31
      }),
32
    }
33
  )
34

35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  const { data, errors } = await response.json()
40
  if (errors) {
41
    throw new Error(JSON.stringify(errors))
42
  }
43

44
  if (data.blogCreate.userErrors.length) {
45
    throw new Error(JSON.stringify(data.blogCreate.userErrors))
46
  }
47

48
  return data.blogCreate
49
}
50

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Create a new Blog
    7
     * Create a new blog
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      body: {
    13
        blog?: {
    14
          metafields?: {
    15
            key?: string;
    16
            namespace?: string;
    17
            value?: string;
    18
            value_type?: 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}/blogs.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