0

Update workspace member role

by
Published Apr 8, 2025

Update a workspace member role. Workspaces must always have at least one owner, so this operation will fail if trying to remove owner role from the last owner in the workspace.

Script xata Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Xata = {
3
	apiKey: string
4
}
5
/**
6
 * Update workspace member role
7
 * Update a workspace member role. Workspaces must always have at least one owner, so this operation will fail if trying to remove owner role from the last owner in the workspace.
8

9
 */
10
export async function main(
11
	auth: Xata,
12
	workspace_id: string,
13
	user_id: string,
14
	body: { role: 'owner' | 'maintainer' }
15
) {
16
	const url = new URL(`https://api.xata.io/workspaces/${workspace_id}/members/${user_id}`)
17

18
	const response = await fetch(url, {
19
		method: 'PUT',
20
		headers: {
21
			'Content-Type': 'application/json',
22
			Authorization: 'Bearer ' + auth.apiKey
23
		},
24
		body: JSON.stringify(body)
25
	})
26
	if (!response.ok) {
27
		const text = await response.text()
28
		throw new Error(`${response.status} ${text}`)
29
	}
30
	return await response.text()
31
}
32