0

Create a role

by
Published Apr 8, 2025

Creates a Postgres role in the specified branch. You can obtain a `project_id` by listing the projects for your Neon account. You can obtain the `branch_id` by listing the project's branches. For related information, see [Manage roles](https://neon.tech/docs/manage/roles/). Connections established to the active compute endpoint will be dropped. If the compute endpoint is idle, the endpoint becomes active for a short period of time and is suspended afterward.

Script neondb Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Neondb = {
3
	apiKey: string
4
}
5
/**
6
 * Create a role
7
 * Creates a Postgres role in the specified branch.
8
You can obtain a `project_id` by listing the projects for your Neon account.
9
You can obtain the `branch_id` by listing the project's branches.
10
For related information, see [Manage roles](https://neon.tech/docs/manage/roles/).
11

12
Connections established to the active compute endpoint will be dropped.
13
If the compute endpoint is idle, the endpoint becomes active for a short period of time and is suspended afterward.
14

15
 */
16
export async function main(
17
	auth: Neondb,
18
	project_id: string,
19
	branch_id: string,
20
	body: { role: { name: string } }
21
) {
22
	const url = new URL(
23
		`https://console.neon.tech/api/v2/projects/${project_id}/branches/${branch_id}/roles`
24
	)
25

26
	const response = await fetch(url, {
27
		method: 'POST',
28
		headers: {
29
			'Content-Type': 'application/json',
30
			Authorization: 'Bearer ' + auth.apiKey
31
		},
32
		body: JSON.stringify(body)
33
	})
34
	if (!response.ok) {
35
		const text = await response.text()
36
		throw new Error(`${response.status} ${text}`)
37
	}
38
	return await response.json()
39
}
40