0

Get User Statistics on Charging Sessions

by
Published Nov 5, 2024

Returns statistics about power consumption and price, binned by sessions for a single user and device, at a location. A session is defined by consecutive readings from the device showing consumption. It can typically take up to 15 minutes between a session ending in reality, and being reflected by this endpoint.

Script enode Verified

The script

Submitted by hugo697 Bun
Verified 581 days ago
1
//native
2
type Enode = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Enode,
8
	userId: string,
9
	startDate: string | undefined,
10
	endDate: string | undefined,
11
	locationId: string | undefined,
12
	id: string | undefined,
13
	type: 'charger' | 'vehicle' | 'hvac' | undefined
14
) {
15
	const url = new URL(
16
		`https://enode-api.production.enode.io/users/${userId}/statistics/charging/sessions`
17
	)
18

19
	for (const [k, v] of [
20
		['startDate', startDate],
21
		['endDate', endDate],
22
		['locationId', locationId],
23
		['id', id],
24
		['type', type]
25
	]) {
26
		if (v !== undefined && v !== '' && k !== undefined) {
27
			url.searchParams.append(k, v)
28
		}
29
	}
30

31
	const response = await fetch(url, {
32
		method: 'GET',
33
		headers: {
34
			Authorization: 'Bearer ' + auth.token
35
		},
36
		body: undefined
37
	})
38

39
	if (!response.ok) {
40
		const text = await response.text()
41
		throw new Error(`${response.status} ${text}`)
42
	}
43

44
	return await response.json()
45
}
46