0

CreateLoyaltyReward

by
Published Oct 17, 2025

Creates a loyalty reward. In the process, the endpoint does following: - Uses the `reward_tier_id` in the request to determine the number of points to lock for this reward. - If the request includes `order_id`, it adds the reward and related discount to the order. After a reward is created, the points are locked and not available for the buyer to redeem another reward.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * CreateLoyaltyReward
7
 * Creates a loyalty reward. In the process, the endpoint does following:
8

9
- Uses the `reward_tier_id` in the request to determine the number of points
10
to lock for this reward.
11
- If the request includes `order_id`, it adds the reward and related discount to the order.
12

13
After a reward is created, the points are locked and
14
not available for the buyer to redeem another reward.
15
 */
16
export async function main(
17
  auth: Square,
18
  body: {
19
    reward: {
20
      id?: string;
21
      status?: "ISSUED" | "REDEEMED" | "DELETED";
22
      loyalty_account_id: string;
23
      reward_tier_id: string;
24
      points?: number;
25
      order_id?: string;
26
      created_at?: string;
27
      updated_at?: string;
28
      redeemed_at?: string;
29
    };
30
    idempotency_key: string;
31
  },
32
) {
33
  const url = new URL(`https://connect.squareup.com/v2/loyalty/rewards`);
34

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