# KV hosts

## Provider output

KV picks the store and generates store-name types. Put namespaces, bindings, and credentials in your config or deployment. Your code imports `kv` from `vite-hub/kv` on every driver (local, Cloudflare, Deno, Vercel, or others).

`vite dev` keeps every store local unless it sets `remote: true`, also when `.env` has Upstash credentials. Read [Development stays local](https://vitehub.dev/docs/kv/configure#development-stays-local).

## Read and write keys during development

`hubKV()` adds `vitehub kv` commands. Start the Vite dev server, then use them from another terminal.

```bash [Terminal]
pnpm vitehub kv list --prefix users:
pnpm vitehub kv get settings --json
pnpm vitehub kv set settings '{"theme":"dark"}' --json-value
pnpm vitehub kv delete settings
```

- They run the app's `kv.*` [operations](https://vitehub.dev/docs/development/operations) in the Nitro runtime, on the same storage. They target the dev server or, with `--url`, a deployed app with the [Console](https://vitehub.dev/docs/development/console) on. `--store <name>` picks a named store.
- Each write prints what it changed. There is no `clear` command.
- `kv set` and `kv delete` refuse `vitehub:` keys (`KV_RESERVED_KEY`), where ViteHub keeps its state. Reads work.

Read [CLI](https://vitehub.dev/docs/development/cli#read-and-write-kv-keys) for every command and option.

- `bigint` values, also nested, show as decimal strings. Values that cannot be serialized return `KV_VALUE_UNSUPPORTED`.
- Cloudflare writes report the TTL after rounding.
- Drivers cannot tell a key's time left, so `kv get` shows `expiry: "unknown"`.
- A write without `--ttl` removes the old expiry. The Console editor warns and has an **Expires in seconds** field.

## Preview deployments

> **Warning**

A Cloudflare Version URL reads and writes production keys ([Compare workflows](https://developers.cloudflare.com/workers/previews/compare-workflows/)).

A Cloudflare Preview uses only the bindings in the Wrangler `previews` block, and Previews that bind the same namespace `id` share data ([Resources and isolation](https://developers.cloudflare.com/workers/previews/resources/)). ViteHub does not write that block. Declare a namespace in `previews.kv_namespaces` yourself.

To give a preview Worker its own namespace:

- Set `KV_NAMESPACE_ID` in its build and leave `namespaceId` unset.
- Set `namespaceId` to the preview id. It wins over `KV_NAMESPACE_ID`.
- Set a separate `namespaceName`, leave `namespaceId` and `KV_NAMESPACE_ID` unset, and provision it before the build.

Read [Preview deployments share data](https://vitehub.dev/docs/development/provisioning#preview-deployments-share-data).

## Production checks

Key prefixes are not tables. Use Database for constraints, joins, migrations, history, or complex queries.

Do not build locks with `kv.get()` and `kv.set()`. Use [`kv.lock()`](https://vitehub.dev/docs/kv/server-api#locks). The atomic methods cover single-use reads, single-use writes with `setIfAbsent()`, owner-only deletes with `deleteIfEquals()`, and counters; they are not a general compare-and-swap API.

## Related guides

Read [Upstash Redis for ViteHub KV](https://vitehub.dev/docs/guides/upstash-redis) for hosted setup and a persistence check.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/kv.md)
- [Get started](https://vitehub.dev/raw/docs/kv/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/kv/configure.md)
- [Server API](https://vitehub.dev/raw/docs/kv/server-api.md)
- [Agent capability](https://vitehub.dev/raw/docs/kv/agent-capability.md)
- [Hosts](https://vitehub.dev/raw/docs/kv/hosts.md) (this page)
- [Limits and errors](https://vitehub.dev/raw/docs/kv/limits-and-errors.md)

Next: [KV_KEY_TOO_LONG](https://vitehub.dev/raw/docs/kv/diagnostics/kv_key_too_long.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
