0

Initiate an outbound call

by
Published Apr 8, 2025

Initiate an outbound TeXML call. Telnyx will request TeXML from the XML Request URL configured for the connection in the Mission Control Portal.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Initiate an outbound call
7
 * Initiate an outbound TeXML call. Telnyx will request TeXML from the XML Request URL configured for the connection in the Mission Control Portal.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	application_id: string,
12
	body: {
13
		ApplicationSid: string
14
		To: string
15
		From: string
16
		Url?: string
17
		UrlMethod?: 'GET' | 'POST'
18
		FallbackUrl?: string
19
		StatusCallback?: string
20
		StatusCallbackMethod?: 'GET' | 'POST'
21
		StatusCallbackEvent?: 'initiated' | 'ringing' | 'answered' | 'completed'
22
		MachineDetection?: 'Enable' | 'Disable' | 'DetectMessageEnd'
23
		DetectionMode?: 'Premium' | 'Regular'
24
		AsyncAmd?: false | true
25
		AsyncAmdStatusCallback?: string
26
		AsyncAmdStatusCallbackMethod?: 'GET' | 'POST'
27
		MachineDetectionTimeout?: number
28
		MachineDetectionSpeechThreshold?: number
29
		MachineDetectionSpeechEndThreshold?: number
30
		MachineDetectionSilenceTimeout?: number
31
		CancelPlaybackOnMachineDetection?: false | true
32
		CancelPlaybackOnDetectMessageEnd?: false | true
33
		PreferredCodecs?: string
34
		Record?: false | true
35
		RecordingChannels?: 'mono' | 'dual'
36
		RecordingStatusCallback?: string
37
		RecordingStatusCallbackMethod?: 'GET' | 'POST'
38
		RecordingStatusCallbackEvent?: string
39
		RecordingTimeout?: number
40
		RecordingTrack?: 'inbound' | 'outbound' | 'both'
41
		SipAuthPassword?: string
42
		SipAuthUsername?: string
43
		Trim?: 'trim-silence' | 'do-not-trim'
44
	}
45
) {
46
	const url = new URL(`https://api.telnyx.com/v2/texml/calls/${application_id}`)
47

48
	const response = await fetch(url, {
49
		method: 'POST',
50
		headers: {
51
			'Content-Type': 'application/json',
52
			Authorization: 'Bearer ' + auth.apiKey
53
		},
54
		body: JSON.stringify(body)
55
	})
56
	if (!response.ok) {
57
		const text = await response.text()
58
		throw new Error(`${response.status} ${text}`)
59
	}
60
	return await response.json()
61
}
62