0

Create organization invitations

by
Published Apr 8, 2025

Creates invitations for a specific organization. If the invited user has an existing account, they automatically join as a member. If they don't yet have an account, they are invited to create one, after which they become a member. Each invited user receives an email notification.

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 organization invitations
7
 * Creates invitations for a specific organization.
8
If the invited user has an existing account, they automatically join as a member.
9
If they don't yet have an account, they are invited to create one, after which they become a member.
10
Each invited user receives an email notification.
11

12
 */
13
export async function main(
14
	auth: Neondb,
15
	org_id: string,
16
	body: { invitations: { email: string; role: 'admin' | 'member' }[] }
17
) {
18
	const url = new URL(`https://console.neon.tech/api/v2/organizations/${org_id}/invitations`)
19

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