0

Update Page

by
Published Nov 8, 2023

Update a page's title, body, handle or visibility; 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 Page
5
 * Update a page. Empty fields are left unchanged, and visibility only changes when published is set to publish or unpublish. Accepts a numeric ID or a GID. Needs the write_online_store_pages scope.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  page_id: string,
10
  title: string | undefined,
11
  body_html: string | undefined,
12
  handle: string | undefined,
13
  published: "keep" | "publish" | "unpublish" = "keep",
14
  publish_date: string | undefined
15
) {
16
  const id = page_id.startsWith("gid://")
17
    ? page_id
18
    : `gid://shopify/Page/${page_id}`
19

20
  const response = await fetch(
21
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
22
    {
23
      method: "POST",
24
      headers: {
25
        "X-Shopify-Access-Token": auth.token,
26
        "Content-Type": "application/json",
27
        Accept: "application/json",
28
      },
29
      body: JSON.stringify({
30
        query: `mutation ($id: ID!, $page: PageUpdateInput!) { pageUpdate(id: $id, page: $page) { page { id title handle isPublished publishedAt templateSuffix createdAt updatedAt } userErrors { field message } } }`,
31
        variables: {
32
          id,
33
          page: {
34
            title: title || undefined,
35
            body: body_html || undefined,
36
            handle: handle || undefined,
37
            isPublished:
38
              published === "publish"
39
                ? true
40
                : published === "unpublish"
41
                  ? false
42
                  : undefined,
43
            publishDate: publish_date || undefined,
44
          },
45
        },
46
      }),
47
    }
48
  )
49

50
  if (!response.ok) {
51
    throw new Error(`${response.status} ${await response.text()}`)
52
  }
53

54
  const { data, errors } = await response.json()
55
  if (errors) {
56
    throw new Error(JSON.stringify(errors))
57
  }
58

59
  if (data.pageUpdate.userErrors.length) {
60
    throw new Error(JSON.stringify(data.pageUpdate.userErrors))
61
  }
62

63
  return data.pageUpdate
64
}
65

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 a page
    7
     * Updates a page.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      page_id: string,
    13
      body: {
    14
        page?: { id?: number; published?: boolean; [k: string]: unknown };
    15
        [k: string]: unknown;
    16
      }
    17
    ) {
    18
      const url = new URL(
    19
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/pages/${page_id}.json`
    20
      );
    21
    
    
    22
      const response = await fetch(url, {
    23
        method: "PUT",
    24
        headers: {
    25
          "Content-Type": "application/json",
    26
          "X-Shopify-Access-Token": auth.token,
    27
        },
    28
        body: JSON.stringify(body),
    29
      });
    30
      if (!response.ok) {
    31
        const text = await response.text();
    32
        throw new Error(`${response.status} ${text}`);
    33
      }
    34
      return await response.json();
    35
    }
    36