0

Add labels to a space

by
Published Oct 17, 2025

Adds labels to a piece of content. Does not modify the existing labels. Notes: - Labels can also be added when creating content ([Create content](#api-content-post)). - Labels can be updated when updating content ([Update content](#api-content-id-put)). This will delete the existing labels and replace them with the labels in the request. **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**: Permission to update the content.

Script confluence
  • Submitted by hugo697 Bun
    Created 268 days ago
    1
    //native
    2
    type Confluence = {
    3
    	email: string
    4
    	apiToken: string
    5
    	domain: string
    6
    }
    7
    /**
    8
     * Add labels to a space
    9
     * Adds labels to a piece of content. Does not modify the existing labels.
    10
    
    
    11
    Notes:
    12
    
    
    13
    - Labels can also be added when creating content ([Create content](#api-content-post)).
    14
    - Labels can be updated when updating content ([Update content](#api-content-id-put)).
    15
    This will delete the existing labels and replace them with the labels in
    16
    the request.
    17
    
    
    18
    **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
    19
    Permission to update the content.
    20
     */
    21
    export async function main(
    22
    	auth: Confluence,
    23
    	spaceKey: string,
    24
    	body: { prefix: string; name: string }[]
    25
    ) {
    26
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/space/${spaceKey}/label`)
    27
    
    
    28
    	const response = await fetch(url, {
    29
    		method: 'POST',
    30
    		headers: {
    31
    			'Content-Type': 'application/json',
    32
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    33
    		},
    34
    		body: JSON.stringify(body)
    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