# Constants

## Introduction

This guide explains how to create and use constants in rulesets.

**Constants** are placeholders with a fixed value that you can use throughout a ruleset. They are comparable to `const` elements in JavaScript.

### Constant types

Rulecube supports these constant types:

* `Simple`\
  This type lets you create simple number or string constants for, for example, a fixed discount percentage like `22` or an information message like `You are too young to apply for this insurance`.&#x20;

* `JSON`\
  This type lets you create a JSON object, such as a table with discount ratings:<br>

  ```json
  {
    "percentage_1": 10,
    "percentage_2": 15,
    "percentage_3": 20
  }
  ```

* `Table`\
  This type lets you create a table with rows and columns:<br>

  | Key           | value |
  | ------------- | ----- |
  | percentage\_1 | 10    |
  | percentage\_2 | 15    |
  | percentage\_3 | 20    |

* `External`\
  This type lets you fetch a value from any website or API in the world.

## Procedure

For each type of constant, we give the steps for creating and using it.&#x20;

1. Create a ruleset named `ProductPrices`.

### Simple constant

#### Create

1. Create a constant named `MsgTooMuch` and set the **Constant type** to `Simple`.
2. In the **Value** field, enter `This product is scarce; therefore, you are allowed a maximum of 3 units`.
3. Click **Save**.

#### Use

You use a simple constant by just referring to its name in a Rule or Function.

### JSON constant

#### Create manually

1. Create a constant named `PercentagesJSON` and set the **Constant type** to `JSON`.\
   ↳ Rulecube enables the **Value** field for JSON content.

2. In the **Value** field, enter these key-value pairs:<br>

   ```json
   {
     "p1": 10,
     "p2": 15,
     "p3": 20
   }
   ```

3. Click **Save**.

#### Create by drag and drop

If you have a JSON file, you only need to create the constant and set **Constant type** to `JSON`. And then drop the JSON file onto the **Value** field.

#### Use

Refer to an element from a JSON constant by addressing the constant and key whose value you want:

```json
PrecentagesJSON.p2
```

### Table constant

Below is a short introduction to **Contant tables**. In the [next chapter](https://docs.rulecube.com/how-to-guides/ruleset-development/constants/constant-tables), you will find a more elaborate explanation with more examples. &#x20;

#### Create manually

1. Create a constant named `PercentagesTable` and set the **Constant type** to `Table`.\
   ↳ Rulecube shows the **Constant Table Designer** section.

2. Click **Row** twice to add extra rows to the table.

3. In the **Constant Table Designer** section, enter these keys and corresponding values:<br>

   | Key | value |
   | --- | ----- |
   | p1  | 10    |
   | p2  | 15    |
   | p3  | 20    |

4. Click **Save**.

#### Create by drag and drop

If you have a [CSV file](https://en.wikipedia.org/wiki/Comma-separated_values) that contains your table data, you only need to create the constant and set its **Type** to `Table`. And then drop the CSV file onto the **Value** field.

#### Use

Refer to an element from a table constant by addressing the table and key whose value you want:

```json
PrecentagesTable.p3
```

### External constant

In addition to explaining how to create and use an external constant, here we also present the **External constant data** page that lists all the external data constants that you defined.

#### Create

1. Create a constant named `Product` and set **Constant type** to `External`.\
   ↳ Rulecube shows the **External data settings** section.

2. In the **URL** field, enter the URL of the API you want to query. In this case: `https://dummyjson.com/products/1`

3. In the **Fetch frequency** list, select `Daily`. And in the **Time (GMT)** field, enter `01:00 AM`.\
   ↳ Now, Rulecube re-fetches the value every night at one o'clock.

4. Click **HTTP settings**.\
   ↳ The **HTTP settings** section opens.

5. In the **Method** list, select `GET`.

6. Click **Fetch**.\
   ↳ Rulecube calls the API and shows the JSON response in the **Data** field:<br>

   ```json
   {
     "id": 1,
     "title": "iPhone 9",
     "description": "An apple mobile which is nothing like apple",
     "price": 549,
     "discountPercentage": 12.96,
     "rating": 4.69,
     "stock": 94,
     "brand": "Apple",
     "category": "smartphones",
     "thumbnail": "https://i.dummyjson.com/data/products/1/thumbnail.jpg",
     "images": [
       "https://i.dummyjson.com/data/products/1/1.jpg",
       "https://i.dummyjson.com/data/products/1/2.jpg",
       "https://i.dummyjson.com/data/products/1/3.jpg",
       "https://i.dummyjson.com/data/products/1/4.jpg",
       "https://i.dummyjson.com/data/products/1/thumbnail.jpg"
     ]
   }
   ```

7. Click **Save**.

#### Use

Since `Product` is the constant that contains the JSON response and `price` is one of its elements, you can refer to the `price` element like this:

```json
Product.price
```

Which returns the value `549`.

Likewise, you can refer to other elements:

```
Product.title
Product.stock
```

#### External constant data page

If you create an external constant, Rulecube adds it to the **External constant data** page for your reference. This page shows the external constants of all your rulesets, and you open this page by clicking **External Data** from the top menu. Its content looks something like this:

<figure><img src="https://content.gitbook.com/content/kPoC1P5CqaOhS6k4bIDl/blobs/QLGXlhFYHYhwmu8yuBYH/image.png" alt=""><figcaption></figcaption></figure>

## Encryption

Data defined in a constant could sometimes be considered "sensitive". For example, you may want to store a username and password, API-key or secret URL in a constant, but you don't want anyone that opens the ruleset to be able to see its value.

For those cases, a constant can be Encrypted. The constant can still be used as you normally would, but it is not visible to anyone that opens the ruleset in Rulecube itself or fetches it via the API.

We must first enable this in the Ruleset itself:

1. Go to the Version settings of a Ruleset
2. Check the "Contains sensitive data" checkbox

Now we can encrypt any constant we want:

1. Open an existing Constant or create a new one
2. Click on the "Encrypt" button:\
   &#x20;![](https://content.gitbook.com/content/kPoC1P5CqaOhS6k4bIDl/blobs/t1rumAthVCCwCvHtbxr9/image.png)

NOTE: Once you save the Ruleset, the encrypted constant can no longer be read. You can only revert this by rolling back to a previous revision from History.

## Convert to Environment Variable

Any simple constant can be converted to an Environment Variable with the following button, found in the top-right of a constant:&#x20;

![](https://content.gitbook.com/content/kPoC1P5CqaOhS6k4bIDl/blobs/X4kKH4CQTLCW89fESZCS/image.png)

This simplifies the process of manually creating an Environment Variable. After clicking the button above, the constant will be useable in other Rulesets as an Environment Variable.
