0

Get CPU usage

by
Published Oct 17, 2025

Get CPU usage for one or more resources.

Script render Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Render = {
3
	apiKey: string
4
}
5
/**
6
 * Get CPU usage
7
 * Get CPU usage for one or more resources.
8

9
 */
10
export async function main(
11
	auth: Render,
12
	startTime: string | undefined,
13
	endTime: string | undefined,
14
	resolutionSeconds: string | undefined,
15
	resource: string | undefined,
16
	service: string | undefined,
17
	instance: string | undefined,
18
	aggregationMethod: 'AVG' | 'MAX' | 'MIN' | undefined
19
) {
20
	const url = new URL(`https://api.render.com/v1/metrics/cpu`)
21
	for (const [k, v] of [
22
		['startTime', startTime],
23
		['endTime', endTime],
24
		['resolutionSeconds', resolutionSeconds],
25
		['resource', resource],
26
		['service', service],
27
		['instance', instance],
28
		['aggregationMethod', aggregationMethod]
29
	]) {
30
		if (v !== undefined && v !== '' && k !== undefined) {
31
			url.searchParams.append(k, v)
32
		}
33
	}
34
	const response = await fetch(url, {
35
		method: 'GET',
36
		headers: {
37
			Authorization: 'Bearer ' + auth.apiKey
38
		},
39
		body: undefined
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