0

Start streaming media from a call.

by
Published Apr 8, 2025

Starts streaming media from a call to a specific WebSocket address.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Start streaming media from a call.
7
 * Starts streaming media from a call to a specific WebSocket address.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	account_sid: string,
12
	call_sid: string,
13
	body: {
14
		StatusCallback?: string
15
		StatusCallbackMethod?: 'GET' | 'POST'
16
		Track?: 'inbound_track' | 'outbound_track' | 'both_tracks'
17
		Name?: string
18
		BidirectionalMode?: 'mp3' | 'rtp'
19
		BidirectionalCodec?: 'PCMU' | 'PCMA' | 'G722'
20
		Url?: string
21
	}
22
) {
23
	const url = new URL(
24
		`https://api.telnyx.com/v2/texml/Accounts/${account_sid}/Calls/${call_sid}/Streams.json`
25
	)
26

27
	const response = await fetch(url, {
28
		method: 'POST',
29
		headers: {
30
			'Content-Type': 'application/x-www-form-urlencoded',
31
			Authorization: 'Bearer ' + auth.apiKey
32
		},
33
		body: new URLSearchParams(body as Record<string, string>)
34
	})
35
	if (!response.ok) {
36
		const text = await response.text()
37
		throw new Error(`${response.status} ${text}`)
38
	}
39
	return await response.json()
40
}
41