🌟

Confluence Server REST API 活用ガイド:ページ操作の基本

2025/03/07に公開

はじめに

Confluenceを使っているけど、
「チームの情報がバラバラで探すのに時間がかかる...」
「毎回同じ資料を手作業で更新するのが面倒...」
「システム連携してConfluenceを自動更新できないだろうか?」

そんな悩みを抱える皆さん、朗報です。Confluence REST APIを活用すれば、これらの課題を一気に解決できます。

ConfluenceはAtlassian社が提供する、チームのナレッジを一元管理できる情報管理ツールです。そして、APIを使えば手作業では難しい様々な自動化が可能になります。

この記事では、Confluence Server REST APIを使ったページの基本操作(取得・作成・更新・削除)について、実践的なコード例とともに解説します。

参照ドキュメント:Confluence Server REST API 8.5.9

認証について

今回は、Basic認証を使用します。これは、ユーザー名とパスワードを組み合わせた認証方式です。

# Basic認証の基本形式
curl --user 'username:password'

1. ページの取得 (GET)

まずは既存ページの情報を取得する方法から見ていきましょう。

エンドポイント

GET /rest/api/content/{id}?expand=body.storage

必要なパラメータ

  • id: 取得するページのID(必須)
  • expand: 展開して取得する情報(オプション。例: body.storage, version など)

リクエスト

curl --request GET \
  --url 'https://your-confluence-server.com/rest/api/content/{pageId}' \
  --user 'username:password' \
  --header 'Accept: application/json'

特定の情報を展開して取得したい場合はexpandパラメータを使用します。

curl --request GET \
  --url 'https://your-confluence-server.com/rest/api/content/{pageId}?expand=body.storage' \
  --user 'username:password' \
  --header 'Accept: application/json'

レスポンス

{
  "id": "{pageId}",
  "type": "page",
  "status": "current",
  "title": "ページタイトル",
  "space": {
    "id": 10001,
    "key": "SPACEKEY",
    "name": "スペース名",
    "type": "global",
    "_links": {
      "webui": "/display/SPACEKEY",
      "self": "https://your-confluence-server.com/rest/api/space/SPACEKEY"
    },
    "_expandable": {
      "metadata": "",
      "icon": "",
      "description": "",
      "homepage": "/rest/api/content/{homepageId}"
    }
  },
  "history": {
    "latest": true,
    "createdBy": {
      "username": "creator",
      "displayName": "Creator Name"
    },
    "createdDate": "2025-03-01T10:00:00.000+09:00"
  },
  "version": {
    "by": {
      "username": "updater",
      "displayName": "Updater Name"
    },
    "when": "2025-03-02T14:30:00.000+09:00",
    "number": 1
  },
  "_links": {
    "webui": "/pages/viewpage.action?pageId={pageId}",
    "tinyui": "/x/AbCdEf",
    "self": "https://your-confluence-server.com/rest/api/content/{pageId}"
  },
  "_expandable": {
    "children": "/rest/api/content/{pageId}/child",
    "ancestors": "",
    "body": ""
  }
}

expand=body.storageを指定した場合のレスポンス(body部分のみ抜粋)

{
  "body": {
    "storage": {
      "value": "<p>ここにページの内容がHTMLで入ります</p>",
      "representation": "storage"
    },
    "_expandable": {
      "editor": "",
      "view": "",
      "export_view": ""
    }
  }
}

2. ページの作成 (POST)

エンドポイント

POST /rest/api/content

必要なパラメータ

  • type: コンテンツタイプ(必須、"page")
  • title: ページのタイトル(必須)
  • space.key: スペースキー(必須)
  • body.storage.value: ページの内容(必須)
  • body.storage.representation: 表現形式(必須、通常は "storage")
  • ancestors: 親ページ情報(オプション、階層化する場合は必須)

リクエスト

curl --request POST \
  --url 'https://your-confluence-server.com/rest/api/content' \
  --user 'username:password' \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  --data '{
    "type": "page",
    "title": "新しいページのタイトル",
    "space": {
        "key": "SPACEKEY"
    },
    "body": {
        "storage": {
            "value": "<p>ページの内容です</p>",
            "representation": "storage"
        }
    },
    "ancestors": [
        {
            "id": "{parentPageId}"
        }
    ]
}'

レスポンス

