Edits history of script submission #8995 for ' Get Contact from ID (freshdesk)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Freshdesk = {
    	apiKey: string
    	baseUrl: string
    }
    
    export async function main(resource: Freshdesk, contactId: string) {
    	// Remove trailing slash from base URL
    	const baseUrl = resource.baseUrl.replace(/\/$/, '')
    
    	// Construct the URL for the API request
    	const url = `${baseUrl}/api/v2/contacts/${contactId}`
    
    	// Base64 encode the API key with a colon and 'X'
    	const encodedKey = btoa(`${resource.apiKey}:X`)
    
    	// Make the API request
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: `Basic ${encodedKey}`,
    			'Content-Type': 'application/json'
    		}
    	})
    
    	// Handle the response
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	// Parse and return the response data
    	const data = await response.json()
    
    	return data
    }
    

    Submitted by hugo697 652 days ago