$npx -y skills add team-telnyx/ai --skill telnyx-messaging-javascriptSend and receive SMS/MMS, handle opt-outs and delivery webhooks. Use for notifications, 2FA, or messaging apps.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Messaging - 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 response = await client.messages.send({ |
| 31 | to: '+18445550001', |
| 32 | from: '+18005550101', |
| 33 | text: 'Hello from Telnyx!', |
| 34 | }); |
| 35 | } catch (err) { |
| 36 | if (err instanceof Telnyx.APIConnectionError) { |
| 37 | console.error('Network error — check connectivity and retry'); |
| 38 | } else if (err instanceof Telnyx.RateLimitError) { |
| 39 | const retryAfter = err.headers?.['retry-after'] || 1; |
| 40 | await new Promise(r => setTimeout(r, retryAfter * 1000)); |
| 41 | } else if (err instanceof Telnyx.APIError) { |
| 42 | console.error(`API error ${err.status}: ${err.message}`); |
| 43 | if (err.status === 422) { |
| 44 | console.error('Validation error — check required fields and formats'); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | Common error codes: `401` invalid API key, `403` insufficient permissions, |
| 51 | `404` resource not found, `422` validation error (check field formats), |
| 52 | `429` rate limited (retry with exponential backoff). |
| 53 | |
| 54 | ## Important Notes |
| 55 | |
| 56 | - **Phone numbers** must be in E.164 format (e.g., `+13125550001`). Include the `+` prefix and country code. No spaces, dashes, or parentheses. |
| 57 | - **Pagination:** List methods return an auto-paginating iterator. Use `for await (const item of result) { ... }` to iterate through all pages automatically. |
| 58 | |
| 59 | ## Operational Caveats |
| 60 | |
| 61 | - The sending number must already be assigned to the correct messaging profile before you send traffic from it. |
| 62 | - US A2P long-code traffic must complete 10DLC registration before production sending or carriers will block or heavily filter messages. |
| 63 | - Delivery webhooks are asynchronous. Treat the send response as acceptance of the request, not final carrier delivery. |
| 64 | |
| 65 | ## Reference Use Rules |
| 66 | |
| 67 | Do not invent Telnyx parameters, enums, response fields, or webhook fields. |
| 68 | |
| 69 | - 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. |
| 70 | - 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). |
| 71 | - Before reading or matching webhook fields beyond the inline examples, read [the webhook payload reference](references/api-details.md#webhook-payload-fields). |
| 72 | |
| 73 | ## Core Tasks |
| 74 | |
| 75 | ### Send an SMS |
| 76 | |
| 77 | Primary outbound messaging flow. Agents need exact request fields and delivery-related response fields. |
| 78 | |
| 79 | `client.messages.send()` — `POST /messages` |
| 80 | |
| 81 | | Parameter | Type | Required | Description | |
| 82 | |-----------|------|----------|-------------| |
| 83 | | `to` | string (E.164) | Yes | Receiving address (+E.164 formatted phone number or short co... | |
| 84 | | `from` | string (E.164) | Yes | Sending address (+E.164 formatted phone number, alphanumeric... | |
| 85 | | `text` | string | Yes | Message body (i.e., content) as a non-empty string. | |
| 86 | | `messaging_profile_id` | string (UUID) | No | Unique identifier for a messaging profile. | |
| 87 | | `media_urls` | array[string] | No | A list of media URLs. | |
| 88 | | `webhook_url` | string (URL) | No | The URL where webhooks related to this message will be sent. | |
| 89 | | ... | | | +7 optional params in [references/api-details.md](references/api-details.md) | |
| 90 | |
| 91 | ```javascript |
| 92 | const response = await client.messages.send({ |
| 93 | to: '+18445550001', |
| 94 | from: '+18005550101', |
| 95 | text: 'Hello from Telnyx!', |
| 96 | }); |
| 97 | |
| 98 | console.log(response.data); |
| 99 | ``` |
| 100 | |
| 101 | Primary response fields: |
| 102 | - `response.data.id` |
| 103 | - `response.data.to` |
| 104 | - `response.data.from` |
| 105 | - `response.data.text` |
| 106 | - `response.data.sentAt` |
| 107 | - `response.data.errors` |
| 108 | |
| 109 | ### Send an SMS with an alphanumeric sender ID |
| 110 | |
| 111 | Common sender variant that requires different request shape. |
| 112 | |
| 113 | `client.messages.sendWithAlphanumericSender()` — `POST /messages/alphanumeric_sender_id` |
| 114 | |
| 115 | | Parameter | Type | Required | Description | |
| 116 | |-----------|------|----------|-------------| |
| 117 | | `from` | string (E.164) | Yes | A valid alphanumeric sender ID on the user's account. | |
| 118 | | `to` | string (E.164) | Yes | Receiving address (+E.164 formatted phone number or short co... | |
| 119 | | `text` | string | Yes | The message body. | |
| 120 | | `messaging_profile_id` | string (UUID) | Yes | The mes |