> ## Documentation Index
> Fetch the complete documentation index at: https://bruno-a6972042-mintlify-c74cb75a.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Variables

### Overview

Variables in the Bruno allow you to store dynamic values that can be reused across multiple API requests, environments, and workflows. This feature enhances flexibility, maintainability, and efficiency by enabling you to manage frequently changing data points such as tokens, environment-specific URLs, or user-defined values in one place.

### Types

There are 7 types of variables you can create:

* [Global Environments Variables](./global-environment-variables)
* [Environment Variables](./environment-variables)
* [Collection Variables](./collection-variables)
* [Folder Variables](./folder-variables)
* [Request Variables](./request-variables)
* [Runtime Variables](./runtime-variables)
* [Prompt Variables](./prompt-variables)

Additionally, Process Environment Variables can be defined in an external environment configuration file:

* [Process Environment Variables](./process-env)

### Variable Precedence and Scope

When a variable is accessed, the following precedence is used to determine which value is used:

<div style={{ display: 'flex', justifyContent: 'center', fontFamily: 'sans-serif', marginTop: '2em', width: '100%' }}>
  <div style={{ display: 'flex', alignItems: 'flex-start', position: 'relative', width: '420px' }}>
    <div style={{ position: 'absolute', left: '-120px', top: 0, height: '100%', display: 'flex', flexDirection: 'column', justifyContent: 'space-between', fontSize: '0.85em', color: '#555', textAlign: 'right', width: '120px' }}>
      <div style={{ paddingRight: '10px' }}>↑ Higher Precedence</div>
      <div style={{ paddingRight: '10px' }}>↓ Broader Scope</div>
    </div>

    <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', width: '300px' }}>
      <div style={{ fontWeight: 'bold', backgroundColor: '#e07f00', color: 'white', padding: '8px', width: '200px', borderRadius: '6px' }}>Runtime Variables</div>
      <div style={{ marginTop: '6px', backgroundColor: '#e88f1a', color: 'white', padding: '8px', width: '220px', borderRadius: '6px' }}>Request Variables</div>
      <div style={{ marginTop: '6px', backgroundColor: '#ee9f33', color: 'white', padding: '8px', width: '240px', borderRadius: '6px' }}>Folder Variables</div>
      <div style={{ marginTop: '6px', backgroundColor: '#f4aa4a', color: 'black', padding: '8px', width: '260px', borderRadius: '6px' }}>Environment Variables</div>
      <div style={{ marginTop: '6px', backgroundColor: '#f6b45d', color: 'black', padding: '8px', width: '280px', borderRadius: '6px' }}>Collection Variables</div>
      <div style={{ marginTop: '6px', backgroundColor: '#f8be70', color: 'black', padding: '8px', width: '300px', borderRadius: '6px' }}>Global Variables</div>
    </div>
  </div>
</div>

* Runtime variables get the highest precedence.
* Prompt Variables are defined using the `{{?Prompt String}}` syntax and hence don't compete with the above.
* Process Environment Variables are accessed using the `{{process.env.VAR_NAME}}` syntax and hence don't compete with the above.

### Variable Storage

Each variable has its own storage location either within your collection file or within the app's memory. **All storage is local.**

| Variable Type       | Storage Location     |
| ------------------- | -------------------- |
| Collection          | `opencollection.yml` |
| Folder              | `folder.yml`         |
| Request             | `request.yml`        |
| Environment         | `<env-name>.yml`     |
| Runtime             | Local storage        |
| Global              | Local storage        |
| Prompt              | Never stored         |
| Process Environment | Separate `.env` file |

### Variable Data Type

<Warning>
  **Breaking change in v4.0.0:** Bruno now supports typed variables such as `string`, `number`, `boolean`, and `object`. Collections written with `@type(...)` annotations will not parse in Bruno versions older than v4.0.0.
</Warning>

Starting with v4.0.0, variables are no longer limited to strings. You can store any of the following types:

| Type      | Example value               |
| --------- | --------------------------- |
| `string`  | `"https://api.example.com"` |
| `number`  | `30`                        |
| `boolean` | `true`                      |
| `object`  | `{ "host": "localhost" }`   |

In `.yml` files, each variable is an object with a `type` field and a `data` field. In `.bru` files, types are stored using `@number`, `@boolean`, or `@object` annotations placed on the line immediately above the variable. Object values use triple-quote (`'''`) blocks.

