0

CreateGiftCard

by
Published Oct 17, 2025

Creates a digital gift card or registers a physical (plastic) gift card. The resulting gift card has a `PENDING` state. To activate a gift card so that it can be redeemed for purchases, call [CreateGiftCardActivity]($e/GiftCardActivities/CreateGiftCardActivity) and create an `ACTIVATE` activity with the initial balance. Alternatively, you can use [RefundPayment]($e/Refunds/RefundPayment) to refund a payment to the new gift card.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * CreateGiftCard
7
 * Creates a digital gift card or registers a physical (plastic) gift card. The resulting gift card
8
has a `PENDING` state. To activate a gift card so that it can be redeemed for purchases, call
9
[CreateGiftCardActivity]($e/GiftCardActivities/CreateGiftCardActivity) and create an `ACTIVATE`
10
activity with the initial balance. Alternatively, you can use [RefundPayment]($e/Refunds/RefundPayment)
11
to refund a payment to the new gift card.
12
 */
13
export async function main(
14
  auth: Square,
15
  body: {
16
    idempotency_key: string;
17
    location_id: string;
18
    gift_card: {
19
      id?: string;
20
      type: "PHYSICAL" | "DIGITAL";
21
      gan_source?: "SQUARE" | "OTHER";
22
      state?: "ACTIVE" | "DEACTIVATED" | "BLOCKED" | "PENDING";
23
      balance_money?: {
24
        amount?: number;
25
        currency?:
26
          | "UNKNOWN_CURRENCY"
27
          | "AED"
28
          | "AFN"
29
          | "ALL"
30
          | "AMD"
31
          | "ANG"
32
          | "AOA"
33
          | "ARS"
34
          | "AUD"
35
          | "AWG"
36
          | "AZN"
37
          | "BAM"
38
          | "BBD"
39
          | "BDT"
40
          | "BGN"
41
          | "BHD"
42
          | "BIF"
43
          | "BMD"
44
          | "BND"
45
          | "BOB"
46
          | "BOV"
47
          | "BRL"
48
          | "BSD"
49
          | "BTN"
50
          | "BWP"
51
          | "BYR"
52
          | "BZD"
53
          | "CAD"
54
          | "CDF"
55
          | "CHE"
56
          | "CHF"
57
          | "CHW"
58
          | "CLF"
59
          | "CLP"
60
          | "CNY"
61
          | "COP"
62
          | "COU"
63
          | "CRC"
64
          | "CUC"
65
          | "CUP"
66
          | "CVE"
67
          | "CZK"
68
          | "DJF"
69
          | "DKK"
70
          | "DOP"
71
          | "DZD"
72
          | "EGP"
73
          | "ERN"
74
          | "ETB"
75
          | "EUR"
76
          | "FJD"
77
          | "FKP"
78
          | "GBP"
79
          | "GEL"
80
          | "GHS"
81
          | "GIP"
82
          | "GMD"
83
          | "GNF"
84
          | "GTQ"
85
          | "GYD"
86
          | "HKD"
87
          | "HNL"
88
          | "HRK"
89
          | "HTG"
90
          | "HUF"
91
          | "IDR"
92
          | "ILS"
93
          | "INR"
94
          | "IQD"
95
          | "IRR"
96
          | "ISK"
97
          | "JMD"
98
          | "JOD"
99
          | "JPY"
100
          | "KES"
101
          | "KGS"
102
          | "KHR"
103
          | "KMF"
104
          | "KPW"
105
          | "KRW"
106
          | "KWD"
107
          | "KYD"
108
          | "KZT"
109
          | "LAK"
110
          | "LBP"
111
          | "LKR"
112
          | "LRD"
113
          | "LSL"
114
          | "LTL"
115
          | "LVL"
116
          | "LYD"
117
          | "MAD"
118
          | "MDL"
119
          | "MGA"
120
          | "MKD"
121
          | "MMK"
122
          | "MNT"
123
          | "MOP"
124
          | "MRO"
125
          | "MUR"
126
          | "MVR"
127
          | "MWK"
128
          | "MXN"
129
          | "MXV"
130
          | "MYR"
131
          | "MZN"
132
          | "NAD"
133
          | "NGN"
134
          | "NIO"
135
          | "NOK"
136
          | "NPR"
137
          | "NZD"
138
          | "OMR"
139
          | "PAB"
140
          | "PEN"
141
          | "PGK"
142
          | "PHP"
143
          | "PKR"
144
          | "PLN"
145
          | "PYG"
146
          | "QAR"
147
          | "RON"
148
          | "RSD"
149
          | "RUB"
150
          | "RWF"
151
          | "SAR"
152
          | "SBD"
153
          | "SCR"
154
          | "SDG"
155
          | "SEK"
156
          | "SGD"
157
          | "SHP"
158
          | "SLL"
159
          | "SLE"
160
          | "SOS"
161
          | "SRD"
162
          | "SSP"
163
          | "STD"
164
          | "SVC"
165
          | "SYP"
166
          | "SZL"
167
          | "THB"
168
          | "TJS"
169
          | "TMT"
170
          | "TND"
171
          | "TOP"
172
          | "TRY"
173
          | "TTD"
174
          | "TWD"
175
          | "TZS"
176
          | "UAH"
177
          | "UGX"
178
          | "USD"
179
          | "USN"
180
          | "USS"
181
          | "UYI"
182
          | "UYU"
183
          | "UZS"
184
          | "VEF"
185
          | "VND"
186
          | "VUV"
187
          | "WST"
188
          | "XAF"
189
          | "XAG"
190
          | "XAU"
191
          | "XBA"
192
          | "XBB"
193
          | "XBC"
194
          | "XBD"
195
          | "XCD"
196
          | "XDR"
197
          | "XOF"
198
          | "XPD"
199
          | "XPF"
200
          | "XPT"
201
          | "XTS"
202
          | "XXX"
203
          | "YER"
204
          | "ZAR"
205
          | "ZMK"
206
          | "ZMW"
207
          | "BTC"
208
          | "XUS";
209
      };
210
      gan?: string;
211
      created_at?: string;
212
      customer_ids?: string[];
213
    };
214
  },
215
) {
216
  const url = new URL(`https://connect.squareup.com/v2/gift-cards`);
217

218
  const response = await fetch(url, {
219
    method: "POST",
220
    headers: {
221
      "Content-Type": "application/json",
222
      Authorization: "Bearer " + auth.token,
223
    },
224
    body: JSON.stringify(body),
225
  });
226
  if (!response.ok) {
227
    const text = await response.text();
228
    throw new Error(`${response.status} ${text}`);
229
  }
230
  return await response.json();
231
}
232