0

Create a Project

by
Published Oct 17, 2025

Creates a project into the company account

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 a Project
8
 * Creates a project into the company account
9
 */
10
export async function main(
11
	auth: Personio,
12
	body: { name: string; active?: false | true },
13
	X_Personio_Partner_ID?: string,
14
	X_Personio_App_ID?: string
15
) {
16
	const url = new URL(`https://api.personio.de/v1/company/attendances/projects`)
17

18
	const response = await fetch(url, {
19
		method: 'POST',
20
		headers: {
21
			...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}),
22
			...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}),
23
			'Content-Type': 'application/json',
24
			Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
25
		},
26
		body: JSON.stringify(body)
27
	})
28
	if (!response.ok) {
29
		const text = await response.text()
30
		throw new Error(`${response.status} ${text}`)
31
	}
32
	return await response.json()
33
}
34

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

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

51
	if (!response.ok) {
52
		const text = await response.text()
53
		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
54
	}
55

56
	const data = await response.json()
57
	return data.access_token
58
}
59