0

Retrieves an absence period by ID.

by
Published Oct 17, 2025

Retrieves an absence period by ID.

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
 * Retrieves an absence period by ID.
8
 * Retrieves an absence period by ID.
9
 */
10
export async function main(auth: Personio, id: string, Beta: string) {
11
	const url = new URL(`https://api.personio.de/v2/absence-periods/${id}`)
12

13
	const response = await fetch(url, {
14
		method: 'GET',
15
		headers: {
16
			Beta: Beta,
17
			Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
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: Personio, tokenUrl: string): Promise<string> {
29
	const params = new URLSearchParams({
30
		grant_type: 'client_credentials',
31
		client_id: auth.clientId,
32
		client_secret: auth.clientSecret
33
	})
34

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

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

49
	const data = await response.json()
50
	return data.access_token
51
}
52