# Snippets: Confluence

> Oktul Code Studio script snippets for Confluence pages, copied from the console and run by its tests.
>
> Documentation for Oktul Code Studio, for Jira Cloud and Confluence Cloud, by Oktul: https://oktul.com/docs/plugins/code-studio/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](../../writing-scripts/) and the [script API reference](../../script-api/).

## 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.

```ts
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.

```ts
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

- [Writing scripts](../../writing-scripts/)
- [Requests to other systems](../../requests-to-other-systems/)
- [Script examples](../../script-examples/)

---

A problem or a question? Write to [support@oktul.com](mailto:support@oktul.com) or open a request in the [Help Center](https://oktul.atlassian.net/servicedesk/customer/portals). Both reach the same service desk, so either way the request gets a reference and an SLA measuring the response.
