Snippets: Confluence
These are the snippets the console’s Snippets dialog offers under Confluence. Each is TypeScript; in a .js script the console shows the same code with its types removed. Every one runs unchanged in Code Studio’s own tests. How the API they call behaves is in writing scripts and the script API reference.
Create a page
Creates a page in a space from storage format. The v2 API takes the space’s id, which the space key leads to.
interface Spaces { results: { id: string }[];}
export default async function () { const { results } = await confluence.get<Spaces>('/wiki/api/v2/spaces', { query: { keys: 'DOCS' } }); if (results.length === 0) throw new Error('No space with the key DOCS.');
const page = await confluence.post<{ id: string }>('/wiki/api/v2/pages', { spaceId: results[0].id, status: 'current', title: `Weekly report ${new Date().toISOString().slice(0, 10)}`, body: { representation: 'storage', value: '<p>Written by Code Studio.</p>' }, }); return { pageId: page.id };}Update a page
Replaces a page’s body. Confluence wants the next version number, so the page is read first.
interface Page { id: string; title: string; status: string; version: { number: number };}
export default async function (event: unknown, context: ScriptContext) { const pageId = context.environment.vars.PAGE_ID ?? '123456'; const page = await confluence.get<Page>(`/wiki/api/v2/pages/${pageId}`); await confluence.put(`/wiki/api/v2/pages/${pageId}`, { id: page.id, status: page.status, title: page.title, body: { representation: 'storage', value: `<p>Updated ${new Date().toISOString().slice(0, 10)}</p>` }, version: { number: page.version.number + 1, message: 'Updated by Code Studio' }, }); return { pageId, version: page.version.number + 1 };}Related
A problem or a question? Write to support@oktul.com or open a request in the Help Center (opens in a new tab). Both reach the same service desk, so either way the request gets a reference and an SLA measuring the response.