{
  "id": "{newPageId}",
  "type": "page",
  "status": "current",
  "title": "新しいページのタイトル",
  "space": {
    "id": 10001,
    "key": "SPACEKEY",
    "name": "スペース名",
    "type": "global",
    "_links": {
      "webui": "/display/SPACEKEY",
      "self": "https://your-confluence-server.com/rest/api/space/SPACEKEY"
    }
  },
  "history": {
    "latest": true,
    "createdBy": {
      "username": "creator",
      "displayName": "Creator Name"
    },
    "createdDate": "2025-03-06T15:00:00.000+09:00"
  },
  "version": {
    "by": {
      "username": "creator",
      "displayName": "Creator Name"
    },
    "when": "2025-03-06T15:00:00.000+09:00",
    "number": 1
  },
  "ancestors": [
    {
      "id": "{parentPageId}",
      "type": "page",
      "status": "current",
      "title": "親ページのタイトル"
    }
  ],
  "body": {
    "storage": {
      "value": "<p>ページの内容です</p>",
      "representation": "storage"
    }
  },
  "_links": {
    "webui": "/display/SPACEKEY/新しいページのタイトル",
    "edit": "/pages/resumedraft.action?draftId={newPageId}",
    "tinyui": "/x/GhIjKl",
    "self": "https://your-confluence-server.com/rest/api/content/{newPageId}"
  }
}

3. ページの更新 (PUT)

ページを更新する際は、必ず現在のバージョン番号+1を指定する必要があります。

エンドポイント

PUT /rest/api/content/{id}

必要なパラメータ

  • id: 更新するページのID(URL内、必須)
  • version.number: バージョン番号(現在のバージョン+1、必須)
  • title: 更新後のタイトル(オプション、変更する場合)
  • type: コンテンツタイプ(必須、"page")
  • body.storage.value: 更新後のページ内容(必須)
  • body.storage.representation: 表現形式(必須、通常は "storage")
  • ancestors: 親ページ情報(オプション、親ページを変更する場合)

リクエスト

curl --request PUT \
  --url 'https://your-confluence-server.com/rest/api/content/{pageId}' \
  --user 'username:password' \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  --data '{
    "version": {
        "number": 2
    },
    "title": "更新後のタイトル",
    "type": "page",
    "body": {
        "storage": {
            "value": "<p>更新後のページ内容です</p>",
            "representation": "storage"
        }
    }
}'

親ページを変更する場合

curl --request PUT \
  --url 'https://your-confluence-server.com/rest/api/content/{pageId}' \
  --user 'username:password' \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  --data '{
    "version": {
        "number": 2
    },
    "ancestors": [
        {
            "id": "{newParentPageId}"
        }
    ],
    "type": "page",
    "body": {
        "storage": {
            "value": "<p>更新後のページ内容です</p>",
            "representation": "storage"
        }
    }
}'

レスポンス

{
  "id": "{pageId}",
  "type": "page",
  "status": "current",
  "title": "更新後のタイトル",
  "space": {
    "id": 10001,
    "key": "SPACEKEY",
    "name": "スペース名",
    "type": "global"
  },
  "history": {
    "latest": true,
    "createdBy": {
      "username": "creator",
      "displayName": "Creator Name"
    },
    "createdDate": "2025-03-06T15:00:00.000+09:00"
  },
  "version": {
    "by": {
      "username": "updater",
      "displayName": "Updater Name"
    },
    "when": "2025-03-06T16:00:00.000+09:00",
    "number": 2
  },
  "body": {
    "storage": {
      "value": "<p>更新後のページ内容です</p>",
      "representation": "storage"
    }
  },
  "_links": {
    "webui": "/display/SPACEKEY/更新後のタイトル",
    "edit": "/pages/resumedraft.action?draftId={pageId}",
    "tinyui": "/x/GhIjKl",
    "self": "https://your-confluence-server.com/rest/api/content/{pageId}"
  }
}

4. ページの削除 (DELETE)

エンドポイント

DELETE /rest/api/content/{id}

必要なパラメータ

  • id: 削除するページのID(URL内、必須)

リクエスト

curl --request DELETE \
  --url 'https://your-confluence-server.com/rest/api/content/{pageId}' \
  --user 'username:password' \
  --header 'Accept: application/json'

レスポンス

削除が成功すると、通常はステータスコード204(No Content)が返され、レスポンスボディは空になります。

まとめ

Confluence Server REST APIを使うことで、ページの基本操作を自動化できます。これにより定期的なレポート作成や情報の一括更新など、様々な業務効率化が可能になります。この記事がConfluence APIを使った自動化の第一歩になれば幸いです。

Discussion