# Key-value store

## CloudService.GameServices.KvStoreService

{% hint style="warning" %}
The player needs need to be [logged in](/1.1/coherence-cloud/using-coherence-cloud-in-unity/game-services/authentication-service-player-accounts.md) to use the key-value store.
{% endhint %}

This class provides the methods to set, get and unset key-value pairs. This is executed within the context of the currently logged in player.

## Example

```csharp
using Coherence.Cloud;
using Coherence.Toolkit;
using System.Collections;
using UnityEngine;
using Result = Coherence.Runtime.Result;

public class KeyValueStoreExample : MonoBehaviour
{
    public CoherenceBridge bridge;

    public string username;
    public string password;
    public string myKey;
    public string myValue;

    private CloudService cloudService;

    void Start()
    {
        cloudService = bridge.CloudService;

        StartCoroutine(LoginAsAUserAndUseKvStore());
    }

    private IEnumerator LoginAsAUserAndUseKvStore()
    {
        IAuthClient authService = cloudService.GameServices.AuthService;

        while (!cloudService.IsConnectedToCloud && authService.SessionTokenRefreshResult == null)
        {
            yield return null;
        }

        // If refresh failed, we have to login as a user with the given credentials again
        if (authService.SessionTokenRefreshResult == Result.InvalidCredentials)
        {
            var task = authService.LoginWithPassword(username, password, true);

            while (!task.IsCompleted)
            {
                yield return null;
            }
        }

        // At this point, we are ready to fully utilize the Cloud Service
        KvStoreClient keyValueStore = cloudService.GameServices.KvStoreService;

        // Sets a value
        // key: lowercase letters, numbers, underscore, dash
        // val: any string (null is not allowed)
        keyValueStore.Set(myKey, myValue);

        // Gets a value
        // key: lowercase letters, numbers, underscore, dash
        var foo = keyValueStore.Get(myKey);
        Debug.Log(string.Format("foo={0}", foo)); // foo=1

        // Unsets a value, removing it from the store
        // key: lowercase letters, numbers, underscore, dash
        keyValueStore.Unset(myKey);
    }
}
```

## Limitations

**Size**: there are no limits to the number of stored key/values as long as the total size is less than 256 kB.

**Requests**: Set/Get/Unset can be called unlimited amount of times but the execution may be throttled.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coherence.io/1.1/coherence-cloud/using-coherence-cloud-in-unity/game-services/key-value-store.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
