Confluence Rest API V2 - Struggling to create a page with the new editor

You can force the legacy editor (v1) or the new editor (v2) by setting/updating the “editor” content property.

You cannot set content properties with the V2 REST API (as far as I know) during page creation. Therefore, you have to create the page first and then check if the “editor” property already exists. If it does, update it; otherwise, create it.

I believe this to be the better solution if you want to force the editor choice. Many other HTML tags, besides <body>, can trigger the legacy editor. Additionally, Confluence might change their legacy-detection code in the future.

Here is some pseudo-code:

// Step 1: Create the page
page = post("https://{your-domain}/wiki/api/v2/pages", {
  "spaceId": "...",
  "status": "current",
  "title": "Interesting page title",
  "parentId": "...",
  "body": {
    "representation": "storage",
    "value": "<p>The page content</p>"
  }
});

// Step 2: Search for the "editor" property
search_for_editor_properties = get(`https://{your-domain}/wiki/api/v2/pages/${page.id}/properties?key=editor`);

if (search_for_editor_properties.results.length === 0) {
  // Step 3: If the editor property does not exist, create it
  post(`https://{your-domain}/wiki/api/v2/pages/${page.id}/properties`, {
    "key": "editor",
    "value": "v2"
  });
} else {
  // Step 4: If the editor property exists, update it
  const editor_property = search_for_editor_properties.results[0];
  put(`https://{your-domain}/wiki/api/v2/pages/${page.id}/properties/${editor_property.id}`, {
    "key": "editor",
    "value": "v2",
    "version": {
      "number": editor_property.version.number + 1
    }
  });
}

So my current process is:

  1. Create a draft
  2. Set the editor property (using the method above)
  3. Set the content
  4. Publish

This works very reliably but it is a pain that I now need to make multiple calls with the V2 REST API for something that I was able to do with one call with the old V1 API. Together with all the retry logic I have, this is very reliable but also very cumbersome. I’m just lucky that in my application, execution time isn’t really an issue.

3 Likes