0

Worker Contacts

by
Published Oct 17, 2025

Update a worker contact.

Script paychex Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Worker Contacts
4
 * Update a worker contact.
5
 */
6
export async function main(auth: RT.Paychex, workerId: string, contactId: string, body: Body) {
7
	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
8
	const url = new URL(`https://api.paychex.com/workers/${workerId}/contacts/${contactId}`)
9

10
	const response = await fetch(url, {
11
		method: 'PATCH',
12
		headers: {
13
			'Content-Type': 'application/json',
14
			Authorization: 'Bearer ' + accessToken
15
		},
16
		body: JSON.stringify(body)
17
	})
18
	if (!response.ok) {
19
		const text = await response.text()
20
		throw new Error(`${response.status} ${text}`)
21
	}
22
	return await response.json()
23
}
24

25
async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
26
	const params = new URLSearchParams({
27
		grant_type: 'client_credentials'
28
	})
29

30
	const response = await fetch(tokenUrl, {
31
		method: 'POST',
32
		headers: {
33
			Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
34
			'Content-Type': 'application/x-www-form-urlencoded'
35
		},
36
		body: params.toString()
37
	})
38

39
	if (!response.ok) {
40
		const text = await response.text()
41
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
42
	}
43

44
	const data = await response.json()
45
	return data.access_token
46
}
47

48
/* eslint-disable */
49
/**
50
 * This file was automatically generated by json-schema-to-typescript.
51
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
52
 * and run json-schema-to-typescript to regenerate this file.
53
 */
54

