0
Retrieves a list of orders
One script reply has been approved by the moderators Verified

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.

Created by hugo697 603 days ago Viewed 22454 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 603 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