Edits history of script submission #9039 for ' Create Credential (certopus)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Certopus = {
    	apiKey: string
    }
    
    export async function main(
    	resource: Certopus,
    	payload: {
    		organisationId: string
    		eventId: string
    		categoryId: string
    		publish: boolean
    		generate: boolean
    		email: string
    		fields: Record<string, any>
    	}
    ) {
    	// Destructure the payload object to extract individual properties
    	const { organisationId, eventId, categoryId, publish, generate, email, fields } = payload
    
    	const endpoint = `https://api.certopus.com/v1/certificates`
    
    	// Construct the request body according to the API requirements
    	const body = {
    		organisationId,
    		eventId,
    		categoryId,
    		publish,
    		generate,
    		recipients: [
    			{
    				email,
    				data: Object.entries(fields).reduce((acc, [key, value]) => {
    					// Format keys like {Name} to match the expected format
    					acc[`{${key}}`] = value
    					return acc
    				}, {} as Record<string, any>)
    			}
    		]
    	}
    
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			accept: 'application/json',
    			'Content-Type': 'application/json',
    			'x-api-key': resource.apiKey
    		},
    		body: JSON.stringify(body)
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    
    	return data
    }
    

    Submitted by hugo697 604 days ago