0

Create Article

by
Published Nov 8, 2023

Create an article in a blog.

Script shopify Verified

The script

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

3
export type DynSelect_blog_id = string
4

5
export async function blog_id(auth: RT.Shopify) {
6
  const response = await fetch(
7
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
8
    {
9
      method: "POST",
10
      headers: {
11
        "X-Shopify-Access-Token": auth.token,
12
        "Content-Type": "application/json",
13
        Accept: "application/json",
14
      },
15
      body: JSON.stringify({
16
        query: `query { blogs(first: 250, sortKey: TITLE) { nodes { id title } } }`,
17
      }),
18
    }
19
  )
20
  if (!response.ok) {
21
    throw new Error(`${response.status} ${await response.text()}`)
22
  }
23
  const { data, errors } = await response.json()
24
  if (errors) {
25
    throw new Error(JSON.stringify(errors))
26
  }
27
  return data.blogs.nodes.map((b: { id: string; title: string }) => ({
28
    value: b.id,
29
    label: b.title,
30
  }))
31
}
32

33
/**
34
 * Create Article
35
 * Create an article in a blog. image_url must be a publicly reachable image; publish_date (ISO 8601) schedules publication. Needs the write_content scope.
36
 */
37
export async function main(
38
  auth: RT.Shopify,
39
  blog_id: DynSelect_blog_id,
40
  title: string,
41
  author_name: string,
42
  body_html: string | undefined,
43
  summary_html: string | undefined,
44
  tags: string[] | undefined,
45
  image_url: string | undefined,
46
  image_alt_text: string | undefined,
47
  is_published: boolean = true,
48
  publish_date: string | undefined
49
) {
50
  const blogId = blog_id.startsWith("gid://")
51
    ? blog_id
52
    : `gid://shopify/Blog/${blog_id}`
53

54
  const response = await fetch(
55
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
56
    {
57
      method: "POST",
58
      headers: {
59
        "X-Shopify-Access-Token": auth.token,
60
        "Content-Type": "application/json",
61
        Accept: "application/json",
62
      },
63
      body: JSON.stringify({
64
        query: `mutation ($article: ArticleCreateInput!) { articleCreate(article: $article) { article { id title handle isPublished publishedAt author { name } blog { id title } tags summary image { url altText } createdAt updatedAt } userErrors { field message } } }`,
65
        variables: {
66
          article: {
67
            blogId,
68
            title,
69
            author: { name: author_name },
70
            body: body_html || undefined,
71
            summary: summary_html || undefined,
72
            tags: tags?.length ? tags : undefined,
73
            image: image_url
74
              ? { url: image_url, altText: image_alt_text || undefined }
75
              : undefined,
76
            isPublished: is_published ?? undefined,
77
            publishDate: publish_date || undefined,
78
          },
79
        },
80
      }),
81
    }
82
  )
83

84
  if (!response.ok) {
85
    throw new Error(`${response.status} ${await response.text()}`)
86
  }
87

88
  const { data, errors } = await response.json()
89
  if (errors) {
90
    throw new Error(JSON.stringify(errors))
91
  }
92

93
  if (data.articleCreate.userErrors.length) {
94
    throw new Error(JSON.stringify(data.articleCreate.userErrors))
95
  }
96

97
  return data.articleCreate
98
}
99

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
     * Creates an article for a blog
    7
     * Creates an article for a blog
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      blog_id: string,
    13
      body: {
    14
        article?: {
    15
          author?: string;
    16
          body_html?: string;
    17
          published?: boolean;
    18
          tags?: string;
    19
          title?: string;
    20
          [k: string]: unknown;
    21
        };
    22
        [k: string]: unknown;
    23
      }
    24
    ) {
    25
      const url = new URL(
    26
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/blogs/${blog_id}/articles.json`
    27
      );
    28
    
    
    29
      const response = await fetch(url, {
    30
        method: "POST",
    31
        headers: {
    32
          "Content-Type": "application/json",
    33
          "X-Shopify-Access-Token": auth.token,
    34
        },
    35
        body: JSON.stringify(body),
    36
      });
    37
      if (!response.ok) {
    38
        const text = await response.text();
    39
        throw new Error(`${response.status} ${text}`);
    40
      }
    41
      return await response.json();
    42
    }
    43