Last updated Dec 13, 2024

Connect cookbook

This cookbook provides snippets of code you can use in Confluence Cloud apps built with Connect.

Getting Confluence spaces

This retrieves a list of spaces from Confluence. If there are many spaces in your instance, you might need to page through the results to see all of them.

1
2
AP.request({
  url: '/rest/api/space',
  success: function(response) {
    // convert the string response to JSON
    response = JSON.parse(response);

    // dump out the response to the console
    console.log(response);
  },
  error: function() {
    console.log(arguments);
  }  
});

Getting specific spaces from Confluence

This code requests a specific Confluence space by space key. In this example, the space key is ds. The result also provides some high-level information about the space. If you're looking for more information about a space, you can find out about the content in the space in the next example, using /rest/api/space/{space.key}/content.

1
2
AP.request({
  url: '/rest/api/space/ds',
  success: function(response) {
    // convert the string response to JSON
    response = JSON.parse(response);

    // dump out the response to the console
    console.log(response);
  },
  error: function() {
    console.log(arguments);
  }   
});

Getting pages in a space

This code returns a collection for a given space key (ds in the example below) containing objects representing content such as pages and blog posts.

1
2
var space
AP.request({
  url: '/rest/api/space/ds/content',
  success: function(response) {
    // convert the string response to JSON
    response = JSON.parse(response);

    // dump out the response to the console
    console.log(response);
  },
  error: function() {
    console.log(arguments);
  }    
});

Using multiple webhooks

To listen for more than one event type, add webhooks to the webhooks module of your app descriptor, and add a route handler for each.

Here is an example modules section showing two webhooks:

1
2
"modules": {
         "webhooks": [
            {
                "event": "page_viewed",
                "url": "/page_viewed"
            },
            {
                "event": "comment_created",
                "url": "/comment_created"
            }
        ]
    }

Here are some minimal route handlers to match:

1
2
app.post('/page_viewed', addon.authenticate(), function (req, res) {
            console.log("Account ID " + req.body.userAccountId + " viewed page '" + req.body.page.title + "' at URL '" + req.body.page.self + "'.");
        }
    );

    app.post('/comment_created', addon.authenticate(), function (req, res) {
            console.log("Comment created.");
        }
    );

Rate this page: