1 | |
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 |
|