0

Add new permission to space

by
Published Oct 17, 2025

Adds new permission to space. If the permission to be added is a group permission, the group can be identified by its group name or group id. Note: Apps cannot access this REST resource - including when utilizing user impersonation. **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**: 'Admin' permission for the space.

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 new permission to space
    9
     * Adds new permission to space.
    10
    
    
    11
    If the permission to be added is a group permission, the group can be identified
    12
    by its group name or group id.
    13
    
    
    14
    Note: Apps cannot access this REST resource - including when utilizing user impersonation.
    15
    
    
    16
    **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
    17
    'Admin' permission for the space.
    18
     */
    19
    export async function main(
    20
    	auth: Confluence,
    21
    	spaceKey: string,
    22
    	body: {
    23
    		subject: { type: 'user' | 'group'; identifier: string }
    24
    		operation: {
    25
    			key:
    26
    				| 'administer'
    27
    				| 'archive'
    28
    				| 'copy'
    29
    				| 'create'
    30
    				| 'delete'
    31
    				| 'export'
    32
    				| 'move'
    33
    				| 'purge'
    34
    				| 'purge_version'
    35
    				| 'read'
    36
    				| 'restore'
    37
    				| 'restrict_content'
    38
    				| 'update'
    39
    				| 'use'
    40
    			target: 'page' | 'blogpost' | 'comment' | 'attachment' | 'space'
    41
    		}
    42
    		_links?: {}
    43
    	}
    44
    ) {
    45
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/space/${spaceKey}/permission`)
    46
    
    
    47
    	const response = await fetch(url, {
    48
    		method: 'POST',
    49
    		headers: {
    50
    			'Content-Type': 'application/json',
    51
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    52
    		},
    53
    		body: JSON.stringify(body)
    54
    	})
    55
    	if (!response.ok) {
    56
    		const text = await response.text()
    57
    		throw new Error(`${response.status} ${text}`)
    58
    	}
    59
    	return await response.json()
    60
    }
    61