---
source_url: https://www.pubnub.com/docs/serverless-sdk/modules-and-libraries/kvstore
title: kvstore
updated_at: 2026-09-30T07:20:08.000Z
---

# kvstore

## Documentation index

To discover more PubNub resources:

1. Fetch [PubNub's llms.txt](https://www.pubnub.com/llms-full.txt) for a list of available pages in Markdown format.
2. Identify relevant URLs from that index.
3. Fetch the target pages.

Do not assume a path exists, always check the index first.

The `kvstore` built-in module is available only inside a PubNub Function. It stores persistent values by subscribe key, so Functions on the same keyset share the same store.

```javascript
const db = require('kvstore');
```

## Storage constraints

* A KV store key can be up to 1,000 characters.
* A KV store value can be up to 32,000 characters.
* A KV store entry lives for 1 day by default, and a per-entry TTL can range from 1 minute up to 1 year. A TTL below the minimum becomes the minimum. A TTL above the maximum becomes the maximum.

## set

`set(key, value, ttl?)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| key | String | Yes |  | Storage key. |
| value | Object | Yes |  | JSON-serializable value to store. |
| ttl | Number | Optional |  | Time to live, in minutes. Defaults and clamps as described above. |

Returns: Promise.

```javascript
const db = require('kvstore');
db.set('key', { value: true }, 2880);
```

The third argument sets the TTL in minutes (`2880` minutes = 2 days).

## get

`get(key)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| key | String | Yes |  | Storage key to retrieve. |

Returns: Promise resolving to the stored value.

```javascript
const db = require('kvstore');
db.get('key').then((value) => {
    console.log('value', value);
});
```

## setItem

`setItem(key, stringValue, ttl?)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| key | String | Yes |  | Storage key. |
| stringValue | String | Yes |  | String value to store. |
| ttl | Number | Optional |  | Time to live, in minutes. Same defaulting and clamping as `set()`. |

Same as `set()`, optimized for string values. Returns: Promise.

```javascript
const db = require('kvstore');
db.setItem('key', 'value', 2880);
```

## getItem

`getItem(key)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| key | String | Yes |  | Storage key to retrieve. |

Same as `get()`, optimized for string values. Returns: Promise resolving to the stored string.

```javascript
const db = require('kvstore');
db.getItem('key').then((value) => {
    console.log('value', value);
});
```

## removeItem

`removeItem(key)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| key | String | Yes |  | Storage key to remove. |

Returns: Promise.

```javascript
const db = require('kvstore');
db.removeItem('key');
```

## getKeys

`getKeys(pagination_key?)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| pagination_key | String | Optional |  | Last key returned from a previous call. Omit for the first page. |

Returns: Promise resolving to an array of keys, up to the maximum keys per call listed in [API limits](https://www.pubnub.com/docs/serverless-sdk/api-limits.md). Do not rely on a static index (such as `result[99]`) into the returned array, since the page size can change.

```javascript
const db = require('kvstore');
db.getKeys().then((keys) => {
    for (let i = 0; i < keys.length; i++) {
        console.log(keys[i]);
    }
});
```

## Counters

Counters increment a stored number atomically, so concurrent Function executions do not overwrite each other's updates. Counters are not subject to TTL.

### getCounter

`getCounter(key)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| key | String | Yes |  | Counter key to retrieve. |

Returns: Promise resolving to the counter value, or `0` if no counter under `key` has yet been incremented.

```javascript
const db = require('kvstore');
db.getCounter('key').then((counter) => {
    console.log('counter', counter);
});
```

### incrCounter

`incrCounter(key, number?)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| key | String | Yes |  | Counter key to increment. Created and initialized to zero if it doesn't exist. |
| number | Number | Optional |  | Amount to increment by. Defaults to `1`. |

Returns: Promise.

```javascript
const db = require('kvstore');
db.incrCounter('key', 5);
```

### getCounterKeys

`getCounterKeys(pagination_key?)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| pagination_key | String | Optional |  | Last key returned from a previous call. Omit for the first page. |

Returns: Promise resolving to an array of counter keys, up to the maximum keys per call listed in [API limits](https://www.pubnub.com/docs/serverless-sdk/api-limits.md).

```javascript
const db = require('kvstore');
db.getCounterKeys().then((counterKeys) => {
    for (let i = 0; i < counterKeys.length; i++) {
        console.log(counterKeys[i]);
    }
});
```

## Related reference

* [Modules and libraries](https://www.pubnub.com/docs/serverless-sdk/modules-and-libraries/overview.md). Every built-in module available inside a Function.
* [API limits](https://www.pubnub.com/docs/serverless-sdk/api-limits.md). Per-execution operation caps and KV store size limits.

Last updated at: 2026-09-30T07:20:08.000Z
