$npx -y skills add team-telnyx/ai --skill telnyx-ai-inference-javascriptAccess Telnyx LLM inference APIs, embeddings, and AI analytics for call insights and summaries. This skill provides JavaScript SDK examples.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Ai Inference - 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 | ## Transcribe speech to text |
| 56 | |
| 57 | Transcribe speech to text. This endpoint is consistent with the [OpenAI Transcription API](https://platform.openai.com/docs/api-reference/audio/createTranscription) and may be used with the OpenAI JS or Python SDK. |
| 58 | |
| 59 | `POST /ai/audio/transcriptions` |
| 60 | |
| 61 | ```javascript |
| 62 | const response = await client.ai.audio.transcribe({ model: 'distil-whisper/distil-large-v2' }); |
| 63 | |
| 64 | console.log(response.text); |
| 65 | ``` |
| 66 | |
| 67 | Returns: `duration` (number), `segments` (array[object]), `text` (string) |
| 68 | |
| 69 | ## Create a chat completion |
| 70 | |
| 71 | Chat with a language model. This endpoint is consistent with the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat) and may be used with the OpenAI JS or Python SDK. |
| 72 | |
| 73 | `POST /ai/chat/completions` — Required: `messages` |
| 74 | |
| 75 | Optional: `api_key_ref` (string), `best_of` (integer), `early_stopping` (boolean), `enable_thinking` (boolean), `frequency_penalty` (number), `guided_choice` (array[string]), `guided_json` (object), `guided_regex` (string), `length_penalty` (number), `logprobs` (boolean), `max_tokens` (integer), `min_p` (number), `model` (string), `n` (number), `presence_penalty` (number), `response_format` (object), `stream` (boolean), `temperature` (number), `tool_choice` (enum: none, auto, required), `tools` (array[object]), `top_logprobs` (integer), `top_p` (number), `use_beam_search` (boolean) |
| 76 | |
| 77 | ```javascript |
| 78 | const response = await client.ai.chat.createCompletion({ |
| 79 | messages: [ |
| 80 | { role: 'system', content: 'You are a friendly chatbot.' }, |
| 81 | { role: 'user', content: 'Hello, world!' }, |
| 82 | ], |
| 83 | }); |
| 84 | |
| 85 | console.log(response); |
| 86 | ``` |
| 87 | |
| 88 | ## List conversations |
| 89 | |
| 90 | Retrieve a list of all AI conversations configured by the user. Supports [PostgREST-style query parameters](https://postgrest.org/en/stable/api.html#horizontal-filtering-rows) for filtering. Examples are included for the standard metadata fields, but you can filter on any field in the metadata JSON object. |
| 91 | |
| 92 | `GET /ai/conversations` |
| 93 | |
| 94 | ```javascript |
| 95 | const conversations = await client.ai.conversations.list(); |
| 96 | |
| 97 | console.log(conversations.data); |
| 98 | ``` |
| 99 | |
| 100 | Returns: `created_at` (date-time), `id` (uuid), `last_message_at` (date-time), `metadata` (object), `name` (string) |
| 101 | |
| 102 | ## Create a conversation |
| 103 | |
| 104 | Create a new AI Conversation. |
| 105 | |
| 106 | `POST /ai/conversations` |
| 107 | |
| 108 | Optional: `metadata` (object), `name` (string) |
| 109 | |
| 110 | ```javascript |
| 111 | const conversation = await client.ai.conversations.create(); |
| 112 | |
| 113 | console.log(conversation.id); |
| 114 | ``` |
| 115 | |
| 116 | Returns: `created_at` (date-time), `id` (uuid), `last_message_at` (date-time), `metadata` (object), `name` (string) |
| 117 | |
| 118 | ## Get Insight Template Groups |
| 119 | |
| 120 | Get all insight groups |
| 121 | |
| 122 | `GET /ai/conversations/insight-groups` |
| 123 | |
| 124 | ```javascript |
| 125 | // Automatically fetches more pages as needed. |
| 126 | for await (const insightTemplateGroup of client.ai.conversations.insightGroups.retrieveInsightGroups()) { |
| 127 | console.log(insightTemplateGroup.id); |
| 128 | } |
| 129 | ``` |
| 130 | |
| 131 | Returns: `created_at` (date-time), `description` (string), `id` (uuid), `insights` (array[object]), `name` (string), `webhoo |