---
title: Conversation context
slug: conversation-context
description: Learn how the Kindly bot's powerful memory, known as "context," stores and retrieves information in conversations. Discover how this key-value store allows for seamless reading and writing, including the ability to overwrite values. Effectively manage the
icon: 🔹
docTags: 
createdAt: 2022-05-13T15:39:50.000Z
---

Every conversation in Kindly has its own key-value store named *context* that serves as the bot's persisted state for that specific conversation.

## Saving values

The bot can collect and persist information on the conversation in many ways. Some are:

- When a [Pattern dialogue](https://help.kindly.ai/patterns) is triggered, what the RegExp matches gets saved into the *context*.
- If the bot sends a [webhook](docId\:Ot0NvUq_I76Ze_M5vRvUC) to your server, your response to it can set new context as well.
- By using the [Widget API](docId\:VEgid5RT7yJ6QjX71W2ly) or [Application API](docId\:CgAd_bxmm1-WVggzsMd1k), you can mutate the context of the conversation.
- By triggering a dialogue that has some context fields attached to it.

Keep in mind that the context memory is a simple key-value storage. If you insert a key-value pair where the key already exists in the memory, the value will be overwritten and the old value will be lost.

## Retrieving values

When defining bot replies, you may insert placeholders that look like `{key}`. If `key` is found in the chat context, the value will be inserted. If `key` is not available, the bot will not use that reply, but try to find something else to reply with, or go to a fallback reply.

The full context is also sent with every [webhook POST](docId\:Ot0NvUq_I76Ze_M5vRvUC), allowing you to perform any operation you'd like on the context data in your own service.

## Examples

### Managing context via Application API

**Writing to context**

When you send data, you can send `new_context` which will be merged with any existing context for the chat (upsert).

```shell
curl \
  -H 'Content-Type: application/json' \
  -H "Authorization: YOUR_API_KEY" \
  -d '{"user_id": "UNIQUE_USER_ID", "message": "Hello world!", "new_context": {"key": "value"}}' \
  https://bot.kindly.ai/api/v1/send
```

**Reading context**

When you receive a message from the bot, you will also find the `context` is attached as part of the JSON payload.

```json
{
  "message": "Hello world!",
  "context": {
    "key": "value"
  }
}
```

### Managing context via Widget API

In some cases you might want to add data to the chat context on Kindly Chat initialization, for instance to know on which page the chat client is loaded or the email of a logged in user.

You can set chat context when initializing the chat client by populating `window.kindlyOptions.context` before loading the Kindly Chat HTML snippet:

```html
<script>
  window.kindlyOptions = {
    context: {
      email: "user@example.com",
      url_path: "/interesting-product-slug"
    }
  };
</script>
<script
  id="kindly-chat"
  src="https://chat.kindlycdn.com/kindly-chat.js"
  data-bot-key="YOUR-BOT-KEY"
  async
></script>
```



