$npx -y skills add team-telnyx/ai --skill telnyx-messaging-hosted-javascriptSet up hosted SMS numbers, toll-free verification, and RCS messaging. Use when migrating numbers or enabling rich messaging features. This skill provides JavaScript SDK examples.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Messaging Hosted - 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 | - **Phone numbers** must be in E.164 format (e.g., `+13125550001`). Include the `+` prefix and country code. No spaces, dashes, or parentheses. |
| 54 | - **Pagination:** List methods return an auto-paginating iterator. Use `for await (const item of result) { ... }` to iterate through all pages automatically. |
| 55 | |
| 56 | ## Send an RCS message |
| 57 | |
| 58 | `POST /messages/rcs` — Required: `agent_id`, `to`, `messaging_profile_id`, `agent_message` |
| 59 | |
| 60 | Optional: `mms_fallback` (object), `sms_fallback` (object), `type` (enum: RCS), `webhook_url` (url) |
| 61 | |
| 62 | ```javascript |
| 63 | const response = await client.messages.rcs.send({ |
| 64 | agent_id: 'Agent007', |
| 65 | agent_message: {}, |
| 66 | messaging_profile_id: '550e8400-e29b-41d4-a716-446655440000', |
| 67 | to: '+13125551234', |
| 68 | }); |
| 69 | |
| 70 | console.log(response.data); |
| 71 | ``` |
| 72 | |
| 73 | Returns: `body` (object), `direction` (string), `encoding` (string), `from` (object), `id` (string), `messaging_profile_id` (string), `organization_id` (string), `received_at` (date-time), `record_type` (string), `to` (array[object]), `type` (string), `wait_seconds` (float) |
| 74 | |
| 75 | ## Generate RCS deeplink |
| 76 | |
| 77 | Generate a deeplink URL that can be used to start an RCS conversation with a specific agent. |
| 78 | |
| 79 | `GET /messages/rcs/deeplinks/{agent_id}` |
| 80 | |
| 81 | ```javascript |
| 82 | const response = await client.messages.rcs.generateDeeplink('agent_id'); |
| 83 | |
| 84 | console.log(response.data); |
| 85 | ``` |
| 86 | |
| 87 | Returns: `url` (string) |
| 88 | |
| 89 | ## List all RCS agents |
| 90 | |
| 91 | `GET /messaging/rcs/agents` |
| 92 | |
| 93 | ```javascript |
| 94 | // Automatically fetches more pages as needed. |
| 95 | for await (const rcsAgent of client.messaging.rcs.agents.list()) { |
| 96 | console.log(rcsAgent.agent_id); |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | Returns: `agent_id` (string), `agent_name` (string), `created_at` (date-time), `enabled` (boolean), `profile_id` (uuid), `updated_at` (date-time), `user_id` (string), `webhook_failover_url` (url), `webhook_url` (url) |
| 101 | |
| 102 | ## Retrieve an RCS agent |
| 103 | |
| 104 | `GET /messaging/rcs/agents/{id}` |
| 105 | |
| 106 | ```javascript |
| 107 | const rcsAgentResponse = await client.messaging.rcs.agents.retrieve('550e8400-e29b-41d4-a716-446655440000'); |
| 108 | |
| 109 | console.log(rcsAgentResponse.data); |
| 110 | ``` |
| 111 | |
| 112 | Returns: `agent_id` (string), `agent_name` (string), `created_at` (date-time), `enabled` (boolean), `profile_id` (uuid), `updated_at` (date-time), `user_id` (string), `webhook_failover_url` (url), `webhook_url` (url) |
| 113 | |
| 114 | ## Modify an RCS agent |
| 115 | |
| 116 | `PATCH /messaging/rcs/agents/{id}` |
| 117 | |
| 118 | Optional: `profile_id` (uuid), `webhook_failover_url` (url), `webhook_url` (url) |
| 119 | |
| 120 | ```javascript |
| 121 | const rcsAgentResponse = await client.messaging.rcs.agents.update('550e8400-e29b-41d4-a716-446655440000'); |
| 122 | |
| 123 | console.log(rcsAgentResponse.data); |
| 124 | ``` |
| 125 | |
| 126 | Returns: `agent_id` (string), `agent_name` (string), `created_at` (date-time), `enabled` (boolean), `profile_id` (uuid), `updated_at` (date-time), `user_id` (string), `webhook_failover_url` (url), `webhook_url` (url) |
| 127 | |
| 128 | ## Check RCS capabilities (batch) |
| 129 | |
| 130 | `POST /messaging/rcs/bulk_capabilities` — Required: `agent_id`, `phone_numbers` |
| 131 | |
| 132 | ```javascript |
| 133 | const response = await client.messaging.rcs.listBulkCapabilities({ |
| 134 | agent_id: 'TestAgent', |
| 135 | phone_numbers: ['+13125551234'], |
| 136 | }); |
| 137 | |
| 138 | console.log(response.data); |
| 139 | ``` |
| 140 | |
| 141 | Returns: `agent_id` (string), `agent_name` (string), |