0

CreateLoyaltyAccount

by
Published Oct 17, 2025

Creates a loyalty account. To create a loyalty account, you must provide the `program_id` and a `mapping` with the `phone_number` of the buyer.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * CreateLoyaltyAccount
7
 * Creates a loyalty account. To create a loyalty account, you must provide the `program_id` and a `mapping` with the `phone_number` of the buyer.
8
 */
9
export async function main(
10
  auth: Square,
11
  body: {
12
    loyalty_account: {
13
      id?: string;
14
      program_id: string;
15
      balance?: number;
16
      lifetime_points?: number;
17
      customer_id?: string;
18
      enrolled_at?: string;
19
      created_at?: string;
20
      updated_at?: string;
21
      mapping?: { id?: string; created_at?: string; phone_number?: string };
22
      expiring_point_deadlines?: { points: number; expires_at: string }[];
23
    };
24
    idempotency_key: string;
25
  },
26
) {
27
  const url = new URL(`https://connect.squareup.com/v2/loyalty/accounts`);
28

29
  const response = await fetch(url, {
30
    method: "POST",
31
    headers: {
32
      "Content-Type": "application/json",
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: JSON.stringify(body),
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43