0

Copy single page

by
Published Oct 17, 2025

Copies a single page and its associated properties, permissions, attachments, and custom contents.

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
 * Copy single page
9
 * Copies a single page and its associated properties, permissions, attachments, and custom contents.
10
 */
11
export async function main(
12
	auth: Confluence,
13
	id: string,
14
	expand: string | undefined,
15
	body: {
16
		copyAttachments?: false | true
17
		copyPermissions?: false | true
18
		copyProperties?: false | true
19
		copyLabels?: false | true
20
		copyCustomContents?: false | true
21
		destination: {
22
			type: 'space' | 'existing_page' | 'parent_page' | 'parent_content'
23
			value: string
24
		}
25
		pageTitle?: string
26
		body?: {
27
			storage?: {
28
				value: string
29
				representation:
30
					| 'view'
31
					| 'export_view'
32
					| 'styled_view'
33
					| 'storage'
34
					| 'editor'
35
					| 'editor2'
36
					| 'anonymous_export_view'
37
					| 'wiki'
38
					| 'atlas_doc_format'
39
					| 'plain'
40
					| 'raw'
41
			}
42
			editor2?: {
43
				value: string
44
				representation:
45
					| 'view'
46
					| 'export_view'
47
					| 'styled_view'
48
					| 'storage'
49
					| 'editor'
50
					| 'editor2'
51
					| 'anonymous_export_view'
52
					| 'wiki'
53
					| 'atlas_doc_format'
54
					| 'plain'
55
					| 'raw'
56
			}
57
		}
58
	}
59
) {
60
	const url = new URL(`https://${auth.domain}/wiki/rest/api/content/${id}/copy`)
61
	for (const [k, v] of [['expand', expand]]) {
62
		if (v !== undefined && v !== '' && k !== undefined) {
63
			url.searchParams.append(k, v)
64
		}
65
	}
66
	const response = await fetch(url, {
67
		method: 'POST',
68
		headers: {
69
			'Content-Type': 'application/json',
70
			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
71
		},
72
		body: JSON.stringify(body)
73
	})
74
	if (!response.ok) {
75
		const text = await response.text()
76
		throw new Error(`${response.status} ${text}`)
77
	}
78
	return await response.text()
79
}
80