0

SIP Refer a call

by
Published Apr 8, 2025

Initiate a SIP Refer on a Call Control call. You can initiate a SIP Refer at any point in the duration of a call. **Expected Webhooks (see [callback schema](https://developers.telnyx.com/api/call-control/refer-call#callbacks) below):** - `call.refer.started` - `call.refer.completed` - `call.refer.failed`

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * SIP Refer a call
7
 * Initiate a SIP Refer on a Call Control call. You can initiate a SIP Refer at any point in the duration of a call.
8

9
**Expected Webhooks (see [callback schema](https://developers.telnyx.com/api/call-control/refer-call#callbacks) below):**
10

11
- `call.refer.started`
12
- `call.refer.completed`
13
- `call.refer.failed`
14

15
 */
16
export async function main(
17
	auth: Telnyx,
18
	call_control_id: string,
19
	body: {
20
		sip_address: string
21
		client_state?: string
22
		command_id?: string
23
		custom_headers?: { name: string; value: string }[]
24
		sip_auth_username?: string
25
		sip_auth_password?: string
26
		sip_headers?: { name: 'User-to-User'; value: string }[]
27
	}
28
) {
29
	const url = new URL(`https://api.telnyx.com/v2/calls/${call_control_id}/actions/refer`)
30

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