0

Get Page by ID

by
Published today

Retrieve a single page by its ID, optionally returning its body in a specific format or a previous version.

Script confluence Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 hours ago
1
//native
2

3
/**
4
 * Get Page by ID
5
 * Retrieve a single page by its ID, optionally returning its body in a specific format or a previous version.
6
 */
7
export async function main(
8
  auth: RT.Confluence,
9
  page_id: string,
10
  body_format:
11
    | "storage"
12
    | "atlas_doc_format"
13
    | "view"
14
    | "export_view"
15
    | "anonymous_export_view"
16
    | "styled_view"
17
    | "editor"
18
    | undefined,
19
  version: number | undefined,
20
  get_draft: boolean | undefined
21
) {
22
  const base = auth.baseUrl.replace(/\/$/, "")
23
  const url = new URL(`${base}/wiki/api/v2/pages/${page_id}`)
24
  if (body_format !== undefined)
25
    url.searchParams.append("body-format", body_format)
26
  if (version !== undefined) url.searchParams.append("version", String(version))
27
  if (get_draft !== undefined)
28
    url.searchParams.append("get-draft", String(get_draft))
29

30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: "Basic " + btoa(`${auth.email}:${auth.apiToken}`),
34
      Accept: "application/json",
35
    },
36
  })
37

38
  if (!response.ok) {
39
    throw new Error(`${response.status} ${await response.text()}`)
40
  }
41

42
  return await response.json()
43
}
44