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

# addToContext

## Syntax

`addToContext(key, value, mode)`

## Description

Adds a value to the Context object.

**Parameters:**

* `key`: `string` — the key in the Context object where the value should be stored.
* `value`: `unknown` — the value to store.
* `mode`: `"simple" | "array"`:
  * `"simple"` — stores the value directly at the specified key, replacing any existing value.
  * `"array"` — adds the value to an array at the specified key, creating the array if it doesn't exist.

**Returns:** `void`

## Example

<AccordionGroup>
  <Accordion title="Simple Mode">
    ```js theme={null}
    // Initial context: {}
    addToContext("user.city", "Berlin", "simple");
    // Context after execution: { user: { city: "Berlin" } }
    // If called again with same key:
    addToContext("user.city", "Munich", "simple");
    // Context after execution: { user: { city: "Munich" } }  // Overwrites previous value
    ```
  </Accordion>

  <Accordion title="Array Mode">
    ```js theme={null}
    // Initial context: {}
    addToContext("user.purchases", "book", "array");
    // Context after execution: { user: { purchases: ["book"] } }

    // Adding more items to the same array:
    addToContext("user.purchases", "laptop", "array");
    addToContext("user.purchases", "coffee", "array");
    // Context after execution: { user: { purchases: ["book", "laptop", "coffee"] } }
    ```
  </Accordion>
</AccordionGroup>
