Problem: Applying a Confluence Template to an Existing Page via Python

Amadeo Andres Granillo
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 18, 2025

I am working on automating Confluence page creation using Python. While I can create pages and retrieve templates, I’m struggling to apply an existing template (blueprint) to a newly created or existing Confluence page.

 

Setup

 

I’m using the Atlassian Python API (atlassian-python-api) to interact with Confluence.

 

Goal

Create a page in Confluence.

Apply a template (contentTemplateId) to that page.

 

Code Overview

 

1. Creating a Page

 

def create_page(self, title: str, space_key: str, content: str, parent_id: str = None):

    page_params = {

        'type': 'page',

        'title': title,

        'space': space_key,

        'body': {

            'storage': {

                'value': content,

                'representation': 'storage'

            }

        }

    }

    

    if parent_id:

        page_params['ancestors'] = [{'id': parent_id}]

 

    try:

        new_page = self.confluence.create_page(**page_params)

        print(f"Page created with ID: {new_page['id']}")

        return new_page['id']

    except Exception as e:

        print(f"Error creating page: {e}")

        return None

 

2. Applying a Template to an Existing Page

 

I tried using set_page_property to attach the template ID to the page:

 

def set_template_to_page(self, page_id: str, template_id: str):

    try:

        self.confluence.set_page_property(

            page_id=page_id,

            key='contentTemplateId',

            data={'value': template_id}

        )

        print(f"Template {template_id} applied to page {page_id}")

    except Exception as e:

        print(f"Error applying template: {e}")

 

3. Error Encountered

 

Despite setting the contentTemplateId, the template doesn’t apply. The page is created but without the template content or structure.

 

I also tried retrieving the page to verify if the template was attached:

 

def get_page_details(self, page_id: str):

    try:

        page = self.confluence.get_page_by_id(page_id)

        print(page)

    except Exception as e:

        print(f"Error retrieving page: {e}")

 

But the output shows content_template_id=None and no blueprint attached:

 

id='1855651970' title='Page with template' space_key='RED' content=None version=1content_template_id=None blueprint=None

 

Additionally, I encountered this error while attempting different approaches:

 

Error creating page: java.lang.NullPointerException: Cannot invoke "Object.hashCode()"because "<parameter1>" is null

 

What I’ve Tried

• Used set_page_property() with contentTemplateId.

•Attempted to include metadata in create_page() (unsupported).

•Verified template IDs by listing all available templates.

 

The Question

 

Has anyone successfully applied a Confluence template (blueprint) to a page using the API?

If so, how can I properly associate a template with a page in Confluence?

 

Environment

Python Library: atlassian-python-api

Confluence Version: Cloud (or Server/DC, specify here)

API Call: POST /rest/api/content

 

Any help would be appreciated! 😊

 

1 answer

0 votes
Mirek
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2025

Hi @Amadeo Andres Granillo 

According to REST API documentation

https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-template/

you probably should use templateId parameter when passing it and by design you should do it when creating a page request

So looking at your code you might want to try modifying it to this

def create_page(self, title: str, space_key: str, template_id: str, parent_id: str = None):

page_params = {
'type': 'page',
'title': title,
'space': space_key,
'body': {
'storage': {
'value': '', # Template should populate the content
'representation': 'storage'
}
},
'metadata': {
'templateId': template_id # Put here templateId
}
}

if parent_id:
page_params['ancestors'] = [{'id': parent_id}]

try:
new_page = self.confluence.create_page(**page_params)
print(f"Page created with ID: {new_page['id']}")

return new_page['id']
except Exception as e:
print(f"Error creating page: {e}")
return None

Hope it helps

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events