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 JavaScript SDK examples.
npx skill4agent add team-telnyx/skills telnyx-voice-media-javascriptnpm install telnyximport Telnyx from 'telnyx';
const client = new Telnyx({
apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
});clienttry {
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');
}
}
}401403404422429overlaytarget_legsselfPOST /calls/{call_control_id}/actions/playback_startaudio_typeaudio_urlcache_audioclient_statecommand_idloopmedia_nameoverlayplayback_contentstoptarget_legsconst response = await client.calls.actions.startPlayback('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);resultcall.playback.endedcall.speak.endedPOST /calls/{call_control_id}/actions/playback_stopclient_statecommand_idoverlaystopconst response = await client.calls.actions.stopPlayback('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);resultPOST /calls/{call_control_id}/actions/record_pauseclient_statecommand_idrecording_idconst response = await client.calls.actions.pauseRecording('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);resultPOST /calls/{call_control_id}/actions/record_resumeclient_statecommand_idrecording_idconst response = await client.calls.actions.resumeRecording('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(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_diarizationtrimconst response = await client.calls.actions.startRecording('call_control_id', {
channels: 'single',
format: 'wav',
});
console.log(response.data);resultcall.recording.savedPOST /calls/{call_control_id}/actions/record_stopclient_statecommand_idrecording_idconst response = await client.calls.actions.stopRecording('v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ');
console.log(response.data);resultcall.speak.startedcall.speak.endedPOST /calls/{call_control_id}/actions/speakpayloadvoiceclient_statecommand_idlanguagelooppayload_typeservice_levelstoptarget_legsvoice_settingsconst response = await client.calls.actions.speak('call_control_id', {
payload: 'Say this on the call',
voice: 'female',
language: 'en-US',
});
console.log(response.data);resulttelnyx-signature-ed25519telnyx-timestamp// 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');
}
});telnyx-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. |