Gets project's logs

Executes a SQL query on the project's logs. Either the 'iso_timestamp_start' and 'iso_timestamp_end' parameters must be provided. If both are not provided, only the last 1 minute of logs will be queried. The timestamp range must be no more than 24 hours and is rounded to the nearest minute. If the range is more than 24 hours, a validation error will be thrown.

Script supabase Verified

by hugo697 ยท 10/17/2025

The script

Submitted by hugo697 Bun
Verified 207 days ago
1
//native
2
type Supabase = {
3
	key: string
4
}
5
/**
6
 * Gets project's logs
7
 * Executes a SQL query on the project's logs.
8

9
Either the 'iso_timestamp_start' and 'iso_timestamp_end' parameters must be provided.
10
If both are not provided, only the last 1 minute of logs will be queried.
11
The timestamp range must be no more than 24 hours and is rounded to the nearest minute. If the range is more than 24 hours, a validation error will be thrown.
12

13
 */
14
export async function main(
15
	auth: Supabase,
16
	ref: string,
17
	sql: string | undefined,
18
	iso_timestamp_start: string | undefined,
19
	iso_timestamp_end: string | undefined
20
) {
21
	const url = new URL(`https://api.supabase.com/v1/projects/${ref}/analytics/endpoints/logs.all`)
22
	for (const [k, v] of [
23
		['sql', sql],
24
		['iso_timestamp_start', iso_timestamp_start],
25
		['iso_timestamp_end', iso_timestamp_end]
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.key
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