<!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. -->
Telnyx Voice Streaming - JavaScript
Installation
Setup
javascript
import Telnyx from 'telnyx';
const client = new Telnyx({
apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
});
All examples below assume
is already initialized as shown above.
Error Handling
All API calls can fail with network errors, rate limits (429), validation errors (422),
or authentication errors (401). Always handle errors in production code:
javascript
try {
const result = await client.messages.send({ to: '+13125550001', from: '+13125550002', text: 'Hello' });
} catch (err) {
if (err instanceof Telnyx.APIConnectionError) {
console.error('Network error — check connectivity and retry');
} else if (err instanceof Telnyx.RateLimitError) {
// 429: rate limited — wait and retry with exponential backoff
const retryAfter = err.headers?.['retry-after'] || 1;
await new Promise(r => setTimeout(r, retryAfter * 1000));
} else if (err instanceof Telnyx.APIError) {
console.error(`API error ${err.status}: ${err.message}`);
if (err.status === 422) {
console.error('Validation error — check required fields and formats');
}
}
}
Common error codes:
invalid API key,
insufficient permissions,
resource not found,
validation error (check field formats),
rate limited (retry with exponential backoff).
Forking start
Call forking allows you to stream the media from a call to a specific target in realtime. This stream can be used to enable realtime audio analysis to support a
variety of use cases, including fraud detection, or the creation of AI-generated audio responses. Requests must specify either the
attribute or the
and
attributes.
POST /calls/{call_control_id}/actions/fork_start
Optional:
(string),
(string),
(string),
(enum: decrypted),
(string)
javascript
const response = await client.calls.actions.startForking('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);
Forking stop
Stop forking a call. Expected Webhooks:
POST /calls/{call_control_id}/actions/fork_stop
Optional:
(string),
(string),
(enum: raw, decrypted)
javascript
const response = await client.calls.actions.stopForking('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);
Streaming start
Start streaming the media from a call to a specific WebSocket address or Dialogflow connection in near-realtime. Audio will be delivered as base64-encoded RTP payload (raw audio), wrapped in JSON payloads. Please find more details about media streaming messages specification under the
link.
POST /calls/{call_control_id}/actions/streaming_start
Optional:
(string),
(string),
(array[object]),
(object),
(boolean),
(string),
stream_bidirectional_codec
(enum: PCMU, PCMA, G722, OPUS, AMR-WB, L16),
stream_bidirectional_mode
(enum: mp3, rtp),
stream_bidirectional_sampling_rate
(enum: 8000, 16000, 22050, 24000, 48000),
stream_bidirectional_target_legs
(enum: both, self, opposite),
(enum: PCMU, PCMA, G722, OPUS, AMR-WB, L16, default),
(enum: inbound_track, outbound_track, both_tracks),
(string)
javascript
const response = await client.calls.actions.startStreaming('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);
Streaming stop
Stop streaming a call to a WebSocket. Expected Webhooks:
POST /calls/{call_control_id}/actions/streaming_stop
Optional:
(string),
(string),
(uuid)
javascript
const response = await client.calls.actions.stopStreaming('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);
Transcription start
Start real-time transcription. Transcription will stop on call hang-up, or can be initiated via the Transcription stop command. Expected Webhooks:
POST /calls/{call_control_id}/actions/transcription_start
Optional:
(string),
(string),
(enum: Google, Telnyx, Deepgram, Azure, A, B),
transcription_engine_config
(object),
(string)
javascript
const response = await client.calls.actions.startTranscription('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);
Transcription stop
Stop real-time transcription.
POST /calls/{call_control_id}/actions/transcription_stop
Optional:
(string),
(string)
javascript
const response = await client.calls.actions.stopTranscription('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);
Webhooks
Webhook Verification
Telnyx signs webhooks with Ed25519. Each request includes
and
headers. Always verify signatures in production:
javascript
// In your webhook handler (e.g., Express — use raw body, not parsed JSON):
app.post('/webhooks', express.raw({ type: 'application/json' }), async (req, res) => {
try {
const event = await client.webhooks.unwrap(req.body.toString(), {
headers: req.headers,
});
// Signature valid — event is the parsed webhook payload
console.log('Received event:', event.data.event_type);
res.status(200).send('OK');
} catch (err) {
console.error('Webhook verification failed:', err.message);
res.status(400).send('Invalid signature');
}
});
The following webhook events are sent to your configured webhook URL.
All webhooks include
and
headers for Ed25519 signature verification. Use
to verify.
| Event | Description |
|---|
| Call Fork Started |
| Call Fork Stopped |
| Call Streaming Failed |
| Call Streaming Started |
| Call Streaming Stopped |
| Transcription |
Webhook payload fields
| Field | Type | Description |
|---|
| enum: event | Identifies the type of the resource. |
| enum: call.fork.started | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
data.payload.connection_id
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
data.payload.call_control_id
| string | Unique ID for controlling the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
data.payload.call_session_id
| string | ID that is unique to the call session and can be used to correlate webhook events. |
data.payload.client_state
| string | State received from a command. |
| enum: decrypted | Type of media streamed. |
| Field | Type | Description |
|---|
| enum: event | Identifies the type of the resource. |
| enum: call.fork.stopped | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
data.payload.connection_id
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
data.payload.call_control_id
| string | Unique ID for controlling the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
data.payload.call_session_id
| string | ID that is unique to the call session and can be used to correlate webhook events. |
data.payload.client_state
| string | State received from a command. |
| enum: decrypted | Type of media streamed. |
| Field | Type | Description |
|---|
| enum: event | Identifies the resource. |
| enum: streaming.failed | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
data.payload.call_control_id
| string | Call ID used to issue commands via Call Control API. |
data.payload.connection_id
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
data.payload.call_session_id
| string | ID that is unique to the call session and can be used to correlate webhook events. |
data.payload.client_state
| string | State received from a command. |
data.payload.failure_reason
| string | A short description explaning why the media streaming failed. |
| uuid | Identifies the streaming. |
| enum: websocket, dialogflow | The type of stream connection the stream is performing. |
| Field | Type | Description |
|---|
| enum: event | Identifies the type of the resource. |
| enum: streaming.started | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
data.payload.call_control_id
| string | Call ID used to issue commands via Call Control API. |
data.payload.connection_id
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
data.payload.call_session_id
| string | ID that is unique to the call session and can be used to correlate webhook events. |
data.payload.client_state
| string | State received from a command. |
| string | Destination WebSocket address where the stream is going to be delivered. |
| Field | Type | Description |
|---|
| enum: event | Identifies the type of the resource. |
| enum: streaming.stopped | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
data.payload.call_control_id
| string | Call ID used to issue commands via Call Control API. |
data.payload.connection_id
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
data.payload.call_session_id
| string | ID that is unique to the call session and can be used to correlate webhook events. |
data.payload.client_state
| string | State received from a command. |
| string | Destination WebSocket address where the stream is going to be delivered. |
| Field | Type | Description |
|---|
| enum: event | Identifies the type of the resource. |
| enum: call.transcription | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
data.payload.call_control_id
| string | Unique identifier and token for controlling the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
data.payload.call_session_id
| string | ID that is unique to the call session and can be used to correlate webhook events. |
data.payload.client_state
| string | Use this field to add state to every subsequent webhook. |
data.payload.connection_id
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |