//native
type Personio = {
clientId: string
clientSecret: string
}
/**
* Creating applications in Personio
* Allows you to create applications in Personio.
*/
export async function main(
auth: Personio,
X_Company_ID: string,
body: {
first_name: string
last_name: string
email: string
job_position_id: number
recruiting_channel_id?: number
external_posting_id?: string
message?: string
application_date?: string
phase?: { type: 'system' | 'custom'; id: string | number }
tags?: string[]
files?: {
uuid: string
original_filename: string
category:
| 'cv'
| 'cover-letter'
| 'employment-reference'
| 'certificate'
| 'work-sample'
| 'other'
}[]
attributes?: { id: string; value: string }[]
},
X_Personio_Partner_ID?: string,
X_Personio_App_ID?: string
) {
const url = new URL(`https://api.personio.de/v1/recruiting/applications`)
const response = await fetch(url, {
method: 'POST',
headers: {
...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}),
...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}),
'X-Company-ID': X_Company_ID,
'Content-Type': 'application/json',
Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
},
body: JSON.stringify(body)
})
if (!response.ok) {
const text = await response.text()
throw new Error(`${response.status} ${text}`)
}
return await response.text()
}
async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> {
const params = new URLSearchParams({
grant_type: 'client_credentials',
client_id: auth.clientId,
client_secret: auth.clientSecret
})
const response = await fetch(tokenUrl, {
method: 'POST',
headers: {
Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params.toString()
})
if (!response.ok) {
const text = await response.text()
throw new Error(`OAuth token request failed: ${response.status} ${text}`)
}
const data = await response.json()
return data.access_token
}
}
Submitted by hugo697 235 days ago