Hey there, fellow developer! Ready to supercharge your Confluence experience with some JavaScript magic? You're in the right place. We're going to dive into building a Confluence API integration using the awesome confluence.js
package. This nifty tool will let you interact with Confluence programmatically, opening up a world of automation possibilities.
Before we jump in, make sure you've got:
Let's get our project off the ground:
Fire up your terminal and create a new directory:
mkdir confluence-api-integration
cd confluence-api-integration
Initialize your npm project:
npm init -y
Install the star of our show, the confluence.js
package:
npm install confluence.js
Now, let's set up our Confluence client:
const Confluence = require('confluence.js'); const confluence = new Confluence({ baseUrl: 'https://your-domain.atlassian.net/wiki', username: '[email protected]', password: 'your-api-token' });
Replace the placeholders with your actual Confluence domain, email, and API token. Easy peasy!
Let's grab a page and see what we've got:
async function getPage(pageId) { try { const page = await confluence.content.getContent({ id: pageId }); console.log('Page title:', page.title); } catch (error) { console.error('Oops! Something went wrong:', error); } }
Time to unleash your creativity:
async function createPage(spaceKey, title, content) { try { const newPage = await confluence.content.createContent({ space: { key: spaceKey }, title: title, type: 'page', body: { storage: { value: content, representation: 'storage' } } }); console.log('New page created with ID:', newPage.id); } catch (error) { console.error('Page creation failed:', error); } }
Let's give that page a makeover:
async function updatePage(pageId, newContent) { try { const page = await confluence.content.getContent({ id: pageId }); const updatedPage = await confluence.content.updateContent({ id: pageId, type: 'page', title: page.title, version: { number: page.version.number + 1 }, body: { storage: { value: newContent, representation: 'storage' } } }); console.log('Page updated successfully'); } catch (error) { console.error('Update failed:', error); } }
Sometimes, we need to say goodbye:
async function deletePage(pageId) { try { await confluence.content.deleteContent({ id: pageId }); console.log('Page deleted successfully'); } catch (error) { console.error('Deletion failed:', error); } }
Spice up your pages with some attachments:
async function addAttachment(pageId, filePath) { try { const attachment = await confluence.content.createAttachment({ id: pageId, file: filePath }); console.log('Attachment added:', attachment.title); } catch (error) { console.error('Attachment upload failed:', error); } }
Keep your content secure:
async function setPagePermissions(pageId, username, permission) { try { await confluence.content.restrictContent({ id: pageId, operation: 'update', restrictions: { user: [{ type: 'known', username: username }], operation: permission } }); console.log('Permissions updated successfully'); } catch (error) { console.error('Failed to update permissions:', error); } }
Find that needle in the haystack:
async function searchContent(query) { try { const results = await confluence.content.searchContent({ cql: query }); console.log('Search results:', results.results); } catch (error) { console.error('Search failed:', error); } }
Always wrap your API calls in try-catch blocks to handle errors gracefully. And remember, Confluence has rate limits, so be kind to the API. Consider implementing retries with exponential backoff for important operations.
Let's put it all together:
async function contentManager() { const spaceKey = 'YOUR_SPACE_KEY'; const pageTitle = 'My Awesome Page'; const content = '<p>Hello, Confluence!</p>'; // Create a new page const newPage = await createPage(spaceKey, pageTitle, content); // Update the page await updatePage(newPage.id, '<p>Updated content!</p>'); // Add an attachment await addAttachment(newPage.id, './my-image.png'); // Set permissions await setPagePermissions(newPage.id, '[email protected]', 'read'); // Search for the page await searchContent(`title ~ "${pageTitle}"`); // Delete the page (careful with this one!) // await deletePage(newPage.id); } contentManager().catch(console.error);
And there you have it! You're now equipped to build some seriously cool Confluence integrations. Remember, this is just scratching the surface. The confluence.js
package has a ton more features to explore.
Keep experimenting, and don't be afraid to dive into the official Confluence API documentation for even more ideas. Happy coding, and may your Confluence pages always be informative and your integrations smooth!