Edits history of script submission #23750 for ' Create Blog (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Blog
     * Create a blog in the online store. The handle is generated from the title when left empty. Needs the write_content scope.
     */
    export async function main(
      auth: RT.Shopify,
      title: string,
      handle: string | undefined,
      comment_policy: "CLOSED" | "MODERATED" | "AUTO_PUBLISHED" | 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 ($blog: BlogCreateInput!) { blogCreate(blog: $blog) { blog { id title handle commentPolicy tags templateSuffix createdAt updatedAt } userErrors { field message } } }`,
            variables: {
              blog: {
                title,
                handle: handle || undefined,
                commentPolicy: comment_policy || 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.blogCreate.userErrors.length) {
        throw new Error(JSON.stringify(data.blogCreate.userErrors))
      }
    
      return data.blogCreate
    }
    

    Submitted by hugo989 3 hours ago