0

Get usage report sync

by
Published Apr 8, 2025

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.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Get usage report sync
7
 * 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.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	start_date: string | undefined,
12
	end_date: string | undefined,
13
	aggregation_type: 'NO_AGGREGATION' | 'PROFILE' | 'TAGS' | undefined,
14
	profiles: string | undefined
15
) {
16
	const url = new URL(`https://api.telnyx.com/v2/reports/mdr_usage_reports/sync`)
17
	for (const [k, v] of [
18
		['start_date', start_date],
19
		['end_date', end_date],
20
		['aggregation_type', aggregation_type],
21
		['profiles', profiles]
22
	]) {
23
		if (v !== undefined && v !== '' && k !== undefined) {
24
			url.searchParams.append(k, v)
25
		}
26
	}
27
	const response = await fetch(url, {
28
		method: 'GET',
29
		headers: {
30
			Authorization: 'Bearer ' + auth.apiKey
31
		},
32
		body: undefined
33
	})
34
	if (!response.ok) {
35
		const text = await response.text()
36
		throw new Error(`${response.status} ${text}`)
37
	}
38
	return await response.json()
39
}
40