Edits history of script submission #23717 for ' Create Page (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Page
     * 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.
     */
    export async function main(
      auth: RT.Shopify,
      title: string,
      body_html: string | undefined,
      handle: string | undefined,
      is_published: boolean = true,
      publish_date: string | undefined,
      template_suffix: string | undefined
    ) {
      const response = await fetch(
        `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
        {
          method: "POST",
          headers: {
            "X-Shopify-Access-Token": auth.token,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({
            query: `mutation ($page: PageCreateInput!) { pageCreate(page: $page) { page { id title handle isPublished publishedAt templateSuffix createdAt updatedAt } userErrors { field message } } }`,
            variables: {
              page: {
                title,
                body: body_html || undefined,
                handle: handle || undefined,
                isPublished: is_published ?? undefined,
                publishDate: publish_date || undefined,
                templateSuffix: template_suffix || undefined,
              },
            },
          }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const { data, errors } = await response.json()
      if (errors) {
        throw new Error(JSON.stringify(errors))
      }
    
      if (data.pageCreate.userErrors.length) {
        throw new Error(JSON.stringify(data.pageCreate.userErrors))
      }
    
      return data.pageCreate
    }
    

    Submitted by hugo989 3 hours ago