0

Create Page

by
Published Nov 8, 2023

Create a page 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 Page
5
 * Create a page in the online store. The handle is generated from the title when left empty; publish_date (ISO 8601) schedules publication. Needs the write_online_store_pages scope.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  title: string,
10
  body_html: string | undefined,
11
  handle: string | undefined,
12
  is_published: boolean = true,
13
  publish_date: string | undefined,
14
  template_suffix: string | undefined
15
) {
16
  const response = await fetch(
17
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
18
    {
19
      method: "POST",
20
      headers: {
21
        "X-Shopify-Access-Token": auth.token,
22
        "Content-Type": "application/json",
23
        Accept: "application/json",
24
      },
25
      body: JSON.stringify({
26
        query: `mutation ($page: PageCreateInput!) { pageCreate(page: $page) { page { id title handle isPublished publishedAt templateSuffix createdAt updatedAt } userErrors { field message } } }`,
27
        variables: {
28
          page: {
29
            title,
30
            body: body_html || undefined,
31
            handle: handle || undefined,
32
            isPublished: is_published ?? undefined,
33
            publishDate: publish_date || undefined,
34
            templateSuffix: template_suffix || undefined,
35
          },
36
        },
37
      }),
38
    }
39
  )
40

41
  if (!response.ok) {
42
    throw new Error(`${response.status} ${await response.text()}`)
43
  }
44

45
  const { data, errors } = await response.json()
46
  if (errors) {
47
    throw new Error(JSON.stringify(errors))
48
  }
49

50
  if (data.pageCreate.userErrors.length) {
51
    throw new Error(JSON.stringify(data.pageCreate.userErrors))
52
  }
53

54
  return data.pageCreate
55
}
56

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