0

List all requirements

by
Published Apr 8, 2025

List all requirements with filtering, sorting, and pagination

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 requirements
7
 * List all requirements with filtering, sorting, and pagination
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	filter_country_code_: string | undefined,
12
	filter_phone_number_type_: 'local' | 'national' | 'toll-free' | undefined,
13
	filter_action_: 'ordering' | 'porting' | undefined,
14
	sort__: 'action' | 'country_code' | 'locality' | 'phone_number_type' | undefined,
15
	page_number_: string | undefined,
16
	page_size_: string | undefined
17
) {
18
	const url = new URL(`https://api.telnyx.com/v2/requirements`)
19
	for (const [k, v] of [
20
		['filter[country_code]', filter_country_code_],
21
		['filter[phone_number_type]', filter_phone_number_type_],
22
		['filter[action]', filter_action_],
23
		['sort[]', sort__],
24
		['page[number]', page_number_],
25
		['page[size]', page_size_]
26
	]) {
27
		if (v !== undefined && v !== '' && k !== undefined) {
28
			url.searchParams.append(k, v)
29
		}
30
	}
31
	const response = await fetch(url, {
32
		method: 'GET',
33
		headers: {
34
			Authorization: 'Bearer ' + auth.apiKey
35
		},
36
		body: undefined
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44