> ## Documentation Index
> Fetch the complete documentation index at: https://docs.attio.com/llms.txt
> Use this file to discover all available pages before exploring further.

# KV Store

> A key-value store for your app

KV Store is a lightweight, server-side key-value database for your app.
Use it to cache data, ensure webhook idempotency, or store transient server state.
It’s available in server functions, webhook handlers, and event handlers.

## Example: Caching a response from an external API

When your app calls an external API, you don’t always need to fetch fresh data every time.
With the KV Store, you can cache the response and set a TTL so it clears itself after a certain period of time.

```ts get-users.server.ts theme={"system"}
import {kv} from "attio/server"

export default async function getAllUsers() {
  const users = await kv.get("users")

  if (users !== null) {
    return users.value
  }

  const response = await fetch("https://api.emailsequence.com/v1/users")
  const data = await response.json()
  await kv.set("users", data, {ttlInSeconds: 60 * 60 * 24 * 30})
  return data
}
```

## API

### `get(key: string): Promise<{ value: string } | null>`

```ts theme={"system"}
const value = await kv.get("key")
```

Get the value of a key. Returns `null` if the key does not exist.

#### Parameters

<ParamField path="key" type="string" required>
  The key to get the value of.
</ParamField>

### `set(key: string, value: string, options?: { ttlInSeconds?: number }): Promise<void>`

```ts theme={"system"}
await kv.set("key", "value", {ttlInSeconds: 60})
```

Set the value of a key. Optionally, you can set a TTL in seconds after which the key will be deleted.

#### Parameters

<ParamField path="key" type="string" required>
  The key to set the value of.
</ParamField>

<ParamField path="value" type="string" required>
  The value to set for the key.
</ParamField>

<ParamField path="options" type="{ ttlInSeconds?: number }">
  The options for the key. Currently, the only option is `ttlInSeconds` which is the number of
  seconds after which the key will be deleted.
</ParamField>

### `delete(key: string): Promise<void>`

```ts theme={"system"}
await kv.delete("key")
```

Delete a key.

#### Parameters

<ParamField path="key" type="string" required>
  The key to delete.
</ParamField>
