$npx -y skills add team-telnyx/ai --skill telnyx-numbers-javascriptSearch, order, and manage phone numbers by location, features, and coverage.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Numbers - JavaScript |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | npm install telnyx@6.74.2 |
| 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 availablePhoneNumbers = await client.availablePhoneNumbers.list(); |
| 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 | const retryAfter = err.headers?.['retry-after'] || 1; |
| 36 | await new Promise(r => setTimeout(r, retryAfter * 1000)); |
| 37 | } else if (err instanceof Telnyx.APIError) { |
| 38 | console.error(`API error ${err.status}: ${err.message}`); |
| 39 | if (err.status === 422) { |
| 40 | console.error('Validation error — check required fields and formats'); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | ``` |
| 45 | |
| 46 | Common error codes: `401` invalid API key, `403` insufficient permissions, |
| 47 | `404` resource not found, `422` validation error (check field formats), |
| 48 | `429` rate limited (retry with exponential backoff). |
| 49 | |
| 50 | ## Important Notes |
| 51 | |
| 52 | - **Phone numbers** must be in E.164 format (e.g., `+13125550001`). Include the `+` prefix and country code. No spaces, dashes, or parentheses. |
| 53 | - **Pagination:** List methods return an auto-paginating iterator. Use `for await (const item of result) { ... }` to iterate through all pages automatically. |
| 54 | |
| 55 | ## Reference Use Rules |
| 56 | |
| 57 | Do not invent Telnyx parameters, enums, response fields, or webhook fields. |
| 58 | |
| 59 | - If the parameter, enum, or response field you need is not shown inline in this skill, read [references/api-details.md](references/api-details.md) before writing code. |
| 60 | - Before using any operation in `## Additional Operations`, read [the optional-parameters section](references/api-details.md#optional-parameters) and [the response-schemas section](references/api-details.md#response-schemas). |
| 61 | |
| 62 | ## Core Tasks |
| 63 | |
| 64 | ### Search available phone numbers |
| 65 | |
| 66 | Number search is the entrypoint for provisioning. Agents need the search method, key query filters, and the fields returned for candidate numbers. |
| 67 | |
| 68 | `client.availablePhoneNumbers.list()` — `GET /available_phone_numbers` |
| 69 | |
| 70 | | Parameter | Type | Required | Description | |
| 71 | |-----------|------|----------|-------------| |
| 72 | | `filter` | object | No | Consolidated filter parameter (deepObject style). | |
| 73 | |
| 74 | ```javascript |
| 75 | const availablePhoneNumbers = await client.availablePhoneNumbers.list(); |
| 76 | |
| 77 | console.log(availablePhoneNumbers.data); |
| 78 | ``` |
| 79 | |
| 80 | Response wrapper: |
| 81 | - items: `availablePhoneNumbers.data` |
| 82 | - pagination: `availablePhoneNumbers.meta` |
| 83 | |
| 84 | Primary item fields: |
| 85 | - `phoneNumber` |
| 86 | - `recordType` |
| 87 | - `quickship` |
| 88 | - `reservable` |
| 89 | - `bestEffort` |
| 90 | - `costInformation` |
| 91 | |
| 92 | ### Create a number order |
| 93 | |
| 94 | Number ordering is the production provisioning step after number selection. |
| 95 | |
| 96 | `client.numberOrders.create()` — `POST /number_orders` |
| 97 | |
| 98 | | Parameter | Type | Required | Description | |
| 99 | |-----------|------|----------|-------------| |
| 100 | | `phone_numbers` | array[object] | Yes | | |
| 101 | | `connection_id` | string (UUID) | No | Identifies the connection associated with this phone number. | |
| 102 | | `messaging_profile_id` | string (UUID) | No | Identifies the messaging profile associated with the phone n... | |
| 103 | | `billing_group_id` | string (UUID) | No | Identifies the billing group associated with the phone numbe... | |
| 104 | | ... | | | +1 optional params in [references/api-details.md](references/api-details.md) | |
| 105 | |
| 106 | ```javascript |
| 107 | const numberOrder = await client.numberOrders.create({ |
| 108 | phone_numbers: [{"phone_number": "+18005550101"}], |
| 109 | }); |
| 110 | |
| 111 | console.log(numberOrder.data); |
| 112 | ``` |
| 113 | |
| 114 | Primary response fields: |
| 115 | - `numberOrder.data.id` |
| 116 | - `numberOrder.data.status` |
| 117 | - `numberOrder.data.phoneNumbersCount` |
| 118 | - `numberOrder.data.requirementsMet` |
| 119 | - `numberOrder.data.messagingProfileId` |
| 120 | - `numberOrder.data.connectionId` |
| 121 | |
| 122 | ### Check number order status |
| 123 | |
| 124 | Order status determines whether provisioning completed or additional requirements are still blocking fulfillment. |
| 125 | |
| 126 | `client.numberOrders.retrieve()` — `GET /number_orders/{number_order_id}` |
| 127 | |
| 128 | | Parameter | Type | Required | Description | |
| 129 | |-----------|------|----------|-------------| |
| 130 | | `number_order_id` | string (UUID) | Yes | The number order ID. | |
| 131 | |
| 132 | ```javascript |
| 133 | const numberOrder = await client.numberOrders.retrieve('550e8400-e29b-41d4-a716-446655440000'); |
| 134 | |
| 135 | console.log(numberOrder.data); |
| 136 | ``` |
| 137 | |
| 138 | Primary response fields: |
| 139 | - `numberOrder.data.id` |
| 140 | - `number |