0

Create an API key

by
Published Apr 8, 2025

Creates an API key. 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 API key
7
 * Creates an API key.
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(auth: Neondb, body: { key_name: string }) {
15
	const url = new URL(`https://console.neon.tech/api/v2/api_keys`)
16

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