0

Update an Employment.

by
Published Oct 17, 2025

- This endpoint enables the update of an Employment resource. - The endpoint requires the personio:persons:write scope.

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
 * Update an Employment.
8
 * - This endpoint enables the update of an Employment resource.
9
- The endpoint requires the personio:persons:write scope.
10

11
 */
12
export async function main(
13
	auth: Personio,
14
	person_id: string,
15
	employment_id: string,
16
	body: {
17
		supervisor?: { id: string }
18
		office?: { id: string }
19
		org_units?: { type: string; id: string }[]
20
		subcompany?: { id: string }
21
		legal_entity?: { id: string }
22
		position?: { title: string }
23
		status?: 'ACTIVE' | 'INACTIVE' | 'ONBOARDING' | 'LEAVE'
24
		employment_start_date?: string
25
		type?: 'INTERNAL' | 'EXTERNAL'
26
		contract_end_date?: string
27
		probation_end_date?: string
28
		probation_period_length?: number
29
		weekly_working_hours?: number
30
		full_time_weekly_working_hours?: number
31
		cost_centers?: { id: string; weight: number }[]
32
	}
33
) {
34
	const url = new URL(
35
		`https://api.personio.de/v2/persons/${person_id}/employments/${employment_id}`
36
	)
37

38
	const response = await fetch(url, {
39
		method: 'PATCH',
40
		headers: {
41
			'Content-Type': 'application/json',
42
			Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
43
		},
44
		body: JSON.stringify(body)
45
	})
46
	if (!response.ok) {
47
		const text = await response.text()
48
		throw new Error(`${response.status} ${text}`)
49
	}
50
	return await response.text()
51
}
52

53
async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> {
54
	const params = new URLSearchParams({
55
		grant_type: 'client_credentials',
56
		client_id: auth.clientId,
57
		client_secret: auth.clientSecret
58
	})
59

60
	const response = await fetch(tokenUrl, {
61
		method: 'POST',
62
		headers: {
63
			Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
64
			'Content-Type': 'application/x-www-form-urlencoded'
65
		},
66
		body: params.toString()
67
	})
68

69
	if (!response.ok) {
70
		const text = await response.text()
71
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
72
	}
73

74
	const data = await response.json()
75
	return data.access_token
76
}
77
}
78