0

Create Blog Post

by
Published today

Create a new blog post in a space.

Script confluence Verified

The script

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

3
/**
4
 * Create Blog Post
5
 * Create a new blog post in a space. The body is supplied as a content string in the chosen representation (default "storage", i.e. Confluence storage HTML).
6
 */
7
export async function main(
8
  auth: RT.Confluence,
9
  space_id: string,
10
  title: string,
11
  body: string,
12
  status: "current" | "draft" | undefined,
13
  representation: "storage" | "atlas_doc_format" | undefined
14
) {
15
  const base = auth.baseUrl.replace(/\/$/, "")
16
  const url = new URL(`${base}/wiki/api/v2/blogposts`)
17

18
  const payload = {
19
    spaceId: space_id,
20
    status: status ?? "current",
21
    title,
22
    body: {
23
      representation: representation ?? "storage",
24
      value: body,
25
    },
26
  }
27

28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      Authorization: "Basic " + btoa(`${auth.email}:${auth.apiToken}`),
32
      "Content-Type": "application/json",
33
      Accept: "application/json",
34
    },
35
    body: JSON.stringify(payload),
36
  })
37

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

42
  return await response.json()
43
}
44