0

Fetch multiple call resources

by
Published Apr 8, 2025

Returns multiple call resouces for an account. This endpoint is eventually consistent.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Fetch multiple call resources
7
 * Returns multiple call resouces for an account. This endpoint is eventually consistent.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	account_sid: string,
12
	Page: string | undefined,
13
	page_size_: string | undefined,
14
	PageToken: string | undefined,
15
	To: string | undefined,
16
	From: string | undefined,
17
	Status: 'canceled' | 'completed' | 'failed' | 'busy' | 'no-answer' | undefined,
18
	StartTime: string | undefined,
19
	StartTime$gt: string | undefined,
20
	StartTime$lt: string | undefined,
21
	EndTime: string | undefined,
22
	EndTime$gt: string | undefined,
23
	EndTime$lt: string | undefined
24
) {
25
	const url = new URL(`https://api.telnyx.com/v2/texml/Accounts/${account_sid}/Calls`)
26
	for (const [k, v] of [
27
		['Page', Page],
28
		['page[size]', page_size_],
29
		['PageToken', PageToken],
30
		['To', To],
31
		['From', From],
32
		['Status', Status],
33
		['StartTime', StartTime],
34
		['StartTime>', StartTime$gt],
35
		['StartTime<', StartTime$lt],
36
		['EndTime', EndTime],
37
		['EndTime>', EndTime$gt],
38
		['EndTime<', EndTime$lt]
39
	]) {
40
		if (v !== undefined && v !== '' && k !== undefined) {
41
			url.searchParams.append(k, v)
42
		}
43
	}
44
	const response = await fetch(url, {
45
		method: 'GET',
46
		headers: {
47
			Authorization: 'Bearer ' + auth.apiKey
48
		},
49
		body: undefined
50
	})
51
	if (!response.ok) {
52
		const text = await response.text()
53
		throw new Error(`${response.status} ${text}`)
54
	}
55
	return await response.json()
56
}
57