0

Get account consumption metrics

by
Published Apr 8, 2025

Retrieves consumption metrics for Scale and Business plan accounts. History begins at the time of upgrade. Available for Scale and Business plan users only.

Script neondb Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Neondb = {
3
	apiKey: string
4
}
5
/**
6
 * Get account consumption metrics
7
 * Retrieves consumption metrics for Scale and Business plan accounts. History begins at the time of upgrade.
8
Available for Scale and Business plan users only.
9

10
 */
11
export async function main(
12
	auth: Neondb,
13
	from: string | undefined,
14
	to: string | undefined,
15
	granularity: 'hourly' | 'daily' | 'monthly' | undefined,
16
	org_id: string | undefined,
17
	include_v1_metrics: string | undefined
18
) {
19
	const url = new URL(`https://console.neon.tech/api/v2/consumption_history/account`)
20
	for (const [k, v] of [
21
		['from', from],
22
		['to', to],
23
		['granularity', granularity],
24
		['org_id', org_id],
25
		['include_v1_metrics', include_v1_metrics]
26
	]) {
27
		if (v !== undefined && v !== '' && k !== undefined) {
28
			url.searchParams.append(k, v)
29
		}
30
	}
31
	const response = await fetch(url, {
32
		method: 'GET',
33
		headers: {
34
			Authorization: 'Bearer ' + auth.apiKey
35
		},
36
		body: undefined
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44