Save a theme setting with KV and read it back in a server route. KV stores each value under a key. This tutorial uses local files, so you need no provider account or credentials.
Before you start
Section titled "Before you start"Complete Installation, check /api/health, and continue in that app with Node.js 24.15 or newer and pnpm.
Prefer to skip the setup? Run this tutorial in your browser with a live editor and preview.
Enable local KV
Section titled "Enable local KV"Install the KV package:
pnpm add @vite-hub/kvAdd kv: true and keep your other options. On the node host, KV stores values in files under .vitehub/data/kv.
export default defineNuxtConfig({
modules: ['vite-hub/nuxt'],
vitehub: {
kv: true,
},
})import { defineConfig } from "vite"
import { nitro } from 'nitro/vite'
import { vitehub } from "vite-hub"
export default defineConfig({
plugins: [
vitehub({
kv: true,
}),
nitro(),
],
})import { defineConfig } from "vite"
import { vitehub } from "vite-hub"
export default defineConfig({
plugins: [
vitehub({
kv: true,
}),
],
})Write and read one value
Section titled "Write and read one value"Create server/api/settings.post.ts. Nitro and Nuxt serve it at POST /api/settings. It saves the request body under the key settings, reads it back, and returns it.
KV calls return [error, value]. Check the error before you use the value. kv.set() returns no value, so the route only checks writeError.
import { defineEventHandler, readBody } from "h3"
import { kv } from "vite-hub/kv"
export default defineEventHandler(async (event) => {
const settings = await readBody<{ theme: string }>(event)
const [writeError] = await kv.set("settings", settings)
if (writeError) throw writeError
const [readError, storedSettings] = await kv.get("settings")
if (readError) throw readError
return { settings: storedSettings }
})Explicit H3 imports make the example work without Nuxt auto-imports.
Check the stored setting
Section titled "Check the stored setting"Restart your app to load the new configuration:
pnpm devIn another terminal, send the setting:
curl -X POST http://localhost:3000/api/settings \
-H 'content-type: application/json' \
-d '{"theme":"system"}'You should receive:
{"settings":{"theme":"system"}}Returning the result of kv.get() proves both the write and read worked. Send "theme":"dark" to replace the stored value.
To check the production server of the Vite app, stop pnpm dev, build and start the server, and repeat the request:
pnpm build
pnpm startIn Nuxt, run pnpm build and node .output/server/index.mjs instead.
Continue with storage
Section titled "Continue with storage"Values survive a restart as long as .vitehub/data/kv exists on disk. Do not commit .vitehub. A deployed server needs persistent storage or a hosted KV provider. Changing the driver does not move existing data.
Read KV configuration to pick a hosted store or add a named store. The KV server API lists method results and errors. For other data, try the Database tutorial or Blob tutorial.