0

Retrieve activity data for a given user ID

by
Published Oct 17, 2025

Fetches completed workout sessions, with a defined start and end time and activity type (e.g. running, cycling, etc.)

Script terra Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Retrieve activity data for a given user ID
4
 * Fetches completed workout sessions, with a defined start and end time and activity type (e.g. running, cycling, etc.)
5
 */
6
export async function main(
7
	auth: RT.Terra,
8
	user_id: string | undefined,
9
	start_date: string | undefined,
10
	end_date?: string | undefined,
11
	to_webhook?: string | undefined,
12
	with_samples?: string | undefined
13
) {
14
	const url = new URL(`https://api.tryterra.co/v2/activity`)
15
	for (const [k, v] of [
16
		['user_id', user_id],
17
		['start_date', start_date],
18
		['end_date', end_date],
19
		['to_webhook', to_webhook],
20
		['with_samples', with_samples]
21
	]) {
22
		if (v !== undefined && v !== '') {
23
			url.searchParams.append(k, v)
24
		}
25
	}
26
	const response = await fetch(url, {
27
		method: 'GET',
28
		headers: {
29
			'dev-id': auth.devId,
30
			'X-api-key': auth.apiKey
31
		},
32
		body: undefined
33
	})
34
	if (!response.ok) {
35
		const text = await response.text()
36
		throw new Error(`${response.status} ${text}`)
37
	}
38
	return await response.json()
39
}
40