0

Add member to group by groupId

by
Published Oct 17, 2025

Adds a user as a member in a group represented by its groupId **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**: User must be a site admin.

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 member to group by groupId
    9
     * Adds a user as a member in a group represented by its groupId
    10
    
    
    11
    **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
    12
    User must be a site admin.
    13
     */
    14
    export async function main(
    15
    	auth: Confluence,
    16
    	groupId: string | undefined,
    17
    	body: { accountId: string }
    18
    ) {
    19
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/group/userByGroupId`)
    20
    	for (const [k, v] of [['groupId', groupId]]) {
    21
    		if (v !== undefined && v !== '' && k !== undefined) {
    22
    			url.searchParams.append(k, v)
    23
    		}
    24
    	}
    25
    	const response = await fetch(url, {
    26
    		method: 'POST',
    27
    		headers: {
    28
    			'Content-Type': 'application/json',
    29
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    30
    		},
    31
    		body: JSON.stringify(body)
    32
    	})
    33
    	if (!response.ok) {
    34
    		const text = await response.text()
    35
    		throw new Error(`${response.status} ${text}`)
    36
    	}
    37
    	return await response.text()
    38
    }
    39