0

Create balance definition

by
Published Apr 8, 2025

Creates balance definition and returns information

Script brevo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Brevo = {
3
  apiKey: string;
4
};
5
/**
6
 * Create balance definition
7
 * Creates balance definition and returns information
8
 */
9
export async function main(
10
  auth: Brevo,
11
  loyaltyProgramId: string,
12
  body: {
13
    name: string;
14
    description?: string;
15
    meta?: {};
16
    unit:
17
      | "points"
18
      | "EUR"
19
      | "USD"
20
      | "MXN"
21
      | "GBP"
22
      | "INR"
23
      | "CAD"
24
      | "SGD"
25
      | "RON"
26
      | "JPY"
27
      | "MYR"
28
      | "CLP"
29
      | "PEN"
30
      | "MAD"
31
      | "AUD"
32
      | "CHF"
33
      | "BRL";
34
    minAmount?: number;
35
    maxAmount?: number;
36
    maxCreditAmountLimit?: number;
37
    maxDebitAmountLimit?: number;
38
    balanceOptionAmountOvertakingStrategy?: "strict" | "partial";
39
    balanceOptionCreditRounding?: "natural" | "upper" | "lower";
40
    balanceOptionDebitRounding?: "natural" | "upper" | "lower";
41
    balanceAvailabilityDurationValue?: number;
42
    balanceAvailabilityDurationUnit?: "day" | "week" | "month" | "year";
43
    balanceAvailabilityDurationModifier?:
44
      | "startOfPeriod"
45
      | "endOfPeriod"
46
      | "noModification";
47
    balanceExpirationDate?: string;
48
  },
49
) {
50
  const url = new URL(
51
    `https://api.brevo.com/v3/loyalty/balance/programs/${loyaltyProgramId}/balance-definitions`,
52
  );
53

54
  const response = await fetch(url, {
55
    method: "POST",
56
    headers: {
57
      "Content-Type": "application/json",
58
      "api-key": auth.apiKey,
59
    },
60
    body: JSON.stringify(body),
61
  });
62
  if (!response.ok) {
63
    const text = await response.text();
64
    throw new Error(`${response.status} ${text}`);
65
  }
66
  return await response.json();
67
}
68