0

Create an assistant

by
Published Apr 8, 2025

Create a new AI Assistant.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * Create an assistant
7
 * Create a new AI Assistant.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	body: {
12
		name: string
13
		model: string
14
		description?: string
15
		instructions: string
16
		tools?:
17
			| {
18
					type: 'function'
19
					function: { name: string; description?: string; parameters?: {} }
20
			  }
21
			| {
22
					type: 'retrieval'
23
					retrieval: { bucket_ids: string[]; max_num_results?: number }
24
			  }[]
25
	}
26
) {
27
	const url = new URL(`https://api.telnyx.com/v2/ai/assistants`)
28

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