---
metadata:
  - name: generator
    content: Diplodoc Platform v5.70.0
alternate:
  - https://divkit.tech/docs/en/concepts/stored-values.md
  - https://divkit.tech/docs/ru/concepts/stored-values.md
  - href: https://divkit.tech/docs/en/concepts/stored-values.md
    type: text/markdown
    title: Markdown version
  - href: https://divkit.tech/docs/en/llms.txt?revision=r22102947
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://divkit.tech/docs/en/llms.txt?revision=r22102947

# Stored values

Stored values allow you to save data between user sessions. Unlike regular variables, stored values are saved on the device and remain available after the app is restarted.

{% note info %}

On Web, stored values work only if external storage is passed to the `render` function in the `store` parameter, see [Parameters of the render function](https://divkit.tech/docs/en/quickstart/web.md#render-params). Without it, the `set_stored_value` action fails with an error.

{% endnote %}

## Stored value scopes {#scopes}

Stored values support different scopes:

- `global` — values are available in all app cards
- `card` — values are available only in the current card

If the scope is not specified, `global` is used.

## set_stored_value action {#set-stored-value}

To save values, use the typed [set_stored_value](https://divkit.tech/docs/en/concepts/divs/2/div-action-set-stored-value.md) action:

```json translate=no
{
  "log_id": "save_theme",
  "typed": {
    "type": "set_stored_value",
    "name": "user_theme",
    "value": {
      "type": "string",
      "value": "dark"
    },
    "lifetime": 86400,
    "scope": "global"
  }
}
```

Action parameters:
- `name` — name of the stored value. Required.
- `value` — typed value: an object with the `type` (`string`, `integer`, `number`, `boolean`, `color`, `url`, `array`, `dict`) and `value` fields. Required.
- `lifetime` — value lifetime in seconds. Required.
- `scope` — scope: `global` or `card`.

The same action in the `url` format:

```translate=no
div-action://set_stored_value?name=<name>&value=<value>&type=<type>&lifetime=<lifetime_in_sec>&scope=<global|card>
```

#|
|| **Parameter** | **Description** ||
|| `name` | Name of the stored value. Required parameter. ||
|| `value` | The value to be saved. Required parameter. ||
|| `type` | Value type: `string`, `integer`, `number`, `boolean`, `color`, `url`, `array`, or `dict`. Required parameter. ||
|| `lifetime` | Lifetime of the value in seconds. After this time, the value is considered expired. Required parameter. ||
|| `scope` | Scope: `global` or `card`. Optional parameter. ||
|#

## Functions for reading stored values {#functions}

To read stored values, use the `getStored<Type>Value` functions:

- `getStoredStringValue(name, fallback)` — reads a string value; `fallback` is returned when the value is not found;
- `getStoredStringValue(name, scope, fallback)` — reads from the specified scope (`'global'` or `'card'`);
- `getStoredArrayValue(name)`, `getStoredArrayValue(name, scope)`, `getStoredDictValue(name)`, `getStoredDictValue(name, scope)` — read an array or a dictionary. These functions have no `fallback`: if the value is not found, the expression evaluation fails.

Similar functions exist for the `integer`, `number`, `boolean`, `color`, and `url` types.

For the full list of functions with signatures, see [Built-in functions](https://divkit.tech/docs/en/concepts/functions.md).

{% cut "Stored values usage example" %}

```json translate=no
{
  "log_id": "stored_values_example",
  "variables": [
    {
      "name": "theme",
      "type": "string",
      "value": "@{getStoredStringValue('user_theme', 'light')}"
    }
  ],
  "states": [
    {
      "state_id": 0,
      "div": {
        "type": "container",
        "items": [
          {
            "type": "text",
            "text": "Current theme: @{theme}",
            "actions": [
              {
                "log_id": "switch_theme",
                "typed": {
                  "type": "set_stored_value",
                  "name": "user_theme",
                  "value": {
                    "type": "string",
                    "value": "@{theme == 'light' ? 'dark' : 'light'}"
                  },
                  "lifetime": 2592000,
                  "scope": "global"
                }
              }
            ]
          }
        ]
      }
    }
  ]
}
```

{% endcut %}

{% cut "Example with different scopes" %}

```json translate=no
{
  "log_id": "stored_values_scopes",
  "variables": [
    {
      "name": "global_setting",
      "type": "string",
      "value": "@{getStoredStringValue('global_setting', 'global', 'default_value')}"
    },
    {
      "name": "card_setting",
      "type": "string",
      "value": "@{getStoredStringValue('card_setting', 'card', 'card_default')}"
    }
  ],
  "states": [
    {
      "state_id": 0,
      "div": {
        "type": "container",
        "items": [
          {
            "type": "text",
            "text": "Global setting: @{global_setting}",
            "actions": [
              {
                "log_id": "set_global",
                "typed": {
                  "type": "set_stored_value",
                  "name": "global_setting",
                  "value": {
                    "type": "string",
                    "value": "new_global_value"
                  },
                  "lifetime": 2592000,
                  "scope": "global"
                }
              }
            ]
          },
          {
            "type": "text",
            "text": "Card setting: @{card_setting}",
            "actions": [
              {
                "log_id": "set_card",
                "typed": {
                  "type": "set_stored_value",
                  "name": "card_setting",
                  "value": {
                    "type": "string",
                    "value": "new_card_value"
                  },
                  "lifetime": 2592000,
                  "scope": "card"
                }
              }
            ]
          }
        ]
      }
    }
  ]
}
```

{% endcut %}

<!-- source: en/_includes/index/troubleshooting.md -->
## Learn more {#troubleshooting}

You can discuss topics of interest in the DivKit user community in Telegram: [https://t.me/divkit_community_en](https://t.me/divkit_community_en).



[DivKit Repository](https://github.com/divkit/divkit)
<!-- endsource: en/_includes/index/troubleshooting.md -->
