0

Query Free/Busy

by
Published today

Get the busy time ranges of one or more calendars over a time window.

Script gcal Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 48 minutes ago
1
//native
2

3
/**
4
 * Query Free/Busy
5
 * Return the busy intervals of one or more calendars (ids or email addresses; `primary` for the connected account) between two RFC 3339 timestamps, e.g. 2026-10-01T09:00:00Z. Needs the calendar.freebusy (or calendar.readonly) scope, which the calendar.events-only connection lacks.
6
 */
7
export async function main(
8
  auth: RT.Gcal,
9
  time_min: string,
10
  time_max: string,
11
  calendar_ids: string[] = ["primary"],
12
  time_zone: string | undefined
13
) {
14
  const body: { [key: string]: any } = {
15
    timeMin: time_min,
16
    timeMax: time_max,
17
    items: calendar_ids.map((id) => ({ id })),
18
  }
19
  if (time_zone !== undefined && time_zone !== "") body.timeZone = time_zone
20

21
  const response = await fetch(
22
    "https://www.googleapis.com/calendar/v3/freeBusy",
23
    {
24
      method: "POST",
25
      headers: {
26
        Authorization: `Bearer ${auth.token}`,
27
        "Content-Type": "application/json",
28
        Accept: "application/json",
29
      },
30
      body: JSON.stringify(body),
31
    }
32
  )
33

34
  if (!response.ok) {
35
    throw new Error(`${response.status} ${await response.text()}`)
36
  }
37

38
  return await response.json()
39
}
40