0

Get Draft Order

by
Published Nov 8, 2023

Get a draft order with its line items, discount, totals and invoice URL.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Get Draft Order
5
 * Get a draft order with its customer, addresses, discount, shipping line, totals, invoice URL and first 50 line items. Once completed, order holds the resulting order.
6
 */
7
export async function main(auth: RT.Shopify, draft_order_id: string) {
8
  const id = draft_order_id.startsWith("gid://")
9
    ? draft_order_id
10
    : `gid://shopify/DraftOrder/${draft_order_id}`
11
  const response = await fetch(
12
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
13
    {
14
      method: "POST",
15
      headers: {
16
        "X-Shopify-Access-Token": auth.token,
17
        "Content-Type": "application/json",
18
        Accept: "application/json",
19
      },
20
      body: JSON.stringify({
21
        query: `query ($id: ID!) { draftOrder(id: $id) { id legacyResourceId name status email phone createdAt updatedAt completedAt invoiceUrl invoiceSentAt tags note2 ready reserveInventoryUntil subtotalPriceSet { shopMoney { amount currencyCode } } totalDiscountsSet { shopMoney { amount currencyCode } } totalShippingPriceSet { shopMoney { amount currencyCode } } totalTaxSet { shopMoney { amount currencyCode } } totalPriceSet { shopMoney { amount currencyCode } } customer { id displayName defaultEmailAddress { emailAddress } } order { id name } customAttributes { key value } appliedDiscount { title description value valueType amountSet { shopMoney { amount currencyCode } } } shippingLine { title originalPriceSet { shopMoney { amount currencyCode } } } shippingAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } billingAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } lineItems(first: 50) { nodes { id name sku quantity custom originalUnitPriceSet { shopMoney { amount currencyCode } } discountedTotalSet { shopMoney { amount currencyCode } } variant { id title } product { id title } } pageInfo { hasNextPage endCursor } } } }`,
22
        variables: { id },
23
      }),
24
    }
25
  )
26

27
  if (!response.ok) {
28
    throw new Error(`${response.status} ${await response.text()}`)
29
  }
30

31
  const { data, errors } = await response.json()
32
  if (errors) {
33
    throw new Error(JSON.stringify(errors))
34
  }
35

36
  return data.draftOrder
37
}
38

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Receive a single DraftOrder
    7
     * Retrieves a specific draft order
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      draft_order_id: string,
    13
      fields: string | undefined
    14
    ) {
    15
      const url = new URL(
    16
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/draft_orders/${draft_order_id}.json`
    17
      );
    18
      for (const [k, v] of [["fields", fields]]) {
    19
        if (v !== undefined && v !== "") {
    20
          url.searchParams.append(k, v);
    21
        }
    22
      }
    23
      const response = await fetch(url, {
    24
        method: "GET",
    25
        headers: {
    26
          "X-Shopify-Access-Token": auth.token,
    27
        },
    28
        body: undefined,
    29
      });
    30
      if (!response.ok) {
    31
        const text = await response.text();
    32
        throw new Error(`${response.status} ${text}`);
    33
      }
    34
      return await response.json();
    35
    }
    36