Variables let you store information from somewhere in Botpress Studio and reuse it somewhere else.

Think of a variable like an empty box. You can fill it with any kind of information you want — like a name, a number, or a preference — and put it away for safekeeping. Then, when your bot needs that information again, it can open the box and access what’s inside.

Variable scopes

You can use different types of variables to scope the information you store. This gives you control over where your bot can access a piece of information. For example, you might want some information to:

Select any variable scope below to learn more about it:

Create a variable

Configuration variables have a different setup process. Check out the configuration variables page for more information.

Before you can store information in a variable, you need to create it:

  1. Open any Workflow.
  2. Select + in the Variables section at the bottom of the left navigation bar.
  3. Choose a Scope, Type and Name for your variable:

By default, variables are empty when created. However, you can create them with a default value if you prefer — just specify a Default Value in the Additional Settings menu.

Assign a value to your variable

A freshly created variable is like an empty box — you need put something inside it before you can use it. This is called assigning a value to your variable.

Studio will usually indicate where you have the option to assign a value to a variable.. For example, in a Capture Information Card:

If you want more control over when and how you assign a value to a variable, you can use code instead.

Read your variable

When you need to access the information stored in a variable, you can read it. This is like opening the box and looking at what’s inside.

Each variable scope is read differently. Check out the section for your variable’s scope for more information.

Data types for variables

Botpress variables are typed, meaning they are limited in what data they can hold. These data types are important to:

  • Ensure the code behind Botpress runs smoothly
  • Help AI tasks generate better results

Here are the different data types available for Botpress variables:

Type

Description

String

Values that are treated as text. Strings can contain any amount of letters, numbers, or special characters.

Example uses: Storing a user’s name, storing an AI task’s generated message.

Boolean

Variables can be true or false, with a lowercase ‘t’ or ‘f’.

Example uses: Storing if a user is a returning customer, storing whether the bot has greeted the user or not

Number

Variables that are numeric, either with or without decimal places.

Example uses: Storing a user’s phone number, storing an address’s area code

Date

Variables that are a single date or date and time. Botpress uses ISO 8601 date/time format, such as 20220921T10:34:14 for 21 September 2023 at 10:34 AM (with 14 seconds).

Example uses: Storing when the user starts a chat, storing a user’s appointment date

Object

Variable that is a collection of key-value pairs. Useful when dealing with code and written like {key: value}.

Example uses: Storing a user’s profile, storing the results of an API call

Array

Variable that is a collection of other, similar variables. Arrays can contain strings, like [“a”, “b”, “c”], or Objects like [{key:value},{key:value}].

Example uses: Storing a user’s past messages, storing options for the user to choose from

Enum

Variable that can be one of a set number of choices.

Example uses: Storing days of the week, storing items available from a food menu

Pattern

Variable that uses Regular Expressions/Regex to store a special pattern. This pattern can match certain words or numbers.

Example uses: Storing account numbers, storing flight numbers