telnyx-voice-streaming-python
Original:🇺🇸 English
Translated
Stream call audio in real-time, fork media to external destinations, and transcribe speech live. Use for real-time analytics and AI integrations. This skill provides Python SDK examples.
3installs
Sourceteam-telnyx/skills
Added on
NPX Install
npx skill4agent add team-telnyx/skills telnyx-voice-streaming-pythonTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →<!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. -->
Telnyx Voice Streaming - Python
Installation
bash
pip install telnyxSetup
python
import os
from telnyx import Telnyx
client = Telnyx(
api_key=os.environ.get("TELNYX_API_KEY"), # This is the default and can be omitted
)All examples below assume is already initialized as shown above.
clientError 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:
python
import telnyx
try:
result = client.messages.send(to="+13125550001", from_="+13125550002", text="Hello")
except telnyx.APIConnectionError:
print("Network error — check connectivity and retry")
except telnyx.RateLimitError:
# 429: rate limited — wait and retry with exponential backoff
import time
time.sleep(1) # Check Retry-After header for actual delay
except telnyx.APIStatusError as e:
print(f"API error {e.status_code}: {e.message}")
if e.status_code == 422:
print("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).
401403404422429Forking 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.
targetrxtxPOST /calls/{call_control_id}/actions/fork_startOptional: (string), (string), (string), (enum: decrypted), (string)
client_statecommand_idrxstream_typetxpython
response = client.calls.actions.start_forking(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)Returns: (string)
resultForking stop
Stop forking a call. Expected Webhooks:
call.fork.stopped
POST /calls/{call_control_id}/actions/fork_stopOptional: (string), (string), (enum: raw, decrypted)
client_statecommand_idstream_typepython
response = client.calls.actions.stop_forking(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)Returns: (string)
resultStreaming 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_startOptional: (string), (string), (array[object]), (object), (boolean), (string), (enum: PCMU, PCMA, G722, OPUS, AMR-WB, L16), (enum: mp3, rtp), (enum: 8000, 16000, 22050, 24000, 48000), (enum: both, self, opposite), (enum: PCMU, PCMA, G722, OPUS, AMR-WB, L16, default), (enum: inbound_track, outbound_track, both_tracks), (string)
client_statecommand_idcustom_parametersdialogflow_configenable_dialogflowstream_auth_tokenstream_bidirectional_codecstream_bidirectional_modestream_bidirectional_sampling_ratestream_bidirectional_target_legsstream_codecstream_trackstream_urlpython
response = client.calls.actions.start_streaming(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
stream_url="wss://example.com/audio-stream",
)
print(response.data)Returns: (string)
resultStreaming stop
Stop streaming a call to a WebSocket. Expected Webhooks:
streaming.stopped
POST /calls/{call_control_id}/actions/streaming_stopOptional: (string), (string), (uuid)
client_statecommand_idstream_idpython
response = client.calls.actions.stop_streaming(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)Returns: (string)
resultTranscription start
Start real-time transcription. Transcription will stop on call hang-up, or can be initiated via the Transcription stop command. Expected Webhooks:
call.transcription
POST /calls/{call_control_id}/actions/transcription_startOptional: (string), (string), (enum: Google, Telnyx, Deepgram, Azure, A, B), (object), (string)
client_statecommand_idtranscription_enginetranscription_engine_configtranscription_trackspython
response = client.calls.actions.start_transcription(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)Returns: (string)
resultTranscription stop
Stop real-time transcription.
POST /calls/{call_control_id}/actions/transcription_stopOptional: (string), (string)
client_statecommand_idpython
response = client.calls.actions.stop_transcription(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)Returns: (string)
resultWebhooks
Webhook Verification
Telnyx signs webhooks with Ed25519. Each request includes
and headers. Always verify signatures in production:
telnyx-signature-ed25519telnyx-timestamppython
# In your webhook handler (e.g., Flask — use raw body, not parsed JSON):
@app.route("/webhooks", methods=["POST"])
def handle_webhook():
payload = request.get_data(as_text=True) # raw body as string
headers = dict(request.headers)
try:
event = client.webhooks.unwrap(payload, headers=headers)
except Exception as e:
print(f"Webhook verification failed: {e}")
return "Invalid signature", 400
# Signature valid — event is the parsed webhook payload
print(f"Received event: {event.data.event_type}")
return "OK", 200The following webhook events are sent to your configured webhook URL.
All webhooks include and headers for Ed25519 signature verification. Use to verify.
telnyx-timestamptelnyx-signature-ed25519client.webhooks.unwrap()| Event | Description |
|---|---|
| Call Fork Started |
| Call Fork Stopped |
| Call Streaming Failed |
| Call Streaming Started |
| Call Streaming Stopped |
| Transcription |
Webhook payload fields
callForkStarted| 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. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | Unique ID for controlling the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| enum: decrypted | Type of media streamed. |
callForkStopped| 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. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | Unique ID for controlling the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| enum: decrypted | Type of media streamed. |
callStreamingFailed| 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. |
| string | Call ID used to issue commands via Call Control API. |
| 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. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| 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. |
callStreamingStarted| 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. |
| string | Call ID used to issue commands via Call Control API. |
| 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. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| string | Destination WebSocket address where the stream is going to be delivered. |
callStreamingStopped| 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. |
| string | Call ID used to issue commands via Call Control API. |
| 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. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| string | Destination WebSocket address where the stream is going to be delivered. |
transcription| 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. |
| 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. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | Use this field to add state to every subsequent webhook. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |