0

Update Article

by
Published Nov 8, 2023

Update an article; empty fields are left unchanged.

Script shopify Verified

The script

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

3
/**
4
 * Update Article
5
 * Update an article. Empty fields are left unchanged; tags, when given, replace the existing list. Visibility only changes when published is set to publish or unpublish. Accepts a numeric ID or a GID. Needs the write_content scope.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  article_id: string,
10
  title: string | undefined,
11
  author_name: string | undefined,
12
  body_html: string | undefined,
13
  summary_html: string | undefined,
14
  tags: string[] | undefined,
15
  image_url: string | undefined,
16
  image_alt_text: string | undefined,
17
  published: "keep" | "publish" | "unpublish" = "keep",
18
  publish_date: string | undefined
19
) {
20
  const id = article_id.startsWith("gid://")
21
    ? article_id
22
    : `gid://shopify/Article/${article_id}`
23

24
  const response = await fetch(
25
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
26
    {
27
      method: "POST",
28
      headers: {
29
        "X-Shopify-Access-Token": auth.token,
30
        "Content-Type": "application/json",
31
        Accept: "application/json",
32
      },
33
      body: JSON.stringify({
34
        query: `mutation ($id: ID!, $article: ArticleUpdateInput!) { articleUpdate(id: $id, article: $article) { article { id title handle isPublished publishedAt author { name } blog { id title } tags summary image { url altText } createdAt updatedAt } userErrors { field message } } }`,
35
        variables: {
36
          id,
37
          article: {
38
            title: title || undefined,
39
            author: author_name ? { name: author_name } : undefined,
40
            body: body_html || undefined,
41
            summary: summary_html || undefined,
42
            tags: tags?.length ? tags : undefined,
43
            image: image_url
44
              ? { url: image_url, altText: image_alt_text || undefined }
45
              : undefined,
46
            isPublished:
47
              published === "publish"
48
                ? true
49
                : published === "unpublish"
50
                  ? false
51
                  : undefined,
52
            publishDate: publish_date || undefined,
53
          },
54
        },
55
      }),
56
    }
57
  )
58

59
  if (!response.ok) {
60
    throw new Error(`${response.status} ${await response.text()}`)
61
  }
62

63
  const { data, errors } = await response.json()
64
  if (errors) {
65
    throw new Error(JSON.stringify(errors))
66
  }
67

68
  if (data.articleUpdate.userErrors.length) {
69
    throw new Error(JSON.stringify(data.articleUpdate.userErrors))
70
  }
71

72
  return data.articleUpdate
73
}
74

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