0

List employments of a given Person.

by
Published Oct 17, 2025

Returns a list of employments of a given person. The employments are returned in sorted order, with the most recent employments appearing first.

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
 * List employments of a given Person.
8
 * Returns a list of employments of a given person. The employments are returned in sorted order, with the most recent employments appearing first.
9
 */
10
export async function main(
11
	auth: Personio,
12
	person_id: string,
13
	limit: string | undefined,
14
	cursor: string | undefined,
15
	id: string | undefined,
16
	updated_at: string | undefined,
17
	updated_at_gt: string | undefined,
18
	updated_at_lt: string | undefined
19
) {
20
	const url = new URL(`https://api.personio.de/v2/persons/${person_id}/employments`)
21
	for (const [k, v] of [
22
		['limit', limit],
23
		['cursor', cursor],
24
		['id', id],
25
		['updated_at', updated_at],
26
		['updated_at.gt', updated_at_gt],
27
		['updated_at.lt', updated_at_lt]
28
	]) {
29
		if (v !== undefined && v !== '' && k !== undefined) {
30
			url.searchParams.append(k, v)
31
		}
32
	}
33
	const response = await fetch(url, {
34
		method: 'GET',
35
		headers: {
36
			Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
37
		},
38
		body: undefined
39
	})
40
	if (!response.ok) {
41
		const text = await response.text()
42
		throw new Error(`${response.status} ${text}`)
43
	}
44
	return await response.json()
45
}
46

47
async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> {
48
	const params = new URLSearchParams({
49
		grant_type: 'client_credentials',
50
		client_id: auth.clientId,
51
		client_secret: auth.clientSecret
52
	})
53

54
	const response = await fetch(tokenUrl, {
55
		method: 'POST',
56
		headers: {
57
			Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
58
			'Content-Type': 'application/x-www-form-urlencoded'
59
		},
60
		body: params.toString()
61
	})
62

63
	if (!response.ok) {
64
		const text = await response.text()
65
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
66
	}
67

68
	const data = await response.json()
69
	return data.access_token
70
}
71