0

Search Draft Orders

by
Published Nov 8, 2023

Search draft orders with Shopify's search syntax, or list them all.

Script shopify Verified

The script

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

3
/**
4
 * Search Draft Orders
5
 * Search draft orders with Shopify's search syntax (e.g. `status:open`, `customer_id:123`, `tag:quote updated_at:>2026-01-01`). Leave query empty to list them all. Returns one page; pass pageInfo.endCursor as after to get the next one.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  query: string | undefined,
10
  sort_key:
11
    | "UPDATED_AT"
12
    | "NUMBER"
13
    | "STATUS"
14
    | "TOTAL_PRICE"
15
    | "CUSTOMER_NAME"
16
    | "ID"
17
    | "RELEVANCE"
18
    | undefined,
19
  reverse: boolean | undefined,
20
  first: number | undefined,
21
  after: string | undefined
22
) {
23
  const response = await fetch(
24
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
25
    {
26
      method: "POST",
27
      headers: {
28
        "X-Shopify-Access-Token": auth.token,
29
        "Content-Type": "application/json",
30
        Accept: "application/json",
31
      },
32
      body: JSON.stringify({
33
        query: `query ($first: Int!, $after: String, $query: String, $sortKey: DraftOrderSortKeys, $reverse: Boolean) { draftOrders(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { 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 } } pageInfo { hasNextPage endCursor } } }`,
34
        variables: {
35
          first: first || 50,
36
          after: after || undefined,
37
          query: query || undefined,
38
          sortKey: sort_key || undefined,
39
          reverse: reverse || undefined,
40
        },
41
      }),
42
    }
43
  )
44

45
  if (!response.ok) {
46
    throw new Error(`${response.status} ${await response.text()}`)
47
  }
48

49
  const { data, errors } = await response.json()
50
  if (errors) {
51
    throw new Error(JSON.stringify(errors))
52
  }
53

54
  return data.draftOrders
55
}
56

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
     * Retrieves a list of draft orders
    7
     * Retrieves a list of draft orders. Note: As of version 2019-10, this endpoint implements pagination by using links that are provided in the response header. Sending the page parameter will return an error. To learn more, see Making requests to paginated REST Admin API endpoints.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      fields: string | undefined,
    13
      limit: string | undefined,
    14
      since_id: string | undefined,
    15
      updated_at_min: string | undefined,
    16
      updated_at_max: string | undefined,
    17
      ids: string | undefined,
    18
      status: string | undefined
    19
    ) {
    20
      const url = new URL(
    21
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/draft_orders.json`
    22
      );
    23
      for (const [k, v] of [
    24
        ["fields", fields],
    25
        ["limit", limit],
    26
        ["since_id", since_id],
    27
        ["updated_at_min", updated_at_min],
    28
        ["updated_at_max", updated_at_max],
    29
        ["ids", ids],
    30
        ["status", status],
    31
      ]) {
    32
        if (v !== undefined && v !== "") {
    33
          url.searchParams.append(k, v);
    34
        }
    35
      }
    36
      const response = await fetch(url, {
    37
        method: "GET",
    38
        headers: {
    39
          "X-Shopify-Access-Token": auth.token,
    40
        },
    41
        body: undefined,
    42
      });
    43
      if (!response.ok) {
    44
        const text = await response.text();
    45
        throw new Error(`${response.status} ${text}`);
    46
      }
    47
      return await response.json();
    48
    }
    49