0

List Bases

by
Published today

List the bases the token can access, with each base's ID and permission level. Requires the schema.bases:read scope.

Script airtable Verified

The script

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

3
/**
4
 * List Bases
5
 * List the bases the token can access, with each base's ID and permission level. Requires the schema.bases:read scope.
6
 */
7
export async function main(auth: RT.Airtable) {
8
  const bases: any[] = []
9
  let offset: string | undefined = undefined
10
  do {
11
    const url = new URL("https://api.airtable.com/v0/meta/bases")
12
    if (offset) {
13
      url.searchParams.append("offset", offset)
14
    }
15
    const response = await fetch(url, {
16
      headers: {
17
        Authorization: `Bearer ${auth.apiKey}`,
18
        Accept: "application/json",
19
      },
20
    })
21
    if (!response.ok) {
22
      throw new Error(`${response.status} ${await response.text()}`)
23
    }
24
    const page = await response.json()
25
    bases.push(...(page.bases ?? []))
26
    offset = page.offset
27
  } while (offset)
28

29
  return { bases }
30
}
31