0

Get consumption metrics for each project

by
Published Apr 8, 2025

Retrieves consumption metrics for Scale and Business plan projects. History begins at the time of upgrade. Available for Scale and Business plan users only. Issuing a call to this API does not wake a project's compute endpoint.

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 consumption metrics for each project
7
 * Retrieves consumption metrics for Scale and Business plan projects. History begins at the time of upgrade.
8
Available for Scale and Business plan users only.
9
Issuing a call to this API does not wake a project's compute endpoint.
10

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