0

Hangup call

by
Published Apr 8, 2025

Hang up the call. **Expected Webhooks (see [callback schema](https://developers.telnyx.com/api/call-control/hangup-call#callbacks) below):** - `call.hangup` - `call.recording.saved`

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Hangup call
7
 * Hang up the call.
8

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

11
- `call.hangup`
12
- `call.recording.saved`
13

14
 */
15
export async function main(
16
	auth: Telnyx,
17
	call_control_id: string,
18
	body: { client_state?: string; command_id?: string }
19
) {
20
	const url = new URL(`https://api.telnyx.com/v2/calls/${call_control_id}/actions/hangup`)
21

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