$npx -y skills add team-telnyx/ai --skill telnyx-ai-assistants-javascriptAI voice assistants with custom instructions, knowledge bases, and tool integrations.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx AI Assistants - 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 assistant = await client.ai.assistants.create({ |
| 31 | instructions: 'You are a helpful assistant.', |
| 32 | name: 'my-resource', |
| 33 | model: 'openai/gpt-4o', |
| 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 | ## Reference Use Rules |
| 60 | |
| 61 | Do not invent Telnyx parameters, enums, response fields, or webhook fields. |
| 62 | |
| 63 | - 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. |
| 64 | - 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). |
| 65 | |
| 66 | ## Core Tasks |
| 67 | |
| 68 | ### Create an assistant |
| 69 | |
| 70 | Assistant creation is the entrypoint for any AI assistant integration. Agents need the exact creation method and the top-level fields returned by the SDK. |
| 71 | |
| 72 | `client.ai.assistants.create()` — `POST /ai/assistants` |
| 73 | |
| 74 | | Parameter | Type | Required | Description | |
| 75 | |-----------|------|----------|-------------| |
| 76 | | `name` | string | Yes | | |
| 77 | | `instructions` | string | Yes | System instructions for the assistant. | |
| 78 | | `tags` | array[string] | No | Tags associated with the assistant. | |
| 79 | | `model` | string | No | ID of the model to use when `external_llm` is not set. | |
| 80 | | `tools` | array[object] | No | Deprecated for new integrations. | |
| 81 | | ... | | | +23 optional params in [references/api-details.md](references/api-details.md) | |
| 82 | |
| 83 | ```javascript |
| 84 | const assistant = await client.ai.assistants.create({ |
| 85 | instructions: 'You are a helpful assistant.', |
| 86 | name: 'my-resource', |
| 87 | model: 'openai/gpt-4o', |
| 88 | }); |
| 89 | |
| 90 | console.log(assistant.id); |
| 91 | ``` |
| 92 | |
| 93 | Primary response fields: |
| 94 | - `assistant.id` |
| 95 | - `assistant.name` |
| 96 | - `assistant.model` |
| 97 | - `assistant.instructions` |
| 98 | - `assistant.createdAt` |
| 99 | - `assistant.conversationFlow` |
| 100 | |
| 101 | ### Chat with an assistant |
| 102 | |
| 103 | Chat is the primary runtime path. Agents need the exact assistant method and the response content field. |
| 104 | |
| 105 | `client.ai.assistants.chat()` — `POST /ai/assistants/{assistant_id}/chat` |
| 106 | |
| 107 | | Parameter | Type | Required | Description | |
| 108 | |-----------|------|----------|-------------| |
| 109 | | `content` | string | Yes | The message content sent by the client to the assistant | |
| 110 | | `conversation_id` | string (UUID) | Yes | A unique identifier for the conversation thread, used to mai... | |
| 111 | | `assistant_id` | string (UUID) | Yes | Unique identifier of the assistant. | |
| 112 | | `name` | string | No | The optional display name of the user sending the message | |
| 113 | |
| 114 | ```javascript |
| 115 | const response = await client.ai.assistants.chat('assistant_id', { |
| 116 | content: 'Tell me a joke about cats', |
| 117 | conversation_id: '42b20469-1215-4a9a-8964-c36f66b406f4', |
| 118 | }); |
| 119 | |
| 120 | console.log(response.content); |
| 121 | ``` |
| 122 | |
| 123 | Primary response fields: |
| 124 | - `response.content` |
| 125 | |
| 126 | ### Create an assistant test |
| 127 | |
| 128 | Test creation is the main validation path for production assistant behavior before deployment. |
| 129 | |
| 130 | `client.ai.assistants.tests.create()` — `POST /ai/assistants/tests` |
| 131 | |
| 132 | | Parameter | Type | Required | Description | |
| 133 | |-----------|------|----------|-------------| |
| 134 | | `name` | string | |