0

Companies

by
Published Oct 17, 2025

Array of companies that your application has been granted with some level of access. Your Application (API Key) can be granted access to one or more companies (clients) accounts that are associated with one or more product lines within Paychex.

Script paychex Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Companies
4
 * Array of companies that your application has been granted with some level of access. Your Application (API Key) can be granted access to one or more companies (clients) accounts that are associated with one or more product lines within Paychex.
5
 */
6
export async function main(auth: RT.Paychex, displayid?: string | undefined) {
7
	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
8
	const url = new URL(`https://api.paychex.com/companies`)
9
	for (const [k, v] of [['displayid', displayid]]) {
10
		if (v !== undefined && v !== '') {
11
			url.searchParams.append(k, v)
12
		}
13
	}
14
	const response = await fetch(url, {
15
		method: 'GET',
16
		headers: {
17
			Authorization: 'Bearer ' + accessToken
18
		},
19
		body: undefined
20
	})
21
	if (!response.ok) {
22
		const text = await response.text()
23
		throw new Error(`${response.status} ${text}`)
24
	}
25
	return await response.json()
26
}
27

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

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

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

47
	const data = await response.json()
48
	return data.access_token
49
}
50