Loading...
Loading...
Play audio files, use text-to-speech, and record calls. Use when building IVR systems, playing announcements, or recording conversations. This skill provides Python SDK examples.
npx skill4agent add team-telnyx/skills telnyx-voice-media-pythonpip install telnyximport os
from telnyx import Telnyx
client = Telnyx(
api_key=os.environ.get("TELNYX_API_KEY"), # This is the default and can be omitted
)clientimport 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")401403404422429overlaytarget_legsselfPOST /calls/{call_control_id}/actions/playback_startaudio_typeaudio_urlcache_audioclient_statecommand_idloopmedia_nameoverlayplayback_contentstoptarget_legsresponse = client.calls.actions.start_playback(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
audio_url="https://example.com/audio.mp3",
)
print(response.data)resultcall.playback.endedcall.speak.endedPOST /calls/{call_control_id}/actions/playback_stopclient_statecommand_idoverlaystopresponse = client.calls.actions.stop_playback(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultPOST /calls/{call_control_id}/actions/record_pauseclient_statecommand_idrecording_idresponse = client.calls.actions.pause_recording(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultPOST /calls/{call_control_id}/actions/record_resumeclient_statecommand_idrecording_idresponse = client.calls.actions.resume_recording(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultcall.recording.savedcall.recording.transcription.savedcall.recording.errorPOST /calls/{call_control_id}/actions/record_startformatchannelsclient_statecommand_idcustom_file_namemax_lengthplay_beeprecording_tracktimeout_secstranscriptiontranscription_enginetranscription_languagetranscription_max_speaker_counttranscription_min_speaker_counttranscription_profanity_filtertranscription_speaker_diarizationtrimresponse = client.calls.actions.start_recording(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
channels="single",
format="wav",
)
print(response.data)resultcall.recording.savedPOST /calls/{call_control_id}/actions/record_stopclient_statecommand_idrecording_idresponse = client.calls.actions.stop_recording(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
)
print(response.data)resultcall.speak.startedcall.speak.endedPOST /calls/{call_control_id}/actions/speakpayloadvoiceclient_statecommand_idlanguagelooppayload_typeservice_levelstoptarget_legsvoice_settingsresponse = client.calls.actions.speak(
call_control_id="550e8400-e29b-41d4-a716-446655440000",
payload="Say this on the call",
voice="female",
language="en-US",
)
print(response.data)resulttelnyx-signature-ed25519telnyx-timestamp# 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", 200telnyx-timestamptelnyx-signature-ed25519client.webhooks.unwrap()| Event | Description |
|---|---|
| Call Playback Ended |
| Call Playback Started |
| Call Recording Error |
| Call Recording Saved |
| Call Recording Transcription Saved |
| Call Speak Ended |
| Call Speak Started |
callPlaybackEnded| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.playback.ended | 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 | The audio URL being played back, if audio_url has been used to start. |
| string | The name of the audio media file being played back, if media_name has been used to start. |
| boolean | Whether the stopped audio was in overlay mode or not. |
| enum: file_not_found, call_hangup, unknown, cancelled, cancelled_amd, completed, failed | Reflects how command ended. |
| string | Provides details in case of failure. |
callPlaybackStarted| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.playback.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 | The audio URL being played back, if audio_url has been used to start. |
| string | The name of the audio media file being played back, if media_name has been used to start. |
| boolean | Whether the audio is going to be played in overlay mode or not. |
callRecordingError| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.recording.error | 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. |
| enum: Failed to authorize with storage using custom credentials, Invalid credentials json, Unsupported backend, Internal server error | Indication that there was a problem recording the call. |
callRecordingSaved| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.recording.saved | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| 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 | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | State received from a command. |
| date-time | ISO 8601 datetime of when recording started. |
| date-time | ISO 8601 datetime of when recording ended. |
| enum: single, dual | Whether recording was recorded in |
callRecordingTranscriptionSaved| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.recording.transcription.saved | 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. |
| enum: pstn, sip | The type of calling party connection. |
| string | ID that is unique to the recording session and can be used to correlate webhook events. |
| string | ID that is unique to the transcription process and can be used to correlate webhook events. |
| enum: completed | The transcription status. |
| string | The transcribed text |
callSpeakEnded| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.speak.ended | 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. |
| enum: completed, call_hangup, cancelled_amd | Reflects how the command ended. |
callSpeakStarted| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.speak.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. |