1 | |
2 |
|
3 | |
4 | * Create Blog |
5 | * Create a blog in the online store. The handle is generated from the title when left empty. Needs the write_content scope. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | title: string, |
10 | handle: string | undefined, |
11 | comment_policy: "CLOSED" | "MODERATED" | "AUTO_PUBLISHED" | undefined |
12 | ) { |
13 | const response = await fetch( |
14 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
15 | { |
16 | method: "POST", |
17 | headers: { |
18 | "X-Shopify-Access-Token": auth.token, |
19 | "Content-Type": "application/json", |
20 | Accept: "application/json", |
21 | }, |
22 | body: JSON.stringify({ |
23 | query: `mutation ($blog: BlogCreateInput!) { blogCreate(blog: $blog) { blog { id title handle commentPolicy tags templateSuffix createdAt updatedAt } userErrors { field message } } }`, |
24 | variables: { |
25 | blog: { |
26 | title, |
27 | handle: handle || undefined, |
28 | commentPolicy: comment_policy || undefined, |
29 | }, |
30 | }, |
31 | }), |
32 | } |
33 | ) |
34 |
|
35 | if (!response.ok) { |
36 | throw new Error(`${response.status} ${await response.text()}`) |
37 | } |
38 |
|
39 | const { data, errors } = await response.json() |
40 | if (errors) { |
41 | throw new Error(JSON.stringify(errors)) |
42 | } |
43 |
|
44 | if (data.blogCreate.userErrors.length) { |
45 | throw new Error(JSON.stringify(data.blogCreate.userErrors)) |
46 | } |
47 |
|
48 | return data.blogCreate |
49 | } |
50 |
|