Edits history of script submission #13278 for ' Get usage report sync (telnyx)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Telnyx = {
    	apiKey: string
    }
    /**
     * Get usage report sync
     * Generate and fetch messaging usage report synchronously. This endpoint will both generate and fetch the messaging report over a specified time period. No polling is necessary but the response may take up to a couple of minutes.
     */
    export async function main(
    	auth: Telnyx,
    	start_date: string | undefined,
    	end_date: string | undefined,
    	aggregation_type: 'NO_AGGREGATION' | 'PROFILE' | 'TAGS' | undefined,
    	profiles: string | undefined
    ) {
    	const url = new URL(`https://api.telnyx.com/v2/reports/mdr_usage_reports/sync`)
    	for (const [k, v] of [
    		['start_date', start_date],
    		['end_date', end_date],
    		['aggregation_type', aggregation_type],
    		['profiles', profiles]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: 'Bearer ' + auth.apiKey
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 428 days ago