0

Create Compensation.

by
Published Oct 17, 2025

Creates a compensations for people for an authorized company. Compensations can be created including one time, recurring, fixed, and hourly compensations. Bonuses are not supported.

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
 * Create Compensation.
8
 * Creates a compensations for people for an authorized company. Compensations can be created including one time, recurring, fixed, and hourly compensations. Bonuses are not supported.
9

10
 */
11
export async function main(
12
	auth: Personio,
13
	body: {
14
		person: { id?: string }
15
		type: { id?: string }
16
		value: number
17
		effective_from: string
18
		interval?: 'MONTHLY' | 'QUARTERLY' | 'HALF-YEARLY' | 'YEARLY'
19
		comment?: string
20
	}
21
) {
22
	const url = new URL(`https://api.personio.de/v2/compensations`)
23

24
	const response = await fetch(url, {
25
		method: 'POST',
26
		headers: {
27
			'Content-Type': 'application/json',
28
			Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
29
		},
30
		body: JSON.stringify(body)
31
	})
32
	if (!response.ok) {
33
		const text = await response.text()
34
		throw new Error(`${response.status} ${text}`)
35
	}
36
	return await response.json()
37
}
38

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

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

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

60
	const data = await response.json()
61
	return data.access_token
62
}
63