0

Send a fax

by
Published Apr 8, 2025

Send a fax. Files have size limits and page count limit validations. If a file is bigger than 50MB or has more than 350 pages it will fail with `file_size_limit_exceeded` and `page_count_limit_exceeded` respectively. **Expected Webhooks:** - `fax.queued` - `fax.media.processed` - `fax.sending.started` - `fax.delivered` - `fax.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
 * Send a fax
7
 * Send a fax. Files have size limits and page count limit validations. If a file is bigger than 50MB or has more than 350 pages it will fail with `file_size_limit_exceeded` and `page_count_limit_exceeded` respectively. 
8

9
**Expected Webhooks:**
10

11
- `fax.queued`
12
- `fax.media.processed`
13
- `fax.sending.started`
14
- `fax.delivered`
15
- `fax.failed`
16

17
 */
18
export async function main(
19
	auth: Telnyx,
20
	body: {
21
		connection_id: string
22
		media_url?: string
23
		media_name?: string
24
		to: string
25
		from: string
26
		from_display_name?: string
27
		quality?: 'normal' | 'high' | 'very_high' | 'ultra_light' | 'ultra_dark'
28
		t38_enabled?: false | true
29
		monochrome?: false | true
30
		store_media?: false | true
31
		store_preview?: false | true
32
		preview_format?: 'pdf' | 'tiff'
33
		webhook_url?: string
34
		client_state?: string
35
	}
36
) {
37
	const url = new URL(`https://api.telnyx.com/v2/faxes`)
38

39
	const formData = new FormData()
40
	for (const [k, v] of Object.entries(body)) {
41
		if (v !== undefined && v !== '') {
42
			formData.append(k, String(v))
43
		}
44
	}
45
	const response = await fetch(url, {
46
		method: 'POST',
47
		headers: {
48
			'Content-Type': 'application/json',
49
			Authorization: 'Bearer ' + auth.apiKey
50
		},
51
		body: JSON.stringify(body)
52
	})
53
	if (!response.ok) {
54
		const text = await response.text()
55
		throw new Error(`${response.status} ${text}`)
56
	}
57
	return await response.json()
58
}
59