0

Create KV Namespace

by
Published today

Create a Workers KV namespace in the account set on the resource.

Script cloudflare Verified

The script

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

3
/**
4
 * Create KV Namespace
5
 * Create a Workers KV namespace in the account set on the resource.
6
 */
7
export async function main(auth: RT.Cloudflare, title: string) {
8
  const response = await fetch(
9
    `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/storage/kv/namespaces`,
10
    {
11
      method: "POST",
12
      headers: {
13
        Authorization: `Bearer ${auth.token}`,
14
        "Content-Type": "application/json",
15
        Accept: "application/json",
16
      },
17
      body: JSON.stringify({ title }),
18
    }
19
  )
20

21
  if (!response.ok) {
22
    throw new Error(`${response.status} ${await response.text()}`)
23
  }
24

25
  return await response.json()
26
}
27