Key Value Storage

Use a key-value data storage in Nuxt.

NuxtHub Key Value Storage automatically configures Nitro Storage, which is built on unstorage.

Getting Started

Enable the key-value storage in your NuxtHub project by adding the kv property to the hub object in your nuxt.config.ts file.

nuxt.config.ts
export default defineNuxtConfig({
  hub: {
    kv: true
  }
})

Automatic Configuration

When building the Nuxt app, NuxtHub automatically configures the key-value storage driver on many providers.

  1. Install the @upstash/redis package
pnpm add @upstash/redis
  1. Set the UPSTASH_REDIS_REST_URL environment variable to your Upstash Redis REST URL.
.env
UPSTASH_REDIS_REST_URL=https://...
UPSTASH_REDIS_REST_TOKEN=...
When deploying to Vercel, we automatically detect if KV_REST_API_URL and KV_REST_API_TOKEN environment variables are set, and use them to configure the Upstash Redis connection.
If no automatic configuration is found, it will default to filesystem and store the data in the .data/kv.

Manual Configuration

You can use any unstorage driver by configuring the hub.kv option with a driver and its options.

You can find the driver list on unstorage documentation with their configuration.
nuxt.config.ts
export default defineNuxtConfig({
  hub: {
    kv: {
      driver: 'redis',
      url: 'redis://localhost:6379',
      /* any additional driver options */
    }
  }
})
You can find the driver list on unstorage documentation with their configuration.

Usage

The hub:kv module provides access to the Key-Value storage through an unstorage instance.

import { kv } from 'hub:kv'
kv is auto-imported on server-side, you can directly use it without importing it from hub:kv.

Set an item

Puts an item in the storage.

import { kv } from 'hub:kv'

await kv.set('vue', { year: 2014 })

// using prefixes to organize your KV namespace, useful for the `keys` operation
await kv.set('vue:nuxt', { year: 2016 })
The maximum size of a value is 25 MiB and the maximum length of a key is 512 bytes.

Expiration

By default, items in your KV namespace will never expire. You can delete them manually using the del() method or set a TTL (time to live) in seconds.

The item will be deleted after the TTL has expired. The ttl option maps to Cloudflare's expirationTtl option. Values that have recently been read will continue to return the cached value for up to 60 seconds and may not be immediately deleted for all regions.

import { kv } from 'hub:kv'

await kv.set('vue:nuxt', { year: 2016 }, { ttl: 60 })

Get an item

Retrieves an item from the Key-Value storage.

import { kv } from 'hub:kv'

const vue = await kv.get('vue')
/*
{
  year: 2014
}
*/

Has an item

Checks if an item exists in the storage.

import { kv } from 'hub:kv'

const hasAngular = await kv.has('angular') // false
const hasVue = await kv.has('vue') // true

Delete an item

Delete an item with the given key from the storage.

import { kv } from 'hub:kv'

await kv.del('react')

Clear the KV namespace

Deletes all items from the KV namespace..

import { kv } from 'hub:kv'

await kv.clear()

To delete all items for a specific prefix, you can pass the prefix as an argument. We recommend using prefixes for better organization in your KV namespace.

import { kv } from 'hub:kv'

await kv.clear('react')

List all keys

Retrieves all keys from the KV storage.

import { kv } from 'hub:kv'

const keys = await kv.keys()
/*
[
  'react',
  'react:gatsby',
  'react:next',
  'vue',
  'vue:nuxt',
  'vue:quasar'
]

To get the keys starting with a specific prefix, you can pass the prefix as an argument. We recommend using prefixes for better organization in your KV namespace.

import { kv } from 'hub:kv'

const vueKeys = await kv.keys('vue')
/*
[
  'vue:nuxt',
  'vue:quasar'
]
*/