0

Upload a document

by
Published Apr 8, 2025

Upload a document.Uploaded files must be linked to a service within 30 minutes or they will be automatically deleted.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Upload a document
7
 * Upload a document.Uploaded files must be linked to a service within 30 minutes or they will be automatically deleted.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	body:
12
		| { url: string; filename?: string; customer_reference?: string }
13
		| { file: string; filename?: string; customer_reference?: string }
14
) {
15
	const url = new URL(`https://api.telnyx.com/v2/documents`)
16

17
	const formData = new FormData()
18
	for (const [k, v] of Object.entries(body)) {
19
		if (v !== undefined && v !== '') {
20
			formData.append(k, String(v))
21
		}
22
	}
23
	const response = await fetch(url, {
24
		method: 'POST',
25
		headers: {
26
			'Content-Type': 'application/json',
27
			Authorization: 'Bearer ' + auth.apiKey
28
		},
29
		body: JSON.stringify(body)
30
	})
31
	if (!response.ok) {
32
		const text = await response.text()
33
		throw new Error(`${response.status} ${text}`)
34
	}
35
	return await response.json()
36
}
37