0

Get Space Labels

by
Published Oct 17, 2025

Returns a list of labels associated with a space. Can provide a prefix as well as other filters to select different types of labels.

Script confluence
  • Submitted by hugo697 Bun
    Created 284 days ago
    1
    //native
    2
    type Confluence = {
    3
    	email: string
    4
    	apiToken: string
    5
    	domain: string
    6
    }
    7
    /**
    8
     * Get Space Labels
    9
     * Returns a list of labels associated with a space. Can provide a prefix as well as other filters to
    10
    select different types of labels.
    11
     */
    12
    export async function main(
    13
    	auth: Confluence,
    14
    	spaceKey: string,
    15
    	prefix: 'global' | 'my' | 'team' | undefined,
    16
    	start: string | undefined,
    17
    	limit: string | undefined
    18
    ) {
    19
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/space/${spaceKey}/label`)
    20
    	for (const [k, v] of [
    21
    		['prefix', prefix],
    22
    		['start', start],
    23
    		['limit', limit]
    24
    	]) {
    25
    		if (v !== undefined && v !== '' && k !== undefined) {
    26
    			url.searchParams.append(k, v)
    27
    		}
    28
    	}
    29
    	const response = await fetch(url, {
    30
    		method: 'GET',
    31
    		headers: {
    32
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    33
    		},
    34
    		body: undefined
    35
    	})
    36
    	if (!response.ok) {
    37
    		const text = await response.text()
    38
    		throw new Error(`${response.status} ${text}`)
    39
    	}
    40
    	return await response.json()
    41
    }
    42