> ## 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.

# CognigyScript

*CognigyScript* is a superset of JavaScript that provides access to the [Input](/ai/agents/develop/ai-agent-memory/input), [Context](/ai/agents/develop/ai-agent-memory/context), and [Profile](/ai/agents/develop/ai-agent-memory/profile) objects. With CognigyScript expressions, you can retrieve values from JSON properties, and use these values in AI Agent conversations.

In most cases, you wrap CognigyScript expressions in double curly braces and quotation marks `{{ }}`, for example, `{{input.text}}`. This syntax enables Cognigy.AI to identify and execute the CognigyScript code embedded in the Flow. However, CognigyScript evaluates conditions as standard JavaScript. You don't need to wrap the CognigyScript expression in `{{ }}`. For example, `context.orders === 3` evaluates to true if the `orders` variable in the Context is equal to `3`.

You can use CognigyScript to:

* Add dynamic and executable content to chat and voice conversations.
* Retrieve information from [Cognigy.AI objects](/ai/agents/develop/ai-agent-memory/overview#lifespan-of-cognigy-ai-objects) and reuse it in chat and voice conversations.
* Run scripts to manipulate data dynamically.
* Use CognigyScript in JSON arguments to make requests to an external system via the HTTP Request Node.

## Working with CognigyScript

You can use CognigyScript in:

* Text fields
* JSON editors
* Code Nodes

The type of field in which you use CognigyScript determines the CognigyScript syntax.

You can also store CognigyScript in [Tokens](/ai/platform-features/tokens).

### Text Fields

For text fields, wrap the CognigyScript expression in `{{ }}`.

<Note>
  If the CognigyScript expression is invalid, CognigyScript returns an empty string.
</Note>

<Accordion title="Example">
  `{{input.text.toUpperCase()}}` returns the text the client sent in uppercase format.
</Accordion>

### JSON Editors

The CognigyScript syntax in JSON editors depends on where you place the CognigyScript expression.

#### Inline CognigyScript

For inline CognigyScript in a JSON object, use `{{ }}`.

<Accordion title="Example">
  ```json theme={null}
  {
      "customer_orders": "{{context.orders}}"
  }
  ```

  The code attempts to fetch the `orders` variable from the Context object and to assign it to `customer_orders`. If `context.orders` doesn't exist, the code skips `customer_orders`.
</Accordion>

#### Inside JSON Arguments

To run CognigyScript within a JSON argument, use `{ "$cs": { "script": "x", "type": "t"}}`, where `x` is the script and `t` is the return type (optional). For instance, you can convert a string such as `"6"` into a number or an object into a string.

<Accordion title="Example">
  ```json theme={null}
  {
      "customer_orders": {
          "$cs": {
              "script": "context.orders",
              "type": "object"
          }
      }
  }
  ```

  The code attempts to fetch the `orders` variable from the Context object and to assign it to `customer_orders` as an object. If `context.orders` doesn't exist, the code skips `customer_orders`.
</Accordion>

### Code Nodes

In a Code Node, you don't need to wrap the CognigyScript expression in `{{ }}`. You can use CognigyScript as standard JavaScript. The `input`, `context`, `profile`, and `actions` variables are exposed by default, as well as `_` and `moment`.

<Accordion title="Example">
  ```js theme={null}
  const ordercount = context.orders;
  switch (ordercount) {
      case 0:
          input.ordertext = "You ordered no items";
          api.output('Hello', {'action': 1}); // outputs the text 'hello' with data {'action': 1}
          break;
      case 1:
          input.ordertext = "You ordered one item";
          break;
      default:
          input.ordertext = "You ordered many items";
  }
  ```
</Accordion>

## Additional Examples

<Accordion title=" Examples of CognigyScript Expressions">
  The following table shows additional examples of CognigyScript expressions:

  | Example                                                             | Description                                                                                                               |
  | ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
  | `{{input.slots.SLOT_NAME[0].keyphrase}}`                            | Extracts the first Keyphrase found for the Slot with the name `SLOT_NAME`.                                                |
  | `{{JSON.stringify(input.data)}}`                                    | Displays a JSON object as text. You can use this CognigyScript expression for debugging purposes.                         |
  | `{{input.slots.DATE[0].start.plain}}`                               | Returns the date information if the user sent a date. Example: July 23, 2021                                              |
  | `{{moment(input.slots.DATE[0].start.ISODate).format('MMM Do YY')}}` | Formats a given date that the user provided in the last message.                                                          |
  | `{{input.currentTime.hour}}`                                        | Gets the hour of the current incoming message. You can use this CognigyScript expression to greet the user appropriately. |
</Accordion>

## More Information

* [Input](/ai/agents/develop/ai-agent-memory/input)
* [Context](/ai/agents/develop/ai-agent-memory/context)
* [Profile](/ai/agents/develop/ai-agent-memory/profile)
