0

SearchOrders

by
Published Oct 17, 2025

Search all orders for one or more locations.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * SearchOrders
7
 * Search all orders for one or more locations.
8
 */
9
export async function main(
10
  auth: Square,
11
  body: {
12
    location_ids?: string[];
13
    cursor?: string;
14
    query?: {
15
      filter?: {
16
        state_filter?: {
17
          states: "OPEN" | "COMPLETED" | "CANCELED" | "DRAFT"[];
18
        };
19
        date_time_filter?: {
20
          created_at?: { start_at?: string; end_at?: string };
21
          updated_at?: { start_at?: string; end_at?: string };
22
          closed_at?: { start_at?: string; end_at?: string };
23
        };
24
        fulfillment_filter?: {
25
          fulfillment_types?: "PICKUP" | "SHIPMENT" | "DELIVERY"[];
26
          fulfillment_states?:
27
            | "COMPLETED"
28
            | "CANCELED"
29
            | "PROPOSED"
30
            | "RESERVED"
31
            | "PREPARED"
32
            | "FAILED"[];
33
        };
34
        source_filter?: { source_names?: string[] };
35
        customer_filter?: { customer_ids?: string[] };
36
      };
37
      sort?: {
38
        sort_field: "CREATED_AT" | "UPDATED_AT" | "CLOSED_AT";
39
        sort_order?: "DESC" | "ASC";
40
      };
41
    };
42
    limit?: number;
43
    return_entries?: false | true;
44
  },
45
) {
46
  const url = new URL(`https://connect.squareup.com/v2/orders/search`);
47

48
  const response = await fetch(url, {
49
    method: "POST",
50
    headers: {
51
      "Content-Type": "application/json",
52
      Authorization: "Bearer " + auth.token,
53
    },
54
    body: JSON.stringify(body),
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62