0

Get All Company Codes

by
Published Oct 17, 2025

Get All Company Codes for the selected company and resource.

Script paylocity Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Paylocity = {
3
	clientId: string
4
	clientSecret: string
5
}
6
/**
7
 * Get All Company Codes
8
 * Get All Company Codes for the selected company and resource.
9
 */
10
export async function main(auth: Paylocity, companyId: string, codeResource: string) {
11
	const url = new URL(
12
		`https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/codes/${codeResource}`
13
	)
14

15
	const response = await fetch(url, {
16
		method: 'GET',
17
		headers: {
18
			Authorization:
19
				'Bearer ' +
20
				(await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token'))
21
		},
22
		body: undefined
23
	})
24
	if (!response.ok) {
25
		const text = await response.text()
26
		throw new Error(`${response.status} ${text}`)
27
	}
28
	return await response.json()
29
}
30

31
async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> {
32
	const params = new URLSearchParams({
33
		grant_type: 'client_credentials',
34
		client_id: auth.clientId,
35
		client_secret: auth.clientSecret
36
	})
37

38
	const response = await fetch(tokenUrl, {
39
		method: 'POST',
40
		headers: {
41
			Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
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