0

RetrieveOrderCustomAttribute

by
Published Oct 17, 2025

Retrieves a [custom attribute]($m/CustomAttribute) associated with an order. You can use the `with_definition` query parameter to also retrieve the custom attribute definition in the same call. To retrieve a custom attribute owned by another application, the `visibility` setting must be `VISIBILITY_READ_ONLY` or `VISIBILITY_READ_WRITE_VALUES`. Note that seller-defined custom attributes also known as custom fields) are always set to `VISIBILITY_READ_WRITE_VALUES`.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * RetrieveOrderCustomAttribute
7
 * Retrieves a [custom attribute]($m/CustomAttribute) associated with an order.
8

9
You can use the `with_definition` query parameter to also retrieve the custom attribute definition
10
in the same call.
11

12
To retrieve a custom attribute owned by another application, the `visibility` setting must be
13
`VISIBILITY_READ_ONLY` or `VISIBILITY_READ_WRITE_VALUES`. Note that seller-defined custom attributes
14
also known as custom fields) are always set to `VISIBILITY_READ_WRITE_VALUES`.
15
 */
16
export async function main(
17
  auth: Square,
18
  order_id: string,
19
  custom_attribute_key: string,
20
  version: string | undefined,
21
  with_definition: string | undefined,
22
) {
23
  const url = new URL(
24
    `https://connect.squareup.com/v2/orders/${order_id}/custom-attributes/${custom_attribute_key}`,
25
  );
26
  for (const [k, v] of [
27
    ["version", version],
28
    ["with_definition", with_definition],
29
  ]) {
30
    if (v !== undefined && v !== "" && k !== undefined) {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      Authorization: "Bearer " + auth.token,
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47