1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Confluence APIでページを作成・編集・削除・検索する

Last updated at Posted at 2024-08-15

API Document

Prerequisite

Token

https://id.atlassian.com/manage-profile/security/api-tokens から取得

Env

ATLASSIAN_CONFLUENCE_URL=https://xxxx.atlassian.net/wiki/
ATLASSIAN_USERNAME=<youremail>
ATLASSIAN_API_TOKEN=<token>
CONFLUENCE_ACCESS_EMAIL_AND_TOKEN="${ATLASSIAN_USERNAME}:${ATLASSIAN_API_TOKEN}"
CONFLUENCE_SPACE_KEY=<your space key>

Getting Started

Get content (v1)

curl -X GET "${ATLASSIAN_CONFLUENCE_URL}/rest/api/content?spaceKey=$CONFLUENCE_SPACE_KEY" \
  -u $CONFLUENCE_ACCESS_EMAIL_AND_TOKEN \
  -H 'Accept: application/json'

Get Content by ID (v1)

PAGE_ID=xxx
curl -X GET "${ATLASSIAN_CONFLUENCE_URL}/rest/api/content/${PAGE_ID}?expand=body.storage" \
  -u $CONFLUENCE_ACCESS_EMAIL_AND_TOKEN \
  -H 'Accept: application/json'

Create a page with parent page

PARENT_PAGE_ID=xxx
curl -X POST "${ATLASSIAN_CONFLUENCE_URL}/rest/api/content" \
  -u  $CONFLUENCE_ACCESS_EMAIL_AND_TOKEN \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "title": "API POST TEST",
  "type": "page",
  "space": {
    "key": "${CONFLUENCE_SPACE_KEY}"
  },
  "status": "current",
  "ancestors": [
    {
      "id": "${PARENT_PAGE_ID}"
    }
  ],
  "body": {
    "storage": {
      "value": "<h3>test</h3><br/>test post from api",
      "representation": "storage"
    }
  }
}'

Update a page (v1)

https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/#update-a-page <- こちら

Delete a page (v1)

curl -v -S -u $CONFLUENCE_ACCESS_EMAIL_AND_TOKEN -X DELETE ${ATLASSIAN_CONFLUENCE_URL}/rest/api/content/<content_id>

Copy a page (v2)

  1. Get page by id: https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-page/#api-pages-id-get

  2. Create page: https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-page/#api-pages-post

spaceIdは、数字。 space keyではないので注意

Pythonの例:

import requests
import json
import os
# ConfluenceのベースURLとAPIエンドポイント
base_url = os.environ["ATLASSIAN_CONFLUENCE_URL"]
# Confluenceの認証情報
user_name, api_token = os.environ["ATLASSIAN_USERNAME"], os.environ["ATLASSIAN_API_TOKEN"]
source_page_id = '<source_page_id>'
destination_parent_page_id = '<parent_page_id>'
# This code sample uses the 'requests' library:
# http://docs.python-requests.org
import requests
from requests.auth import HTTPBasicAuth
import json
url = f"{base_url}/api/v2/pages/{source_page_id}?body-format=storage"
auth = HTTPBasicAuth(username=user_name, password=api_token)
headers = {
  "Accept": "application/json"
}
response = requests.request(
   "GET",
   url,
   headers=headers,
   auth=auth
)
source_page = response.json()
print(source_page)
# 新しいページのデータを作成する
new_page_data = {
    "spaceId": source_page["spaceId"],
    "title": f"Copy of {source_page['title']}",
    "parentId": source_page['parentId'],
    "body": {
        "value": source_page['body']['storage']['value'],
        "representation": "storage"
    }
}
# 新しいページを作成する
headers = {
    'Content-Type': 'application/json'
}
response = requests.post(f"{base_url}/api/v2/pages", headers=headers, auth=auth, data=json.dumps(new_page_data))
if response.status_code == 200 or response.status_code == 201:
    print("ページが正常にコピーされました。")
else:
    print(f"エラーが発生しました: {response.status_code}")
    print(response.text)

Get a specific Property (例. editor)

以下の例では、editor プロパティを取得する

import os
import requests
from requests.auth import HTTPBasicAuth
import json

base_url = os.environ["ATLASSIAN_CONFLUENCE_URL"]
page_id = <page_id>
property = "editor"
property_editor_url = f"{base_url}/api/v2/pages/{page_id}/properties?key={property}"
auth = HTTPBasicAuth(os.environ["ATLASSIAN_USERNAME"], os.environ["ATLASSIAN_API_TOKEN"])

headers = {
  "Accept": "application/json"
}

response = requests.request(
   "GET",
   property_editor_url,
   headers=headers,
   auth=auth
)

Update a property (例. editor)

以下の例ではeditor propertyを v2に変更する

import os
import requests
from requests.auth import HTTPBasicAuth
import json

page_id = <page_id>
base_url = os.environ["ATLASSIAN_CONFLUENCE_URL"]
response = requests.post(
    f"{base_url}/api/v2/pages/{page_id}/properties",
    headers={
        "Accept": "application/json",
        "Content-Type": "application/json"
    },
    auth=auth,
    data=json.dumps({
        "key": "editor",
        "value": "v2"
    })
)

Search by CQL

url = os.environ["CONFLUENCE_URL"]
username = os.environ["ATLASSIAN_USERNAME"]
password = os.environ["ATLASSIAN_PASSWORD"]
client = ConfluenceClient(url, username, password)
print(client.search_by_cql("space = SPACEKEY AND text ~ 'git'"))

ConfluenceのUI検索と同じCQL

siteSearch ~ 'keyword'

Ref

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?