0

Company Organization

by
Published Oct 17, 2025

Information about a single Organization.

Script paychex Verified

The script

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

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

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

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

54
	const data = await response.json()
55
	return data.access_token
56
}
57