0

List Tables (Get Base Schema)

by
Published today

Get the schema of a base: every table with its fields (IDs, names, types, options) and views. 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
export type DynSelect_base_id = string
4

5
// Dropdown of the bases the token can access.
6
export async function base_id(auth: RT.Airtable) {
7
  const options: { value: string; label: string }[] = []
8
  let offset: string | undefined = undefined
9
  do {
10
    const url = new URL("https://api.airtable.com/v0/meta/bases")
11
    if (offset) {
12
      url.searchParams.append("offset", offset)
13
    }
14
    const response = await fetch(url, {
15
      headers: {
16
        Authorization: `Bearer ${auth.apiKey}`,
17
        Accept: "application/json",
18
      },
19
    })
20
    if (!response.ok) {
21
      throw new Error(`${response.status} ${await response.text()}`)
22
    }
23
    const page = (await response.json()) as {
24
      bases: { id: string; name: string }[]
25
      offset?: string
26
    }
27
    options.push(...page.bases.map((b) => ({ value: b.id, label: b.name })))
28
    offset = page.offset
29
  } while (offset)
30
  return options
31
}
32

33
/**
34
 * List Tables (Get Base Schema)
35
 * Get the schema of a base: every table with its fields (IDs, names, types, options) and views. Requires the schema.bases:read scope.
36
 */
37
export async function main(auth: RT.Airtable, base_id: DynSelect_base_id) {
38
  const response = await fetch(
39
    `https://api.airtable.com/v0/meta/bases/${base_id}/tables`,
40
    {
41
      headers: {
42
        Authorization: `Bearer ${auth.apiKey}`,
43
        Accept: "application/json",
44
      },
45
    }
46
  )
47
  if (!response.ok) {
48
    throw new Error(`${response.status} ${await response.text()}`)
49
  }
50

51
  return await response.json()
52
}
53