$npx -y skills add team-telnyx/ai --skill telnyx-numbers-services-javascriptConfigure voicemail, voice channels, and emergency (E911) services for your phone numbers. This skill provides JavaScript SDK examples.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Numbers Services - JavaScript |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | npm install telnyx |
| 9 | ``` |
| 10 | |
| 11 | ## Setup |
| 12 | |
| 13 | ```javascript |
| 14 | import Telnyx from 'telnyx'; |
| 15 | |
| 16 | const client = new Telnyx({ |
| 17 | apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted |
| 18 | }); |
| 19 | ``` |
| 20 | |
| 21 | All examples below assume `client` is already initialized as shown above. |
| 22 | |
| 23 | ## Error Handling |
| 24 | |
| 25 | All API calls can fail with network errors, rate limits (429), validation errors (422), |
| 26 | or authentication errors (401). Always handle errors in production code: |
| 27 | |
| 28 | ```javascript |
| 29 | try { |
| 30 | const result = await client.messages.send({ to: '+13125550001', from: '+13125550002', text: 'Hello' }); |
| 31 | } catch (err) { |
| 32 | if (err instanceof Telnyx.APIConnectionError) { |
| 33 | console.error('Network error — check connectivity and retry'); |
| 34 | } else if (err instanceof Telnyx.RateLimitError) { |
| 35 | // 429: rate limited — wait and retry with exponential backoff |
| 36 | const retryAfter = err.headers?.['retry-after'] || 1; |
| 37 | await new Promise(r => setTimeout(r, retryAfter * 1000)); |
| 38 | } else if (err instanceof Telnyx.APIError) { |
| 39 | console.error(`API error ${err.status}: ${err.message}`); |
| 40 | if (err.status === 422) { |
| 41 | console.error('Validation error — check required fields and formats'); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | ``` |
| 46 | |
| 47 | Common error codes: `401` invalid API key, `403` insufficient permissions, |
| 48 | `404` resource not found, `422` validation error (check field formats), |
| 49 | `429` rate limited (retry with exponential backoff). |
| 50 | |
| 51 | ## Important Notes |
| 52 | |
| 53 | - **Pagination:** List methods return an auto-paginating iterator. Use `for await (const item of result) { ... }` to iterate through all pages automatically. |
| 54 | |
| 55 | ## List your voice channels for non-US zones |
| 56 | |
| 57 | Returns the non-US voice channels for your account. voice channels allow you to use Channel Billing for calls to your Telnyx phone numbers. Please check the Telnyx Support Articles section for full information and examples of how to utilize Channel Billing. |
| 58 | |
| 59 | `GET /channel_zones` |
| 60 | |
| 61 | ```javascript |
| 62 | // Automatically fetches more pages as needed. |
| 63 | for await (const channelZoneListResponse of client.channelZones.list()) { |
| 64 | console.log(channelZoneListResponse.id); |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | Returns: `channels` (int64), `countries` (array[string]), `created_at` (string), `id` (string), `name` (string), `record_type` (enum: channel_zone), `updated_at` (string) |
| 69 | |
| 70 | ## Update voice channels for non-US Zones |
| 71 | |
| 72 | Update the number of Voice Channels for the Non-US Zones. This allows your account to handle multiple simultaneous inbound calls to Non-US numbers. Use this endpoint to increase or decrease your capacity based on expected call volume. |
| 73 | |
| 74 | `PUT /channel_zones/{channel_zone_id}` — Required: `channels` |
| 75 | |
| 76 | ```javascript |
| 77 | const channelZone = await client.channelZones.update('channel_zone_id', { channels: 0 }); |
| 78 | |
| 79 | console.log(channelZone.id); |
| 80 | ``` |
| 81 | |
| 82 | Returns: `channels` (int64), `countries` (array[string]), `created_at` (string), `id` (string), `name` (string), `record_type` (enum: channel_zone), `updated_at` (string) |
| 83 | |
| 84 | ## List dynamic emergency addresses |
| 85 | |
| 86 | Returns the dynamic emergency addresses according to filters |
| 87 | |
| 88 | `GET /dynamic_emergency_addresses` |
| 89 | |
| 90 | ```javascript |
| 91 | // Automatically fetches more pages as needed. |
| 92 | for await (const dynamicEmergencyAddress of client.dynamicEmergencyAddresses.list()) { |
| 93 | console.log(dynamicEmergencyAddress.id); |
| 94 | } |
| 95 | ``` |
| 96 | |
| 97 | Returns: `administrative_area` (string), `country_code` (enum: US, CA, PR), `created_at` (string), `extended_address` (string), `house_number` (string), `house_suffix` (string), `id` (string), `locality` (string), `postal_code` (string), `record_type` (string), `sip_geolocation_id` (string), `status` (enum: pending, activated, rejected), `street_name` (string), `street_post_directional` (string), `street_pre_directional` (string), `street_suffix` (string), `updated_at` (string) |
| 98 | |
| 99 | ## Create a dynamic emergency address. |
| 100 | |
| 101 | Creates a dynamic emergency address. |
| 102 | |
| 103 | `POST /dynamic_emergency_addresses` — Required: `house_number`, `street_name`, `locality`, `administrative_area`, `postal_code`, `country_code` |
| 104 | |
| 105 | Optional: `created_at` (string), `extended_address` (string), `house_suffix` (string), `id` (string), `record_type` (string), `sip_geolocation_id` (string), `status` (enum: pending, activated, rejected), `street_post_directional` (string), `street_pre_directional` (string), `street_suffix` (string), `updated_at` (string) |
| 106 | |
| 107 | ```javascript |
| 108 | const dynamicEmergencyAddress = await client.dynamicEmergencyAddresses.create({ |
| 109 | administrative_area: 'TX', |
| 110 | country_code: 'US', |
| 111 | house_number: '600', |
| 112 | locality: 'Austin', |
| 113 | postal_code: '78701', |
| 114 | street_name: 'Congress', |
| 115 | }); |
| 116 | |
| 117 | console.log(dynamicEmergencyA |