//native
type Telnyx = {
apiKey: string
}
/**
* List available phone numbers
*
*/
export async function main(
auth: Telnyx,
filter_phone_number__starts_with_: string | undefined,
filter_phone_number__ends_with_: string | undefined,
filter_phone_number__contains_: string | undefined,
filter_locality_: string | undefined,
filter_administrative_area_: string | undefined,
filter_country_code_: string | undefined,
filter_national_destination_code_: string | undefined,
filter_rate_center_: string | undefined,
filter_phone_number_type_:
| 'local'
| 'toll_free'
| 'mobile'
| 'national'
| 'shared_cost'
| undefined,
filter_features_: string | undefined,
filter_limit_: string | undefined,
filter_best_effort_: string | undefined,
filter_quickship_: string | undefined,
filter_reservable_: string | undefined,
filter_exclude_held_numbers_: string | undefined
) {
const url = new URL(`https://api.telnyx.com/v2/available_phone_numbers`)
for (const [k, v] of [
['filter[phone_number][starts_with]', filter_phone_number__starts_with_],
['filter[phone_number][ends_with]', filter_phone_number__ends_with_],
['filter[phone_number][contains]', filter_phone_number__contains_],
['filter[locality]', filter_locality_],
['filter[administrative_area]', filter_administrative_area_],
['filter[country_code]', filter_country_code_],
['filter[national_destination_code]', filter_national_destination_code_],
['filter[rate_center]', filter_rate_center_],
['filter[phone_number_type]', filter_phone_number_type_],
['filter[features]', filter_features_],
['filter[limit]', filter_limit_],
['filter[best_effort]', filter_best_effort_],
['filter[quickship]', filter_quickship_],
['filter[reservable]', filter_reservable_],
['filter[exclude_held_numbers]', filter_exclude_held_numbers_]
]) {
if (v !== undefined && v !== '' && k !== undefined) {
url.searchParams.append(k, v)
}
}
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization: 'Bearer ' + auth.apiKey
},
body: undefined
})
if (!response.ok) {
const text = await response.text()
throw new Error(`${response.status} ${text}`)
}
return await response.json()
}
Submitted by hugo697 428 days ago