0

SearchLoyaltyAccounts

by
Published Oct 17, 2025

Searches for loyalty accounts in a loyalty program. You can search for a loyalty account using the phone number or customer ID associated with the account. To return all loyalty accounts, specify an empty `query` object or omit it entirely. Search results are sorted by `created_at` in ascending order.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * SearchLoyaltyAccounts
7
 * Searches for loyalty accounts in a loyalty program.
8

9
You can search for a loyalty account using the phone number or customer ID associated with the account. To return all loyalty accounts, specify an empty `query` object or omit it entirely.
10

11
Search results are sorted by `created_at` in ascending order.
12
 */
13
export async function main(
14
  auth: Square,
15
  body: {
16
    query?: {
17
      mappings?: { id?: string; created_at?: string; phone_number?: string }[];
18
      customer_ids?: string[];
19
    };
20
    limit?: number;
21
    cursor?: string;
22
  },
23
) {
24
  const url = new URL(
25
    `https://connect.squareup.com/v2/loyalty/accounts/search`,
26
  );
27

28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      "Content-Type": "application/json",
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: JSON.stringify(body),
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