0

Reject a call

by
Published Apr 8, 2025

Reject an incoming call. **Expected Webhooks (see [callback schema](https://developers.telnyx.com/api/call-control/reject-call#callbacks) below):** - `call.hangup`

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Reject a call
7
 * Reject an incoming call.
8

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

11
- `call.hangup`
12

13
 */
14
export async function main(
15
	auth: Telnyx,
16
	call_control_id: string,
17
	body: {
18
		client_state?: string
19
		command_id?: string
20
		cause: 'CALL_REJECTED' | 'USER_BUSY'
21
	}
22
) {
23
	const url = new URL(`https://api.telnyx.com/v2/calls/${call_control_id}/actions/reject`)
24

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