0

CreateCustomerCard

by
Published Oct 17, 2025

Adds a card on file to an existing customer. As with charges, calls to `CreateCustomerCard` are idempotent. Multiple calls with the same card nonce return the same card record that was created with the provided nonce during the _first_ call.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * CreateCustomerCard
7
 * Adds a card on file to an existing customer.
8

9
As with charges, calls to `CreateCustomerCard` are idempotent. Multiple
10
calls with the same card nonce return the same card record that was created
11
with the provided nonce during the _first_ call.
12
 */
13
export async function main(
14
  auth: Square,
15
  customer_id: string,
16
  body: {
17
    card_nonce: string;
18
    billing_address?: {
19
      address_line_1?: string;
20
      address_line_2?: string;
21
      address_line_3?: string;
22
      locality?: string;
23
      sublocality?: string;
24
      sublocality_2?: string;
25
      sublocality_3?: string;
26
      administrative_district_level_1?: string;
27
      administrative_district_level_2?: string;
28
      administrative_district_level_3?: string;
29
      postal_code?: string;
30
      country?:
31
        | "ZZ"
32
        | "AD"
33
        | "AE"
34
        | "AF"
35
        | "AG"
36
        | "AI"
37
        | "AL"
38
        | "AM"
39
        | "AO"
40
        | "AQ"
41
        | "AR"
42
        | "AS"
43
        | "AT"
44
        | "AU"
45
        | "AW"
46
        | "AX"
47
        | "AZ"
48
        | "BA"
49
        | "BB"
50
        | "BD"
51
        | "BE"
52
        | "BF"
53
        | "BG"
54
        | "BH"
55
        | "BI"
56
        | "BJ"
57
        | "BL"
58
        | "BM"
59
        | "BN"
60
        | "BO"
61
        | "BQ"
62
        | "BR"
63
        | "BS"
64
        | "BT"
65
        | "BV"
66
        | "BW"
67
        | "BY"
68
        | "BZ"
69
        | "CA"
70
        | "CC"
71
        | "CD"
72
        | "CF"
73
        | "CG"
74
        | "CH"
75
        | "CI"
76
        | "CK"
77
        | "CL"
78
        | "CM"
79
        | "CN"
80
        | "CO"
81
        | "CR"
82
        | "CU"
83
        | "CV"
84
        | "CW"
85
        | "CX"
86
        | "CY"
87
        | "CZ"
88
        | "DE"
89
        | "DJ"
90
        | "DK"
91
        | "DM"
92
        | "DO"
93
        | "DZ"
94
        | "EC"
95
        | "EE"
96
        | "EG"
97
        | "EH"
98
        | "ER"
99
        | "ES"
100
        | "ET"
101
        | "FI"
102
        | "FJ"
103
        | "FK"
104
        | "FM"
105
        | "FO"
106
        | "FR"
107
        | "GA"
108
        | "GB"
109
        | "GD"
110
        | "GE"
111
        | "GF"
112
        | "GG"
113
        | "GH"
114
        | "GI"
115
        | "GL"
116
        | "GM"
117
        | "GN"
118
        | "GP"
119
        | "GQ"
120
        | "GR"
121
        | "GS"
122
        | "GT"
123
        | "GU"
124
        | "GW"
125
        | "GY"
126
        | "HK"
127
        | "HM"
128
        | "HN"
129
        | "HR"
130
        | "HT"
131
        | "HU"
132
        | "ID"
133
        | "IE"
134
        | "IL"
135
        | "IM"
136
        | "IN"
137
        | "IO"
138
        | "IQ"
139
        | "IR"
140
        | "IS"
141
        | "IT"
142
        | "JE"
143
        | "JM"
144
        | "JO"
145
        | "JP"
146
        | "KE"
147
        | "KG"
148
        | "KH"
149
        | "KI"
150
        | "KM"
151
        | "KN"
152
        | "KP"
153
        | "KR"
154
        | "KW"
155
        | "KY"
156
        | "KZ"
157
        | "LA"
158
        | "LB"
159
        | "LC"
160
        | "LI"
161
        | "LK"
162
        | "LR"
163
        | "LS"
164
        | "LT"
165
        | "LU"
166
        | "LV"
167
        | "LY"
168
        | "MA"
169
        | "MC"
170
        | "MD"
171
        | "ME"
172
        | "MF"
173
        | "MG"
174
        | "MH"
175
        | "MK"
176
        | "ML"
177
        | "MM"
178
        | "MN"
179
        | "MO"
180
        | "MP"
181
        | "MQ"
182
        | "MR"
183
        | "MS"
184
        | "MT"
185
        | "MU"
186
        | "MV"
187
        | "MW"
188
        | "MX"
189
        | "MY"
190
        | "MZ"
191
        | "NA"
192
        | "NC"
193
        | "NE"
194
        | "NF"
195
        | "NG"
196
        | "NI"
197
        | "NL"
198
        | "NO"
199
        | "NP"
200
        | "NR"
201
        | "NU"
202
        | "NZ"
203
        | "OM"
204
        | "PA"
205
        | "PE"
206
        | "PF"
207
        | "PG"
208
        | "PH"
209
        | "PK"
210
        | "PL"
211
        | "PM"
212
        | "PN"
213
        | "PR"
214
        | "PS"
215
        | "PT"
216
        | "PW"
217
        | "PY"
218
        | "QA"
219
        | "RE"
220
        | "RO"
221
        | "RS"
222
        | "RU"
223
        | "RW"
224
        | "SA"
225
        | "SB"
226
        | "SC"
227
        | "SD"
228
        | "SE"
229
        | "SG"
230
        | "SH"
231
        | "SI"
232
        | "SJ"
233
        | "SK"
234
        | "SL"
235
        | "SM"
236
        | "SN"
237
        | "SO"
238
        | "SR"
239
        | "SS"
240
        | "ST"
241
        | "SV"
242
        | "SX"
243
        | "SY"
244
        | "SZ"
245
        | "TC"
246
        | "TD"
247
        | "TF"
248
        | "TG"
249
        | "TH"
250
        | "TJ"
251
        | "TK"
252
        | "TL"
253
        | "TM"
254
        | "TN"
255
        | "TO"
256
        | "TR"
257
        | "TT"
258
        | "TV"
259
        | "TW"
260
        | "TZ"
261
        | "UA"
262
        | "UG"
263
        | "UM"
264
        | "US"
265
        | "UY"
266
        | "UZ"
267
        | "VA"
268
        | "VC"
269
        | "VE"
270
        | "VG"
271
        | "VI"
272
        | "VN"
273
        | "VU"
274
        | "WF"
275
        | "WS"
276
        | "YE"
277
        | "YT"
278
        | "ZA"
279
        | "ZM"
280
        | "ZW";
281
      first_name?: string;
282
      last_name?: string;
283
    };
284
    cardholder_name?: string;
285
    verification_token?: string;
286
  },
287
) {
288
  const url = new URL(
289
    `https://connect.squareup.com/v2/customers/${customer_id}/cards`,
290
  );
291

292
  const response = await fetch(url, {
293
    method: "POST",
294
    headers: {
295
      "Content-Type": "application/json",
296
      Authorization: "Bearer " + auth.token,
297
    },
298
    body: JSON.stringify(body),
299
  });
300
  if (!response.ok) {
301
    const text = await response.text();
302
    throw new Error(`${response.status} ${text}`);
303
  }
304
  return await response.json();
305
}
306