$npx -y skills add team-telnyx/ai --skill telnyx-messaging-profiles-javascriptCreate and manage messaging profiles with number pools, sticky sender, and geomatch features. Configure short codes for high-volume messaging. This skill provides JavaScript SDK examples.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Messaging Profiles - 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 messaging profiles |
| 56 | |
| 57 | `GET /messaging_profiles` |
| 58 | |
| 59 | ```javascript |
| 60 | // Automatically fetches more pages as needed. |
| 61 | for await (const messagingProfile of client.messagingProfiles.list()) { |
| 62 | console.log(messagingProfile.id); |
| 63 | } |
| 64 | ``` |
| 65 | |
| 66 | Returns: `ai_assistant_id` (string | null), `alpha_sender` (string | null), `created_at` (date-time), `daily_spend_limit` (string), `daily_spend_limit_enabled` (boolean), `enabled` (boolean), `health_webhook_url` (url), `id` (uuid), `mms_fall_back_to_sms` (boolean), `mms_transcoding` (boolean), `mobile_only` (boolean), `name` (string), `number_pool_settings` (object | null), `organization_id` (string), `record_type` (enum: messaging_profile), `redaction_enabled` (boolean), `redaction_level` (integer), `resource_group_id` (string | null), `smart_encoding` (boolean), `updated_at` (date-time), `url_shortener_settings` (object | null), `v1_secret` (string), `webhook_api_version` (enum: 1, 2, 2010-04-01), `webhook_failover_url` (url), `webhook_url` (url), `whitelisted_destinations` (array[string]) |
| 67 | |
| 68 | ## Create a messaging profile |
| 69 | |
| 70 | `POST /messaging_profiles` — Required: `name`, `whitelisted_destinations` |
| 71 | |
| 72 | Optional: `ai_assistant_id` (string | null), `alpha_sender` (string | null), `daily_spend_limit` (string), `daily_spend_limit_enabled` (boolean), `enabled` (boolean), `health_webhook_url` (url), `mms_fall_back_to_sms` (boolean), `mms_transcoding` (boolean), `mobile_only` (boolean), `number_pool_settings` (object | null), `resource_group_id` (string | null), `smart_encoding` (boolean), `url_shortener_settings` (object | null), `webhook_api_version` (enum: 1, 2, 2010-04-01), `webhook_failover_url` (url), `webhook_url` (url) |
| 73 | |
| 74 | ```javascript |
| 75 | const messagingProfile = await client.messagingProfiles.create({ |
| 76 | name: 'My name', |
| 77 | whitelisted_destinations: ['US'], |
| 78 | }); |
| 79 | |
| 80 | console.log(messagingProfile.data); |
| 81 | ``` |
| 82 | |
| 83 | Returns: `ai_assistant_id` (string | null), `alpha_sender` (string | null), `created_at` (date-time), `daily_spend_limit` (string), `daily_spend_limit_enabled` (boolean), `enabled` (boolean), `health_webhook_url` (url), `id` (uuid), `mms_fall_back_to_sms` (boolean), `mms_transcoding` (boolean), `mobile_only` (boolean), `name` (string), `number_pool_settings` (object | null), `organization_id` (string), `record_type` (enum: messaging_profile), `redaction_enabled` (boolean), `redaction_level` (integer), `resource_group_id` (string | null), `smart_encoding` (boolean), `updated_at` (date-time), `url_shortener_settings` (object | null), `v1_secret` (string), `webhook_api_version` (enum: 1, 2, 2010-04-01), `webhook_failover_url` (url), `webhook_url` (url), `whitelisted_destinations` (array[string]) |
| 84 | |
| 85 | ## Retrieve a messaging profile |
| 86 | |
| 87 | `GET /messaging_profiles/{id}` |
| 88 | |
| 89 | ```javascript |
| 90 | const messagingProfile = await client.messagingProfiles.retrieve( |
| 91 | '182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e', |
| 92 | ); |
| 93 | |
| 94 | console.log(messagingProfile.data); |
| 95 | ``` |
| 96 | |
| 97 | Returns: `ai_assistant_id` ( |