# Use Upstash Redis for ViteHub KV

Use Upstash for a hosted KV store instead of local files. ViteHub talks to
Upstash's REST API, and your code calls `kv` from `vite-hub/kv`.

First, complete [Store your first KV value](https://vitehub.dev/docs/kv/get-started) and keep its
settings routes. Create an Upstash Redis database and copy its REST URL and
token. Reads and writes can cost money.

## Select the REST driver

Install the Upstash client:

```bash [Terminal]
pnpm add @upstash/redis
```

Replace the tutorial's ViteHub options with this. Keep your other plugins and
features.

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: { host: 'node', kv: { driver: 'upstash' } },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
	plugins: [vitehub({ host: 'node', kv: { driver: 'upstash' } }), nitro()],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
	plugins: [vitehub({ host: 'node', kv: { driver: 'upstash' } })],
})
```

Set both variables where you build and run the app:

```bash [Terminal]
export UPSTASH_REDIS_REST_URL='https://your-database.upstash.io'
export UPSTASH_REDIS_REST_TOKEN='your-rest-token'
pnpm dev
```

ViteHub also reads `KV_REST_API_URL` and `KV_REST_API_TOKEN`, and that pair
wins. If the app connects to the wrong database, remove old values. In
production, use the host's secret store. Keep the token out of browser code.

For Vercel, use `host: 'vercel'` and set the same credentials in the project.
That host uses Upstash for KV by default.

## Verify remote persistence

```bash [Terminal]
curl --fail-with-body -X PUT http://localhost:3000/api/settings \
  -H 'content-type: application/json' -d '{"theme":"dark"}'
curl --fail-with-body http://localhost:3000/api/settings
```

Both responses contain `{ "settings": { "theme": "dark" } }`. Restart the app,
repeat the GET, and find the key in Upstash. The value must survive without a
local `.vitehub/data/kv` directory.

## Keep Redis-specific work separate

ViteHub KV is a storage API, not every Redis command. For Redis data
structures or commands, use the Upstash SDK. Do not build a distributed lock
from separate `kv.get()` and `kv.set()` calls.

See [KV configuration](https://vitehub.dev/docs/kv/configure) for named stores,
[KV limits](https://vitehub.dev/docs/kv/limits-and-errors) for supported operations, and the
[Upstash Redis docs](https://upstash.com/docs/redis/overall/getstarted) for
regions, credentials, and limits.

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

- [Overview](https://vitehub.dev/raw/docs/guides.md)
- [Agents](https://vitehub.dev/raw/docs/guides/deploy-agent-cloudflare-vercel.md)
- [Chat bots](https://vitehub.dev/raw/docs/guides/slack-ai-bot.md)
- Storage
  - [Upstash Redis](https://vitehub.dev/raw/docs/guides/upstash-redis.md) (this page)
  - [Cloudflare R2](https://vitehub.dev/raw/docs/guides/cloudflare-r2.md)
  - [Vercel Blob](https://vitehub.dev/raw/docs/guides/vercel-blob.md)
- [Databases](https://vitehub.dev/raw/docs/guides/turso.md)
- [Server work](https://vitehub.dev/raw/docs/guides/resend.md)
- [Better Auth on Cloudflare](https://vitehub.dev/raw/docs/guides/better-auth-cloudflare.md)
- [Concepts](https://vitehub.dev/raw/docs/guides/rate-limiting-algorithms.md)

Next: [Turso](https://vitehub.dev/raw/docs/guides/turso.md)

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