0

Search Orders

by
Published Nov 8, 2023

Search orders with Shopify's search syntax, or list the most recent ones.

Script shopify Verified

The script

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

3
/**
4
 * Search Orders
5
 * Search orders with Shopify's search syntax (e.g. `financial_status:paid created_at:>2026-01-01 tag:wholesale`). Leave query empty to list the most recent orders. Returns one page; pass pageInfo.endCursor as after to get the next one. Orders older than 60 days need the read_all_orders scope.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  query: string | undefined,
10
  sort_key:
11
    | "CREATED_AT"
12
    | "UPDATED_AT"
13
    | "PROCESSED_AT"
14
    | "ORDER_NUMBER"
15
    | "TOTAL_PRICE"
16
    | "FINANCIAL_STATUS"
17
    | "FULFILLMENT_STATUS"
18
    | "CUSTOMER_NAME"
19
    | "ID"
20
    | "RELEVANCE"
21
    | undefined,
22
  reverse: boolean | undefined,
23
  first: number | undefined,
24
  after: string | undefined
25
) {
26
  const response = await fetch(
27
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
28
    {
29
      method: "POST",
30
      headers: {
31
        "X-Shopify-Access-Token": auth.token,
32
        "Content-Type": "application/json",
33
        Accept: "application/json",
34
      },
35
      body: JSON.stringify({
36
        query: `query ($first: Int!, $after: String, $query: String, $sortKey: OrderSortKeys, $reverse: Boolean) { orders(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id legacyResourceId name email createdAt updatedAt processedAt cancelledAt closed test tags note displayFinancialStatus displayFulfillmentStatus currencyCode totalPriceSet { shopMoney { amount currencyCode } } customer { id displayName defaultEmailAddress { emailAddress } } } pageInfo { hasNextPage endCursor } } }`,
37
        variables: {
38
          first: first || 50,
39
          after: after || undefined,
40
          query: query || undefined,
41
          sortKey: sort_key || undefined,
42
          reverse: reverse || undefined,
43
        },
44
      }),
45
    }
46
  )
47

48
  if (!response.ok) {
49
    throw new Error(`${response.status} ${await response.text()}`)
50
  }
51

52
  const { data, errors } = await response.json()
53
  if (errors) {
54
    throw new Error(JSON.stringify(errors))
55
  }
56

57
  return data.orders
58
}
59

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 514 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Retrieves a list of orders
    7
     * Retrieves a list of 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
      ids: string | undefined,
    13
      name: string | undefined,
    14
      limit: string | undefined,
    15
      since_id: string | undefined,
    16
      created_at_min: string | undefined,
    17
      created_at_max: string | undefined,
    18
      updated_at_min: string | undefined,
    19
      updated_at_max: string | undefined,
    20
      processed_at_min: string | undefined,
    21
      processed_at_max: string | undefined,
    22
      attribution_app_id: string | undefined,
    23
      status: string | undefined,
    24
      financial_status: string | undefined,
    25
      fulfillment_status: string | undefined,
    26
      fields: string | undefined
    27
    ) {
    28
      const url = new URL(
    29
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders.json`
    30
      );
    31
      for (const [k, v] of [
    32
        ["ids", ids],
    33
        ["name", name],
    34
        ["limit", limit],
    35
        ["since_id", since_id],
    36
        ["created_at_min", created_at_min],
    37
        ["created_at_max", created_at_max],
    38
        ["updated_at_min", updated_at_min],
    39
        ["updated_at_max", updated_at_max],
    40
        ["processed_at_min", processed_at_min],
    41
        ["processed_at_max", processed_at_max],
    42
        ["attribution_app_id", attribution_app_id],
    43
        ["status", status],
    44
        ["financial_status", financial_status],
    45
        ["fulfillment_status", fulfillment_status],
    46
        ["fields", fields],
    47
      ]) {
    48
        if (v !== undefined && v !== "") {
    49
          url.searchParams.append(k, v);
    50
        }
    51
      }
    52
      const response = await fetch(url, {
    53
        method: "GET",
    54
        headers: {
    55
          "X-Shopify-Access-Token": auth.token,
    56
        },
    57
        body: undefined,
    58
      });
    59
      if (!response.ok) {
    60
        const text = await response.text();
    61
        throw new Error(`${response.status} ${text}`);
    62
      }
    63
      return await response.json();
    64
    }
    65