Key Value Storage

Add a global, low-latency key-value data storage to your Nuxt application.

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
  }
})
This option will use Cloudflare platform proxy in development and automatically create a Cloudflare Workers KV namespace for your project when you deploy it.
Nuxt DevTools KV
NuxtHub Admin KV

List all keys

Retrieves all keys from the KV storage.

const keys = await hubKV().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.

const vueKeys = await hubKV().keys('vue')
/*
[
  'vue:nuxt',
  'vue:quasar'
]
*/
We recommend to use prefixes for better organization and performance as keys() will scan the entire namespace.

Get an item

Retrieves an item from the Key-Value storage.

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

Set an item

Puts an item in the storage.

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

You can delimit the key with a : to create a namespace:

await hubKV().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

You can also set a TTL (time to live) in seconds:

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

The item will be deleted after the TTL has expired.

Has an item

Checks if an item exists in the storage.

const hasAngular = await hubKV().has('angular')

Delete an item

Delete an item from the storage.

await hubKV().del('react')

Limits

  • The maximum size of a value is 25 MiB.
  • The maximum length of a key is 512 bytes.
  • The TTL must be at least 60 seconds.

Learn more about Cloudflare KV limits.

Learn More

hubKV() is an instance of unstorage with the Cloudflare KV binding driver.