Bootstrap FreeKB - Atlassian - List Confluence Pages using Python
Atlassian - List Confluence Pages using Python

Updated:   |  Atlassian articles

This assumes you have already created an API token and have a base 64 encoded string of the API token. Check out my article FreeKB - Atlassian - Create API token.

Using the base64 encoded string, you should now be able to submit a GET request and get JSON response. For example, to GET Confluence Pages.

More GET request examples here => The Confluence Cloud REST API

import requests

response = requests.get(
  f"https://acme.atlassian.net/wiki/api/v2/pages",
  headers={ "Accept": "application/json", "Authorization": "Basic <your base64 encoded API token>" }
)
print(response.json())

 

Something like this should be returned.

{
  "results": [
    {
      "parentId": null,
      "spaceId": "15335479",
      "ownerId": "557058:a9d57db5-f0d2-4c9b-b526-18382bec4da0",
      "createdAt": "2022-10-13T20:11:24.718Z",
      "authorId": "557058:a9d57db5-f0d2-4c9b-b526-18382bec4da0",
      "parentType": null,
      "version": {
        "number": 2,
        "message": "",
        "minorEdit": false,
        "authorId": "557058:a9d57db5-f0d2-4c9b-b526-18382bec4da0",
        "createdAt": "2024-10-13T20:11:30.241Z",
        "ncsStepVersion": null
      },
      "lastOwnerId": null,
      "position": 39,
      "body": {},
      "status": "current",
      "title": "my page",
      "id": "15335585",
      "_links": {
        "editui": "/pages/resumedraft.action?draftId=15335585",
        "webui": "/spaces/A1/overview",
        "edituiv2": "/spaces/A1/pages/edit-v2/15335585",
        "tinyui": "/x/oQDq"
      }
    }
  ],
  "_links": {
    "next": "/wiki/api/v2/pages?cursor=eyJpZCI6IjE1MzM1NTg1IiwiY29udGVudE9yZGVyIjoiaWQiLCJjb250ZW50T3JkZXJWYWx1ZSI6MTUzMzU1ODV9",
    "base": "https://acme.atlassian.net/wiki"
  }
}

 

By default, the first 25 spaces will be returned. Notice in the above output there is a "next" endpoint. The "next" endpoint can be used to return the next 25 spaces. In this scenario, it probably makes sense to use a while loop to loop through 25 spaces at a time.

import requests

def return_json(endpoint):
  response = requests.get(
    f"https://acme.atlassian.net{endpoint}",
    headers={ "Accept": "application/json", "Authorization": "Basic <your base64 encoded API token>" }
  )
  return response.json()

match = False
endpoint = "/wiki/api/v2/pages"

while match == False:
  return_json_response = return_json(endpoint)

  # KeyError will be raised if '_links' does not contain 'next' meaning there isn't a "next" endpoint
  # In this scenario, let's set last_page to True
  try:
    endpoint = return_json_response['_links']['next']

  except KeyError:
    last_page = True
    pass  

  for item in return_json_response['results']:
    print(f"item['name'] = {item['name']}")

  if last_page == True:
    print("reached the last page")
    break

 

If you want to return pages in a space, you can submit a GET request to the spaces endpoint to get the space ID.

curl \
--request GET \
--header "Accept: application/json" \
--header "Authorization: Basic <base64 encoded string>" \
--url "https://acme.atlassian.net/wiki/api/v2/spaces"| jq

 

For example, let's say the space ID is 12345678. You can then submit a GET request to the pages endpoint including the space URL paramater to only return the pages in the space.

import requests

def return_json(endpoint):
  response = requests.get(
    f"https://acme.atlassian.net{endpoint}",
    headers={ "Accept": "application/json", "Authorization": "Basic <your base64 encoded API token>" }
  )
  return response.json()

match = False
endpoint = "/wiki/api/v2/pages"

while match == False:
  return_json_response = return_json(endpoint)

  # KeyError will be raised if '_links' does not contain 'next' meaning there isn't a "next" endpoint
  # In this scenario, let's set last_page to True
  try:
    endpoint = return_json_response['_links']['next']

  except KeyError:
    last_page = True
    pass  

  for item in return_json_response['results']:
    print(f"item['name'] = {item['name']}")
    if re.match('^my Space$', item['name'], re.IGNORECASE):
      space_id = item['id']
      break
    
  if last_page == True:
    print("reached the last page")
    break
    
endpoint = f"/wiki/api/v2/pages?space-id={space_id}"    
    
while match == False:
  return_json_response = return_json(endpoint)

  # KeyError will be raised if '_links' does not contain 'next' meaning there isn't a "next" endpoint
  # In this scenario, let's set last_page to True
  try:
    endpoint = return_json_response['_links']['next']

  except KeyError:
    last_page = True
    pass  

  for item in return_json_response['results']:
    print(f"item['name'] = {item['name']}")

  if last_page == True:
    print("reached the last page")
    break    

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 054629 in the box below so that we can be sure you are a human.