0

Worker Time Off

by
Published Oct 17, 2025

Array of time off balance(s) a worker has for each policy type of time off, for that worker. NOTE: This data is only available if the client has the Time Off Accrual product (This is not related to the Flex Time product which has it is own dev portal for those APIs).

Script paychex Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Worker Time Off
4
 * Array of time off balance(s) a worker has for each policy type of time off, for that worker. NOTE: This data is only available if the client has the Time Off Accrual product (This is not related to the Flex Time product which has it is own dev portal for those APIs).
5
 */
6
export async function main(auth: RT.Paychex, workerId: string) {
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}/timeoff`)
9

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

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

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

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

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