0

Retrieve an employment.

by
Published Oct 17, 2025

Retrieves an employment for the provided 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
 * Retrieve an employment.
8
 * Retrieves an employment for the provided ID.
9
 */
10
export async function main(auth: Personio, person_id: string, id: string) {
11
	const url = new URL(`https://api.personio.de/v2/persons/${person_id}/employments/${id}`)
12

13
	const response = await fetch(url, {
14
		method: 'GET',
15
		headers: {
16
			Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
17
		},
18
		body: undefined
19
	})
20
	if (!response.ok) {
21
		const text = await response.text()
22
		throw new Error(`${response.status} ${text}`)
23
	}
24
	return await response.json()
25
}
26

27
async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> {
28
	const params = new URLSearchParams({
29
		grant_type: 'client_credentials',
30
		client_id: auth.clientId,
31
		client_secret: auth.clientSecret
32
	})
33

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

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

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