0

Transfer call

by
Published Apr 8, 2025

Transfer a call to a new destination.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Transfer call
7
 * Transfer a call to a new destination.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	call_control_id: string,
12
	body: {
13
		to: string
14
		from?: string
15
		from_display_name?: string
16
		audio_url?: string
17
		early_media?: false | true
18
		media_name?: string
19
		timeout_secs?: number
20
		time_limit_secs?: number
21
		answering_machine_detection?:
22
			| 'premium'
23
			| 'detect'
24
			| 'detect_beep'
25
			| 'detect_words'
26
			| 'greeting_end'
27
			| 'disabled'
28
		answering_machine_detection_config?: {
29
			total_analysis_time_millis?: number
30
			after_greeting_silence_millis?: number
31
			between_words_silence_millis?: number
32
			greeting_duration_millis?: number
33
			initial_silence_millis?: number
34
			maximum_number_of_words?: number
35
			maximum_word_length_millis?: number
36
			silence_threshold?: number
37
			greeting_total_analysis_time_millis?: number
38
			greeting_silence_duration_millis?: number
39
		}
40
		custom_headers?: { name: string; value: string }[]
41
		client_state?: string
42
		target_leg_client_state?: string
43
		command_id?: string
44
		media_encryption?: 'disabled' | 'SRTP'
45
		sip_auth_username?: string
46
		sip_auth_password?: string
47
		sip_headers?: { name: 'User-to-User'; value: string }[]
48
		sip_transport_protocol?: 'UDP' | 'TCP' | 'TLS'
49
		sound_modifications?: {
50
			pitch?: number
51
			semitone?: number
52
			octaves?: number
53
			track?: string
54
		}
55
		webhook_url?: string
56
		webhook_url_method?: 'POST' | 'GET'
57
	}
58
) {
59
	const url = new URL(`https://api.telnyx.com/v2/calls/${call_control_id}/actions/transfer`)
60

61
	const response = await fetch(url, {
62
		method: 'POST',
63
		headers: {
64
			'Content-Type': 'application/json',
65
			Authorization: 'Bearer ' + auth.apiKey
66
		},
67
		body: JSON.stringify(body)
68
	})
69
	if (!response.ok) {
70
		const text = await response.text()
71
		throw new Error(`${response.status} ${text}`)
72
	}
73
	return await response.json()
74
}
75