Cloudflare KV (http)
Store data in Cloudflare KV using the Cloudflare API v4.
You need to create a KV namespace. See KV Bindings for more information.
Note: This driver uses native fetch and works universally! For using directly in a cloudflare worker environment, please use cloudflare-kv-binding
driver for best performance!
import { createStorage } from "unstorage";
import cloudflareKVHTTPDriver from "unstorage/drivers/cloudflare-kv-http";
// Using `apiToken`
const storage = createStorage({
driver: cloudflareKVHTTPDriver({
accountId: "my-account-id",
namespaceId: "my-kv-namespace-id",
apiToken: "supersecret-api-token",
}),
});
// Using `email` and `apiKey`
const storage = createStorage({
driver: cloudflareKVHTTPDriver({
accountId: "my-account-id",
namespaceId: "my-kv-namespace-id",
email: "me@example.com",
apiKey: "my-api-key",
}),
});
// Using `userServiceKey`
const storage = createStorage({
driver: cloudflareKVHTTPDriver({
accountId: "my-account-id",
namespaceId: "my-kv-namespace-id",
userServiceKey: "v1.0-my-service-key",
}),
});
Options:
accountId
: Cloudflare account ID.namespaceId
: The ID of the KV namespace to target. Note: be sure to use the namespace's ID, and not the name or binding used in a worker environment.apiToken
: API Token generated from the User Profile 'API Tokens' page.email
: Email address associated with your account. May be used along withapiKey
to authenticate in place ofapiToken
.apiKey
: API key generated on the "My Account" page of the Cloudflare console. May be used along withemail
to authenticate in place ofapiToken
.userServiceKey
: A special Cloudflare API key good for a restricted set of endpoints. Always begins with "v1.0-", may vary in length. May be used to authenticate in place ofapiToken
orapiKey
andemail
.apiURL
: Custom API URL. Default ishttps://api.cloudflare.com
.base
: Adds prefix to all stored keys
Supported methods:
getItem
: Maps to Read key-value pairGET accounts/:account_identifier/storage/kv/namespaces/:namespace_identifier/values/:key_name
hasItem
: Maps to Read key-value pairGET accounts/:account_identifier/storage/kv/namespaces/:namespace_identifier/values/:key_name
. Returnstrue
if<parsed response body>.success
istrue
.setItem
: Maps to Write key-value pairPUT accounts/:account_identifier/storage/kv/namespaces/:namespace_identifier/values/:key_name
removeItem
: Maps to Delete key-value pairDELETE accounts/:account_identifier/storage/kv/namespaces/:namespace_identifier/values/:key_name
getKeys
: Maps to List a Namespace's KeysGET accounts/:account_identifier/storage/kv/namespaces/:namespace_identifier/keys
clear
: Maps to Delete key-value pairDELETE accounts/:account_identifier/storage/kv/namespaces/:namespace_identifier/bulk