0

Get Absence Balance for Employee

by
Published Oct 17, 2025

Retrieve the absence balance for a specific employee

Script personio Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Personio = {
3
	clientId: string
4
	clientSecret: string
5
}
6
/**
7
 * Get Absence Balance for Employee
8
 * Retrieve the absence balance for a specific employee
9
 */
10
export async function main(
11
	auth: Personio,
12
	employee_id: string,
13
	X_Personio_Partner_ID?: string,
14
	X_Personio_App_ID?: string
15
) {
16
	const url = new URL(
17
		`https://api.personio.de/v1/company/employees/${employee_id}/absences/balance`
18
	)
19

20
	const response = await fetch(url, {
21
		method: 'GET',
22
		headers: {
23
			...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}),
24
			...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}),
25
			Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
26
		},
27
		body: undefined
28
	})
29
	if (!response.ok) {
30
		const text = await response.text()
31
		throw new Error(`${response.status} ${text}`)
32
	}
33
	return await response.json()
34
}
35

36
async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> {
37
	const params = new URLSearchParams({
38
		grant_type: 'client_credentials',
39
		client_id: auth.clientId,
40
		client_secret: auth.clientSecret
41
	})
42

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

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

57
	const data = await response.json()
58
	return data.access_token
59
}
60