0

Create asynchronous content body conversion tasks in bulk

by
Published Oct 17, 2025

Asynchronously converts content bodies from one format to another format in bulk.

Script confluence Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Confluence = {
3
	email: string
4
	apiToken: string
5
	domain: string
6
}
7
/**
8
 * Create asynchronous content body conversion tasks in bulk
9
 * Asynchronously converts content bodies from one format to another format in bulk.
10
 */
11
export async function main(
12
	auth: Confluence,
13
	body: {
14
		to: string
15
		allowCache?: false | true
16
		spaceKeyContext?: string
17
		contentIdContext?: string
18
		embeddedContentRender?: 'current' | 'version-at-save'
19
		expand?: string[]
20
		body: {
21
			value: string
22
			representation:
23
				| 'view'
24
				| 'export_view'
25
				| 'styled_view'
26
				| 'storage'
27
				| 'editor'
28
				| 'editor2'
29
				| 'anonymous_export_view'
30
				| 'wiki'
31
				| 'atlas_doc_format'
32
				| 'plain'
33
				| 'raw'
34
		}
35
	}[]
36
) {
37
	const url = new URL(`https://${auth.domain}/wiki/rest/api/contentbody/convert/async/bulk/tasks`)
38

39
	const response = await fetch(url, {
40
		method: 'POST',
41
		headers: {
42
			'Content-Type': 'application/json',
43
			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
44
		},
45
		body: JSON.stringify(body)
46
	})
47
	if (!response.ok) {
48
		const text = await response.text()
49
		throw new Error(`${response.status} ${text}`)
50
	}
51
	return await response.json()
52
}
53