0

Add or update document

by
Published Aug 26, 2024

The ID of the document you want to insert or replace

Script dust Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 652 days ago
1
type Dust = {
2
	apiKey: string
3
	workspaceId: string
4
}
5

6
export async function main(
7
	resource: Dust,
8
	params: {
9
		datasource: string
10
		documentId: string
11
		content: string
12
		sourceUrl?: string
13
		tags?: string[]
14
	}
15
) {
16
	const { datasource, documentId, content, sourceUrl, tags } = params
17
	const endpoint = `https://dust.tt/api/v1/w/${resource.workspaceId}/data_sources/${datasource}/documents/${documentId}`
18

19
	const body = {
20
		text: content,
21
		source_url: sourceUrl,
22
		tags: tags
23
	}
24

25
	const response = await fetch(endpoint, {
26
		method: 'POST',
27
		headers: {
28
			accept: 'application/json',
29
			'Content-Type': 'application/json',
30
			Authorization: `Bearer ${resource.apiKey}`
31
		},
32
		body: JSON.stringify(body)
33
	})
34

35
	if (!response.ok) {
36
		throw new Error(`HTTP error! status: ${response.status}`)
37
	}
38

39
	const data = await response.json()
40

41
	return data.document
42
}
43