0
Create Credential
One script reply has been approved by the moderators Verified

Create a credential

Created by hugo697 86 days ago Viewed 156 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 86 days ago
1
type Certopus = {
2
	apiKey: string
3
}
4

5
export async function main(
6
	resource: Certopus,
7
	payload: {
8
		organisationId: string
9
		eventId: string
10
		categoryId: string
11
		publish: boolean
12
		generate: boolean
13
		email: string
14
		fields: Record<string, any>
15
	}
16
) {
17
	// Destructure the payload object to extract individual properties
18
	const { organisationId, eventId, categoryId, publish, generate, email, fields } = payload
19

20
	const endpoint = `https://api.certopus.com/v1/certificates`
21

22
	// Construct the request body according to the API requirements
23
	const body = {
24
		organisationId,
25
		eventId,
26
		categoryId,
27
		publish,
28
		generate,
29
		recipients: [
30
			{
31
				email,
32
				data: Object.entries(fields).reduce((acc, [key, value]) => {
33
					// Format keys like {Name} to match the expected format
34
					acc[`{${key}}`] = value
35
					return acc
36
				}, {} as Record<string, any>)
37
			}
38
		]
39
	}
40

41
	const response = await fetch(endpoint, {
42
		method: 'POST',
43
		headers: {
44
			accept: 'application/json',
45
			'Content-Type': 'application/json',
46
			'x-api-key': resource.apiKey
47
		},
48
		body: JSON.stringify(body)
49
	})
50

51
	if (!response.ok) {
52
		throw new Error(`HTTP error! status: ${response.status}`)
53
	}
54

55
	const data = await response.json()
56

57
	return data
58
}
59