0

List all phone numbers

by
Published Apr 8, 2025

Returns a list of all active phone numbers associated with the given external connection.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * List all phone numbers
7
 * Returns a list of all active phone numbers associated with the given external connection.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	id: string,
12
	page_number_: string | undefined,
13
	page_size_: string | undefined,
14
	filter_phone_number__eq_: string | undefined,
15
	filter_phone_number__contains_: string | undefined,
16
	filter_civic_address_id__eq_: string | undefined,
17
	filter_location_id__eq_: string | undefined
18
) {
19
	const url = new URL(`https://api.telnyx.com/v2/external_connections/${id}/phone_numbers`)
20
	for (const [k, v] of [
21
		['page[number]', page_number_],
22
		['page[size]', page_size_],
23
		['filter[phone_number][eq]', filter_phone_number__eq_],
24
		['filter[phone_number][contains]', filter_phone_number__contains_],
25
		['filter[civic_address_id][eq]', filter_civic_address_id__eq_],
26
		['filter[location_id][eq]', filter_location_id__eq_]
27
	]) {
28
		if (v !== undefined && v !== '' && k !== undefined) {
29
			url.searchParams.append(k, v)
30
		}
31
	}
32
	const response = await fetch(url, {
33
		method: 'GET',
34
		headers: {
35
			Authorization: 'Bearer ' + auth.apiKey
36
		},
37
		body: undefined
38
	})
39
	if (!response.ok) {
40
		const text = await response.text()
41
		throw new Error(`${response.status} ${text}`)
42
	}
43
	return await response.json()
44
}
45