0

Company Location

by
Published Oct 17, 2025

Information about a single Location.

Script paychex Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Company Location
4
 * Information about a single Location.
5
 */
6
export async function main(
7
	auth: RT.Paychex,
8
	companyId: string,
9
	locationId: string,
10
	asof?: string | undefined
11
) {
12
	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
13
	const url = new URL(`https://api.paychex.com/companies/${companyId}/locations/${locationId}`)
14
	for (const [k, v] of [['asof', asof]]) {
15
		if (v !== undefined && v !== '') {
16
			url.searchParams.append(k, v)
17
		}
18
	}
19
	const response = await fetch(url, {
20
		method: 'GET',
21
		headers: {
22
			Authorization: 'Bearer ' + accessToken
23
		},
24
		body: undefined
25
	})
26
	if (!response.ok) {
27
		const text = await response.text()
28
		throw new Error(`${response.status} ${text}`)
29
	}
30
	return await response.json()
31
}
32

33
async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
34
	const params = new URLSearchParams({
35
		grant_type: 'client_credentials'
36
	})
37

38
	const response = await fetch(tokenUrl, {
39
		method: 'POST',
40
		headers: {
41
			Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
42
			'Content-Type': 'application/x-www-form-urlencoded'
43
		},
44
		body: params.toString()
45
	})
46

47
	if (!response.ok) {
48
		const text = await response.text()
49
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
50
	}
51

52
	const data = await response.json()
53
	return data.access_token
54
}
55