0

Create an organization API key

by
Published Apr 8, 2025

Creates an API key for the specified organization. The `key_name` is a user-specified name for the key. This method returns an `id` and `key`. The `key` is a randomly generated, 64-bit token required to access the Neon API. API keys can also be managed in the Neon Console. See [Manage API keys](https://neon.tech/docs/manage/api-keys/).

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 an organization API key
7
 * Creates an API key for the specified organization.
8
The `key_name` is a user-specified name for the key.
9
This method returns an `id` and `key`. The `key` is a randomly generated, 64-bit token required to access the Neon API.
10
API keys can also be managed in the Neon Console.
11
See [Manage API keys](https://neon.tech/docs/manage/api-keys/).
12

13
 */
14
export async function main(
15
	auth: Neondb,
16
	org_id: string,
17
	body: { key_name: string } & { project_id?: string }
18
) {
19
	const url = new URL(`https://console.neon.tech/api/v2/organizations/${org_id}/api_keys`)
20

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