0

List D1 Databases

by
Published today

List the D1 databases in the account, optionally filtered by name.

Script cloudflare Verified

The script

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

3
/**
4
 * List D1 Databases
5
 * List the D1 databases in the account, optionally filtered by name.
6
 */
7
export async function main(
8
  auth: RT.Cloudflare,
9
  name: string | undefined,
10
  page: number | undefined,
11
  per_page: number | undefined
12
) {
13
  const url = new URL(
14
    `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/d1/database`
15
  )
16
  for (const [k, v] of Object.entries({ name, page, per_page })) {
17
    if (v != null && v !== "") url.searchParams.append(k, String(v))
18
  }
19

20
  const response = await fetch(url, {
21
    headers: {
22
      Authorization: `Bearer ${auth.token}`,
23
      Accept: "application/json",
24
    },
25
  })
26

27
  if (!response.ok) {
28
    throw new Error(`${response.status} ${await response.text()}`)
29
  }
30

31
  return await response.json()
32
}
33