0

Restore content version

by
Published Oct 17, 2025

Restores a historical version to be the latest version. That is, a new version is created with the content of the historical version. **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**: Permission to update the content.

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
 * Restore content version
9
 * Restores a historical version to be the latest version. That is, a new version
10
is created with the content of the historical version.
11

12
**[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
13
Permission to update the content.
14
 */
15
export async function main(
16
	auth: Confluence,
17
	id: string,
18
	expand: string | undefined,
19
	body: {
20
		operationKey: 'restore'
21
		params: {
22
			versionNumber: number
23
			message: string
24
			restoreTitle?: false | true
25
		}
26
	}
27
) {
28
	const url = new URL(`https://${auth.domain}/wiki/rest/api/content/${id}/version`)
29
	for (const [k, v] of [['expand', expand]]) {
30
		if (v !== undefined && v !== '' && k !== undefined) {
31
			url.searchParams.append(k, v)
32
		}
33
	}
34
	const response = await fetch(url, {
35
		method: 'POST',
36
		headers: {
37
			'Content-Type': 'application/json',
38
			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
39
		},
40
		body: JSON.stringify(body)
41
	})
42
	if (!response.ok) {
43
		const text = await response.text()
44
		throw new Error(`${response.status} ${text}`)
45
	}
46
	return await response.json()
47
}
48