0

Update stored media

by
Published Apr 8, 2025

Updates a stored media file.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Update stored media
7
 * Updates a stored media file.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	media_name: string,
12
	body: { media_url?: string; ttl_secs?: number }
13
) {
14
	const url = new URL(`https://api.telnyx.com/v2/media/${media_name}`)
15

16
	const formData = new FormData()
17
	for (const [k, v] of Object.entries(body)) {
18
		if (v !== undefined && v !== '') {
19
			formData.append(k, String(v))
20
		}
21
	}
22
	const response = await fetch(url, {
23
		method: 'PUT',
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