0

Create a branch

by
Published Apr 8, 2025

Creates a branch in the specified project.

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 branch
7
 * Creates a branch in the specified project.
8
 */
9
export async function main(
10
	auth: Neondb,
11
	project_id: string,
12
	body: {
13
		endpoints?: {
14
			type: 'read_only' | 'read_write'
15
			autoscaling_limit_min_cu?: number
16
			autoscaling_limit_max_cu?: number
17
			provisioner?: string
18
			suspend_timeout_seconds?: number
19
		}[]
20
		branch?: {
21
			parent_id?: string
22
			name?: string
23
			parent_lsn?: string
24
			parent_timestamp?: string
25
			protected?: false | true
26
			archived?: false | true
27
			schema_initialization_type?: 'empty'
28
		}
29
	} & { annotation_value?: {} }
30
) {
31
	const url = new URL(`https://console.neon.tech/api/v2/projects/${project_id}/branches`)
32

33
	const response = await fetch(url, {
34
		method: 'POST',
35
		headers: {
36
			'Content-Type': 'application/json',
37
			Authorization: 'Bearer ' + auth.apiKey
38
		},
39
		body: JSON.stringify(body)
40
	})
41
	if (!response.ok) {
42
		const text = await response.text()
43
		throw new Error(`${response.status} ${text}`)
44
	}
45
	return await response.json()
46
}
47