0

RetrieveInventoryCount

by
Published Oct 17, 2025

Retrieves the current calculated stock count for a given [CatalogObject]($m/CatalogObject) at a given set of [Location]($m/Location)s. Responses are paginated and unsorted. For more sophisticated queries, use a batch endpoint.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * RetrieveInventoryCount
7
 * Retrieves the current calculated stock count for a given
8
[CatalogObject]($m/CatalogObject) at a given set of
9
[Location]($m/Location)s. Responses are paginated and unsorted.
10
For more sophisticated queries, use a batch endpoint.
11
 */
12
export async function main(
13
  auth: Square,
14
  catalog_object_id: string,
15
  location_ids: string | undefined,
16
  cursor: string | undefined,
17
) {
18
  const url = new URL(
19
    `https://connect.squareup.com/v2/inventory/${catalog_object_id}`,
20
  );
21
  for (const [k, v] of [
22
    ["location_ids", location_ids],
23
    ["cursor", cursor],
24
  ]) {
25
    if (v !== undefined && v !== "" && k !== undefined) {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42