<CodeGroup>
  ```yaml YAML format (.yml) theme={null}
  runtime:
    variables:
      - name: timeout
        value:
          type: number
          data: "30"

      - name: debug
        value:
          type: boolean
          data: "true"

      - name: config
        value:
          type: object
          data: '{"host":"localhost","port":8080}'

      - name: baseUrl
        value: https://api.example.com
  ```

  ```bru BRU format (.bru) theme={null}
  vars:pre-request {
    @number
    timeout: 30

    @boolean
    debug: true

    @object
    config: '''
      {
        "host": "localhost",
        "port": 8080
      }
    '''

    baseUrl: https://api.example.com
  }
  ```
</CodeGroup>

**Setting typed variables from scripts**

When you call `bru.setEnvVar(key, value)`, Bruno infers the type from the value you pass and writes the correct annotation automatically:

```javascript theme={null}
bru.setEnvVar("timeout", 30)            // written as @number
bru.setEnvVar("debug", true)            // written as @boolean
bru.setEnvVar("config", { a: 1 })       // written as @object
bru.setEnvVar("baseUrl", "example.com") // written as plain string (no annotation needed)
```

On subsequent reads, `timeout` resolves as a number and `config` resolves as an object. It preserves the original type across runs.

Typed values work across all variable scopes: environment, global environment, collection, folder, and request variables.

**Type selector in the UI**

In the Vars tab you can select the type of each variable using the type dropdown next to the value field.

<img src="https://mintcdn.com/bruno-a6972042-mintlify-c74cb75a/gzBEQzDtiQXT21Y4/images/screenshots/v4/chores/data-types.webp?fit=max&auto=format&n=gzBEQzDtiQXT21Y4&q=85&s=08523ffcf7acfe38297cda826457a041" alt="Variable type selector" width="2602" height="1094" data-path="images/screenshots/v4/chores/data-types.webp" />

<Note>
  **Backward compatibility:** Existing `.bru` files and environments that use plain string values continue to work without any changes or migration. The `@type(...)` annotation is only written when a non-string type is used.
</Note>

### Environment Color Coding

<Info>
  Environment color coding is now available (v3.1.0+) for Environment Variables and Global Environment Variables, making it easier to visually identify and distinguish between different environments.
</Info>

Bruno allows you to customize the visual appearance of your environments using color coding. This feature helps you quickly identify which environment you're currently working with, reducing the risk of accidentally running requests against the wrong environment.

#### Color Customization

You can assign a custom color to each environment:

1. Navigate to environment settings (top right corner)
2. Click the brush icon next to the environment and select a color from the color picker component
3. If no color is selected, a default color will be assigned

<img src="https://mintcdn.com/bruno-a6972042-mintlify-c74cb75a/gzBEQzDtiQXT21Y4/images/screenshots/variables/color-coding-env-click-pencil-icon.webp?fit=max&auto=format&n=gzBEQzDtiQXT21Y4&q=85&s=f372d609bd08b4fb5e6c2b25f575fb7d" alt="Color Picker" width="2606" height="1368" data-path="images/screenshots/variables/color-coding-env-click-pencil-icon.webp" />

#### Color-Coded Visual Indicators

Once colors are assigned, they appear in multiple places throughout the interface:

**Environment Selector Dropdown**: A small color swatch is displayed next to each environment name, making it easy to distinguish between environments at a glance.

**Active Environment Indicator**: The currently selected environment's color is prominently displayed in the environment pill in the tabs UI, providing constant visual feedback about which environment you're working with.

<img src="https://mintcdn.com/bruno-a6972042-mintlify-c74cb75a/gzBEQzDtiQXT21Y4/images/screenshots/variables/view-color-coding-env.webp?fit=max&auto=format&n=gzBEQzDtiQXT21Y4&q=85&s=7bab313aa5cc2be665eb14f137240fca" alt="Environment Color Coding" width="2606" height="1368" data-path="images/screenshots/variables/view-color-coding-env.webp" />

This visual system helps prevent mistakes when switching between development, staging, and production environments, especially in fast-paced workflows.

### Debugging Variables in Console

You can access and debug variables in the console using the following pattern:

The pattern follows `bru.get[Type]Var(key)` where:

* `[Type]` is the variable type (Runtime, Request, Folder, etc.)
* `key` is the variable name you want to access

#### Example:

```javascript theme={null}
// Basic syntax: console.log(bru.get[Type]Var(key))
console.log(bru.getVar('myVar'))           // Runtime variables
```

### Scripting API

Please see the [Scripting API](/scripting/javascript-reference#collection-variables) for more information on how to access variables in your scripts.

Prompt variables are not accessible via the Scripting API.