55
export interface Body {
56
	/**
57
	 * The ID for the workers specific contact.
58
	 */
59
	contactId?: string
60
	/**
61
	 * The type of this worker contact. Required for POST contacts.
62
	 */
63
	contactType?: {
64
		/**
65
		 * The ID for specific type of contact. Required for POST contacts.
66
		 */
67
		contactTypeId?: string
68
		/**
69
		 * Enum:  "EMERGENCY_CONTACT"
70
		 *
71
		 * The name for specific type of contact.
72
		 */
73
		contactTypeName?: string
74
		[k: string]: unknown
75
	}
76
	/**
77
	 * The relationship for this worker contract. Required for POST contacts.
78
	 */
79
	relationship?: {
80
		/**
81
		 * The ID for the workers relationship to contact.
82
		 */
83
		relationshipId?: string
84
		/**
85
		 * The ID for the workers specific relation.
86
		 */
87
		relationWeid?: string
88
		/**
89
		 * The relationship type for this worker contact. Required for POST person contacts.
90
		 */
91
		relationshipType?: {
92
			/**
93
			 * ID of worker specific type of relationship with the contact. Required for POST contacts.
94
			 */
95
			relationshipTypeId?: string
96
			/**
97
			 * The name of worker specific type of relationship with the contact.
98
			 *
99
			 * Enum: "Spouse", "Parent", "Sibling", "Child",  "Friend", "Domestic Partner", "Other"
100
			 */
101
			relationshipTypeName?: string
102
			[k: string]: unknown
103
		}
104
		/**
105
		 * Where to indicate if the contact is selected as Primary, where true = Primary contact and false = not Primary contact.
106
		 */
107
		primary?: boolean
108
		/**
109
		 * The person associated with this relationship. The relationship must have either a person or entity.
110
		 */
111
		person?: {
112
			/**
113
			 * Information about the workers name.
114
			 */
115
			name?: {
116
				/**
117
				 * The family or last name of a worker.
118
				 */
119
				familyName?: string
120
				/**
121
				 * A subordinate given name, or initial representing that name, of a worker. <br />NOTE:  Please send in just a single character initial for middleName.  <br />If sending in more than one character the name will be truncated the first letter of the middleName. <br /> A full middle name can be entered directly in Paychex Flex only at this time.
122
				 */
123
				middleName?: string
124
				/**
125
				 * The given or first name of a worker. For an independent contractor that is a company rather than an individual, the name of the company.
126
				 */
127
				givenName?: string
128
				/**
129
				 * The first name that the worker prefers to go by
130
				 */
131
				preferredName?: string
132
				/**
133
				 * The last name that the worker prefers to go by
134
				 */
135
				preferredLastName?: string
136
				/**
137
				 * The pronoun that the worker prefers to go by. This data field cannot be POSTED.
138
				 */
139
				pronoun?: string
140
				/**
141
				 * A qualifier to the name of a worker, indicating generation.
142
				 */
143
				qualificationAffixCode?: string
144
				/**
145
				 * A personal title that comes before an individual's name in a formal address (such as Dr., Prof., Rev., Mr.).
146
				 */
147
				titleAffixCode?: string
148
				[k: string]: unknown
149
			}
150
			/**
151
			 * The set of communications for this contact. Required for POST contacts.
152
			 */
153
			communication?: {
154
				/**
155
				 * The telecom communications for this contact.
156
				 */
157
				telecom?: {
158
					/**
159
					 * The ID for the workers relationship to contact.
160
					 */
161
					communicationId?: string
162
					/**
163
					 * The country code. Required for POST contacts.
164
					 */
165
					dialCountry?: string
166
					/**
167
					 * The area code. Required for POST contacts.
168
					 */
169
					dialArea?: string
170
					/**
171
					 * The phone number. Required for POST contacts.
172
					 */
173
					dialNumber?: string
174
					/**
175
					 * Enum: "PHONE", "MOBILE_PHONE"
176
					 *
177
					 * Indicates whether or not the number is for a mobile device. Required for POST contacts.
178
					 */
179
					type?: string
180
					/**
181
					 * Enum: "PERSONAL", "BUSINESS"
182
					 *
183
					 * A code classifying a designated use associated with a contact method. For example, whether a telephone or email address is one for work communications or one primarily for personal use. This data field cannot be PATCHED. Required for POST contacts.
184
					 */
185
					usageType?: string
186
					/**
187
					 * The phone extension.
188
					 */
189
					extension?: string
190
					[k: string]: unknown
191
				}[]
192
				/**
193
				 * The postal communications for this contact.
194
				 */
195
				postal?: {
196
					/**
197
					 * The ID for the workers relationship to contact.
198
					 */
199
					communicationId?: string
200
					/**
201
					 * The street address line one. Required for street address. Required for POST contacts.
202
					 */
203
					streetLineOne?: string
204
					/**
205
					 * The street address line two.
206
					 */
207
					streetLineTwo?: string
208
					/**
209
					 * The postal office box. Required for PO Box with POST contacts.
210
					 */
211
					postOfficeBox?: string
212
					/**
213
					 * The city name. Required for POST contacts.
214
					 */
215
					city?: string
216
					/**
217
					 * The zip-code. Required for POST contacts.
218
					 */
219
					postalCode?: string
220
					/**
221
					 * The state code (ISO 3166 subdivision code). Required for POST contacts.
222
					 */
223
					countrySubdivisionCode?: string
224
					/**
225
					 * The country code (ISO 3166 alpha-2). Required for POST contacts.
226
					 */
227
					countryCode?: string
228
					[k: string]: unknown
229
				}[]
230
				/**
231
				 * The email communications for this contact.
232
				 */
233
				email?: {
234
					/**
235
					 * The ID for this email communication.
236
					 */
237
					communicationId?: string
238
					/**
239
					 * The mailto address as specified in RFC2368. Required for POST contacts.
240
					 */
241
					uri?: string
242
					/**
243
					 * Enum: "PERSONAL", "BUSINESS"
244
					 *
245
					 * A code classifying a designated use associated with a contact method. For example, whether a telephone or email address is one for work communications or one primarily for personal use. This data field cannot be PATCHED. Required for POST contacts.
246
					 */
247
					usageType?: string
248
					[k: string]: unknown
249
				}[]
250
				[k: string]: unknown
251
			}
252
			[k: string]: unknown
253
		}
254
		/**
255
		 * The organization associated with this relationship. The relationship must have either a person or entity.
256
		 */
257
		entity?: {
258
			/**
259
			 * The ID for this contact.
260
			 */
261
			entityId?: string
262
			/**
263
			 * The entity name. Required for POST contacts.
264
			 */
265
			entityName?: string
266
			/**
267
			 * The set of communications for this contact. Required for POST contacts.
268
			 */
269
			communication?: {
270
				/**
271
				 * The telecom communications for this contact.
272
				 */
273
				telecom?: {
274
					/**
275
					 * The ID for the workers relationship to contact.
276
					 */
277
					communicationId?: string
278
					/**
279
					 * The country code. Required for POST contacts.
280
					 */
281
					dialCountry?: string
282
					/**
283
					 * The area code. Required for POST contacts.
284
					 */
285
					dialArea?: string
286
					/**
287
					 * The phone number. Required for POST contacts.
288
					 */
289
					dialNumber?: string
290
					/**
291
					 * Enum: "PHONE", "MOBILE_PHONE"
292
					 *
293
					 * Indicates whether or not the number is for a mobile device. Required for POST contacts.
294
					 */
295
					type?: string
296
					/**
297
					 * Enum: "PERSONAL", "BUSINESS"
298
					 *
299
					 * A code classifying a designated use associated with a contact method. For example, whether a telephone or email address is one for work communications or one primarily for personal use. This data field cannot be PATCHED. Required for POST contacts.
300
					 */
301
					usageType?: string
302
					/**
303
					 * The phone extension.
304
					 */
305
					extension?: string
306
					[k: string]: unknown
307
				}
308
				/**
309
				 * The postal communications for this contact.
310
				 */
311
				postal?: {
312
					/**
313
					 * The ID for the workers relationship to contact.
314
					 */
315
					communicationId?: string
316
					/**
317
					 * The street address line one. Required for street address. Required for POST contacts.
318
					 */
319
					streetLineOne?: string
320
					/**
321
					 * The street address line two.
322
					 */
323
					streetLineTwo?: string
324
					/**
325
					 * The postal office box. Required for PO Box with POST contacts.
326
					 */
327
					postOfficeBox?: string
328
					/**
329
					 * The city name. Required for POST contacts.
330
					 */
331
					city?: string
332
					/**
333
					 * The zip-code. Required for POST contacts.
334
					 */
335
					postalCode?: string
336
					/**
337
					 * The state code (ISO 3166 subdivision code). Required for POST contacts.
338
					 */
339
					countrySubdivisionCode?: string
340
					/**
341
					 * The country code (ISO 3166 alpha-2). Required for POST contacts.
342
					 */
343
					countryCode?: string
344
					[k: string]: unknown
345
				}
346
				/**
347
				 * The email communications for this contact.
348
				 */
349
				email?: {
350
					/**
351
					 * The ID for this email communication.
352
					 */
353
					communicationId?: string
354
					/**
355
					 * The mailto address as specified in RFC2368. Required for POST contacts.
356
					 */
357
					uri?: string
358
					/**
359
					 * Enum: "PERSONAL", "BUSINESS"
360
					 *
361
					 * A code classifying a designated use associated with a contact method. For example, whether a telephone or email address is one for work communications or one primarily for personal use. This data field cannot be PATCHED. Required for POST contacts.
362
					 */
363
					usageType?: string
364
					[k: string]: unknown
365
				}
366
				[k: string]: unknown
367
			}
368
			[k: string]: unknown
369
		}
370
		[k: string]: unknown
371
	}
372
	[k: string]: unknown
373
}
374