0

Create new Recurring Employee Deduction

by
Published Oct 17, 2025

**Summary Description** This function will allow the API user to create a new deduction on the employee record. Garnishments cannot be created using the Deduction endpoints.

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
 * Create new Recurring Employee Deduction
8
 * **Summary Description**
9

10
 This function will allow the API user to create a new deduction on the employee record.
11
Garnishments cannot be created using the Deduction endpoints.
12
 */
13
export async function main(
14
	auth: Paylocity,
15
	companyId: string,
16
	employeeId: string,
17
	body: {
18
		effectiveFrom?: string
19
		effectiveTo?: string
20
		calculationCode?: string
21
		rate?: number
22
		frequency?: string
23
		agency?: string
24
		arrear?: number
25
		miscellaneousInfo?: string
26
		note?: string
27
		selfInsured?: false | true
28
		priority?: number
29
		loan401K?: {
30
			loanNumber?: string
31
			issueDate?: string
32
			firstPaymentDate?: string
33
		}
34
		costCenters?: { level?: number; code?: string }[]
35
		limits?: {
36
			goal?: number
37
			paidToDate?: number
38
			payPeriodMinimum?: number
39
			payPeriodMaximum?: number
40
			annualMaximum?: number
41
		}
42
		code?: string
43
	}
44
) {
45
	const url = new URL(
46
		`https://dc1prodgwext.paylocity.com/apiHub/payroll/v1/companies/${companyId}/employees/${employeeId}/deductions`
47
	)
48

49
	const response = await fetch(url, {
50
		method: 'POST',
51
		headers: {
52
			'Content-Type': 'application/json',
53
			Authorization:
54
				'Bearer ' +
55
				(await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token'))
56
		},
57
		body: JSON.stringify(body)
58
	})
59
	if (!response.ok) {
60
		const text = await response.text()
61
		throw new Error(`${response.status} ${text}`)
62
	}
63
	return await response.text()
64
}
65

66
async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> {
67
	const params = new URLSearchParams({
68
		grant_type: 'client_credentials',
69
		client_id: auth.clientId,
70
		client_secret: auth.clientSecret
71
	})
72

73
	const response = await fetch(tokenUrl, {
74
		method: 'POST',
75
		headers: {
76
			Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
77
			'Content-Type': 'application/x-www-form-urlencoded'
78
		},
79
		body: params.toString()
80
	})
81

82
	if (!response.ok) {
83
		const text = await response.text()
84
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
85
	}
86

87
	const data = await response.json()
88
	return data.access_token
89
}
90