0

Get paginated mdrs

by
Published Apr 8, 2025

Fetch all Mdr records

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 paginated mdrs
7
 * Fetch all Mdr records
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	start_date: string | undefined,
12
	end_date: string | undefined,
13
	id: string | undefined,
14
	direction: 'INBOUND' | 'OUTBOUND' | undefined,
15
	profile: string | undefined,
16
	cld: string | undefined,
17
	cli: string | undefined,
18
	status:
19
		| 'GW_TIMEOUT'
20
		| 'DELIVERED'
21
		| 'DLR_UNCONFIRMED'
22
		| 'DLR_TIMEOUT'
23
		| 'RECEIVED'
24
		| 'GW_REJECT'
25
		| 'FAILED'
26
		| undefined,
27
	message_type: 'SMS' | 'MMS' | undefined
28
) {
29
	const url = new URL(`https://api.telnyx.com/v2/reports/mdrs`)
30
	for (const [k, v] of [
31
		['start_date', start_date],
32
		['end_date', end_date],
33
		['id', id],
34
		['direction', direction],
35
		['profile', profile],
36
		['cld', cld],
37
		['cli', cli],
38
		['status', status],
39
		['message_type', message_type]
40
	]) {
41
		if (v !== undefined && v !== '' && k !== undefined) {
42
			url.searchParams.append(k, v)
43
		}
44
	}
45
	const response = await fetch(url, {
46
		method: 'GET',
47
		headers: {
48
			Authorization: 'Bearer ' + auth.apiKey
49
		},
50
		body: undefined
51
	})
52
	if (!response.ok) {
53
		const text = await response.text()
54
		throw new Error(`${response.status} ${text}`)
55
	}
56
	return await response.json()
57
}
58