0

Play audio URL

by
Published Apr 8, 2025

Play an audio file on the call.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Play audio URL
7
 * Play an audio file on the call.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	call_control_id: string,
12
	body: {
13
		audio_url?: string
14
		media_name?: string
15
		loop?: string | number
16
		overlay?: false | true
17
		stop?: string
18
		target_legs?: string
19
		cache_audio?: false | true
20
		audio_type?: 'mp3' | 'wav'
21
		playback_content?: string
22
		client_state?: string
23
		command_id?: string
24
	}
25
) {
26
	const url = new URL(`https://api.telnyx.com/v2/calls/${call_control_id}/actions/playback_start`)
27

28
	const response = await fetch(url, {
29
		method: 'POST',
30
		headers: {
31
			'Content-Type': 'application/json',
32
			Authorization: 'Bearer ' + auth.apiKey
33
		},
34
		body: JSON.stringify(body)
35
	})
36
	if (!response.ok) {
37
		const text = await response.text()
38
		throw new Error(`${response.status} ${text}`)
39
	}
40
	return await response.json()
41
}
42