Create a new DraftOrder

Creates a draft order.

Script shopify Verified

by hugo697 ยท 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Create a new DraftOrder
7
 * Creates a draft order.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  body: {
13
    draft_order?: {
14
      line_items?: {
15
        applied_discount?: {
16
          amount?: string;
17
          description?: string;
18
          title?: string;
19
          value?: string;
20
          value_type?: string;
21
          [k: string]: unknown;
22
        };
23
        price?: string;
24
        quantity?: number;
25
        title?: string;
26
        [k: string]: unknown;
27
      }[];
28
      [k: string]: unknown;
29
    };
30
    [k: string]: unknown;
31
  }
32
) {
33
  const url = new URL(
34
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/draft_orders.json`
35
  );
36

37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      "Content-Type": "application/json",
41
      "X-Shopify-Access-Token": auth.token,
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51