0

Search content by CQL

by
Published Oct 17, 2025

Returns the list of content that matches a Confluence Query Language (CQL) query.

Script confluence Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Confluence = {
3
	email: string
4
	apiToken: string
5
	domain: string
6
}
7
/**
8
 * Search content by CQL
9
 * Returns the list of content that matches a Confluence Query Language
10
(CQL) query.
11
 */
12
export async function main(
13
	auth: Confluence,
14
	cql: string | undefined,
15
	cqlcontext: string | undefined,
16
	expand: string | undefined,
17
	cursor: string | undefined,
18
	limit: string | undefined
19
) {
20
	const url = new URL(`https://${auth.domain}/wiki/rest/api/content/search`)
21
	for (const [k, v] of [
22
		['cql', cql],
23
		['cqlcontext', cqlcontext],
24
		['expand', expand],
25
		['cursor', cursor],
26
		['limit', limit]
27
	]) {
28
		if (v !== undefined && v !== '' && k !== undefined) {
29
			url.searchParams.append(k, v)
30
		}
31
	}
32
	const response = await fetch(url, {
33
		method: 'GET',
34
		headers: {
35
			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
36
		},
37
		body: undefined
38
	})
39
	if (!response.ok) {
40
		const text = await response.text()
41
		throw new Error(`${response.status} ${text}`)
42
	}
43
	return await response.json()
44
}
45