0

Find target entities related to a source entity

by
Published Oct 17, 2025

Returns all target entities that have a particular relationship to the source entity. Note, relationships are one way. For example, the following method finds all content that the current user has an 'ignore' relationship with: `GET /wiki/rest/api/relation/ignore/from/user/current/to/content` Note, 'ignore' is an example custom relationship type. **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**: Permission to view both the target entity and source entity.

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
     * Find target entities related to a source entity
    9
     * Returns all target entities that have a particular relationship to the
    10
    source entity. Note, relationships are one way.
    11
    
    
    12
    For example, the following method finds all content that the current user
    13
    has an 'ignore' relationship with:
    14
    `GET /wiki/rest/api/relation/ignore/from/user/current/to/content`
    15
    Note, 'ignore' is an example custom relationship type.
    16
    
    
    17
    **[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:
    18
    Permission to view both the target entity and source entity.
    19
     */
    20
    export async function main(
    21
    	auth: Confluence,
    22
    	relationName: string,
    23
    	sourceType: 'user' | 'content' | 'space',
    24
    	sourceKey: string,
    25
    	targetType: 'user' | 'content' | 'space',
    26
    	sourceStatus: string | undefined,
    27
    	targetStatus: string | undefined,
    28
    	sourceVersion: string | undefined,
    29
    	targetVersion: string | undefined,
    30
    	expand: string | undefined,
    31
    	start: string | undefined,
    32
    	limit: string | undefined
    33
    ) {
    34
    	const url = new URL(
    35
    		`https://${auth.domain}/wiki/rest/api/relation/${relationName}/from/${sourceType}/${sourceKey}/to/${targetType}`
    36
    	)
    37
    	for (const [k, v] of [
    38
    		['sourceStatus', sourceStatus],
    39
    		['targetStatus', targetStatus],
    40
    		['sourceVersion', sourceVersion],
    41
    		['targetVersion', targetVersion],
    42
    		['expand', expand],
    43
    		['start', start],
    44
    		['limit', limit]
    45
    	]) {
    46
    		if (v !== undefined && v !== '' && k !== undefined) {
    47
    			url.searchParams.append(k, v)
    48
    		}
    49
    	}
    50
    	const response = await fetch(url, {
    51
    		method: 'GET',
    52
    		headers: {
    53
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    54
    		},
    55
    		body: undefined
    56
    	})
    57
    	if (!response.ok) {
    58
    		const text = await response.text()
    59
    		throw new Error(`${response.status} ${text}`)
    60
    	}
    61
    	return await response.json()
    62
    }
    63