0

Get Custom Report by ID

by
Published Oct 17, 2025

This endpoint provides you with the data of an existing Custom Report.

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
 * Get Custom Report by ID
8
 * This endpoint provides you with the data of an existing Custom Report.
9
 */
10
export async function main(
11
	auth: Personio,
12
	report_id: string,
13
	locale: string | undefined,
14
	page: string | undefined,
15
	limit: string | undefined,
16
	X_Personio_Partner_ID?: string,
17
	X_Personio_App_ID?: string
18
) {
19
	const url = new URL(`https://api.personio.de/v1/company/custom-reports/reports/${report_id}`)
20
	for (const [k, v] of [
21
		['locale', locale],
22
		['page', page],
23
		['limit', limit]
24
	]) {
25
		if (v !== undefined && v !== '' && k !== undefined) {
26
			url.searchParams.append(k, v)
27
		}
28
	}
29
	const response = await fetch(url, {
30
		method: 'GET',
31
		headers: {
32
			...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}),
33
			...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}),
34
			Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
35
		},
36
		body: undefined
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44

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

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

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

66
	const data = await response.json()
67
	return data.access_token
68
}
69