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

# Environment Variables

> Store and manage reusable values across your tests with environment-specific configurations in Test Data.

## Introduction

Environment variables provide a centralized way to store values that can be shared across all tests in your project. They are managed through the **Test Data** section in your workspace and work across both the **Stably Web Editor** and the **Stably SDK**.

<Tip>
  Environment variables live at the project level and are shared by every test in the workspace. For test-specific values, use [local variables](/platform-capabilities/variables#local-variables) instead.
</Tip>

***

## Adding Environment Variables

<Steps>
  <Step title="Navigate to Test Data">
    Go to **Test Data → Variables** in your workspace.
  </Step>

  <Step title="Add variable">
    Click **+ Add Variable** to create a new environment variable.
  </Step>

  <Step title="Set the scope">
    Select which environment this variable belongs to (e.g., `staging`, `production`, or `All Environments`). Variables scoped to a specific environment are only available when tests run in that environment. If you're unsure, choose **All Environments** so every test can access it.
    <Note>Use environment-scoped variables to store values like API keys, base URLs, and configuration that differ between staging and production.</Note>
  </Step>

  <Step title="Configure its value">
    Enter the variable name and value. JSON values are also supported — they will be accessible in your tests as parsed JSON objects.
  </Step>

  <Step title="Set the sensitivity">
    Toggle **Sensitive** to encrypt and hide the value after creation.
    This will prevent the value from being captured in Playwright traces.
    <Warning>Once a variable is marked as sensitive, you will not be able to view its current value or change it back to non-sensitive. You can still update the value by providing a new one.</Warning>
  </Step>

  <Step title="Save the variable">
    Save the variable to make it available across all of your tests.
  </Step>
</Steps>

<Frame>
  <img src="https://mintcdn.com/stablyai/SbGP2Et5yfutCjiA/images/test-data/example-var.png?fit=max&auto=format&n=SbGP2Et5yfutCjiA&q=85&s=5e5cab3955dc670edaee888f6a79b8d1" style={{ borderRadius: '0.5rem' }} width="2562" height="658" data-path="images/test-data/example-var.png" />
</Frame>

***

## Downloading Variables as `.env`

You can download all your environment variables as a `.env` file directly from the dashboard. This makes it easy to sync your variables to your local development environment for use with the **Stably SDK**.

Click the **Download .env** button in the Environment Variables section of the Test Data page:

<Frame>
  <img src="https://mintcdn.com/stablyai/4CWg8RUuZ4Cq7R0Q/images/test-data/download-env-button.png?fit=max&auto=format&n=4CWg8RUuZ4Cq7R0Q&q=85&s=df0aace7f8555f0b4e20234a4833a0fd" alt="Download .env button on the Test Data page" style={{ borderRadius: '0.5rem' }} width="1600" height="940" data-path="images/test-data/download-env-button.png" />
</Frame>

The downloaded file contains all your variables. Non-sensitive variables include their full values, while sensitive variables appear as empty placeholders with a comment (e.g., `# MY_SECRET - SENSITIVE (value not exported)`). Place the file in your project root for local test execution.

<Warning>
  Sensitive variable values are **not** included in the download. You will need to fill them in manually in the `.env` file.
</Warning>

<Tip>
  When you add or update variables in the dashboard, click **Download .env** again to keep your local file in sync.
</Tip>

***

## Using Environment Variables

Environment variables are accessed via `process.env.VARIABLE_NAME` in your test code. How they get loaded depends on whether you're using Stably on the web or locally.

<Tabs>
  <Tab title="Stably Web">
    Environment variables you configure in **Test Data** are **automatically injected** into the AI agent's environment. No additional setup is needed.

    The AI agent will use `process.env.VARIABLE_NAME` in the generated test code:

    ```typescript theme={null}
    import { test, expect } from '@stablyai/playwright-test';

    test('login flow', async ({ page }) => {
      await page.goto(process.env.BASE_URL!);
      await page.fill('#username', process.env.TEST_USERNAME!);
      await page.fill('#password', process.env.TEST_PASSWORD!);
    });
    ```

    You can also reference variables naturally in your AI instructions — the agent knows which environment variables are available and will match them to your intent:

    * "Navigate to the base URL"
    * "Log in with the test account credentials"
    * "Use the API key to authenticate"

    <Tip>
      You don't need to specify the exact variable name in your instructions — the AI agent will pick the right one automatically.
    </Tip>
  </Tab>

  <Tab title="Stably CLI">
    The Stably CLI can load variables from a named **[Environment](/stably2/environments)** stored on Stably using the `--env` flag. This is the recommended approach — no local files to manage.

    ```bash theme={null}
    # Load variables from your "Staging" environment
    stably --env Staging test

    # Works with all CLI commands
    stably --env Production create "login test"
    ```

    You can also load variables from a local `.env` file:

    ```bash theme={null}
    stably test --env-file .env.staging
    ```

    Or combine both:

    ```bash theme={null}
    stably --env Production test --env-file .env
    ```

    #### Variable Precedence

    When using the CLI, variables can come from multiple sources. They are resolved in the following order (highest priority wins):

    1. Stably internals (`STABLY_API_KEY`, `STABLY_PROJECT_ID`)
    2. `--env` — remote environment from Stably
    3. `--env-file` — local `.env` file(s)
    4. `process.env` — system/shell environment

    Once loaded, variables are available as `process.env.VARIABLE_NAME` in your tests:

    ```typescript theme={null}
    import { test, expect } from '@stablyai/playwright-test';

    test('login flow', async ({ page }) => {
      await page.goto(process.env.BASE_URL!);
      await page.fill('#username', process.env.TEST_USERNAME!);
      await page.fill('#password', process.env.TEST_PASSWORD!);
    });
    ```
  </Tab>

  <Tab title="Manual Setup">
    You can also load environment variables manually using `dotenv` in your Playwright config. This works with any Playwright setup, including without the Stably CLI.

    <Steps>
      <Step title="Download your variables">
        Go to **Environments** in the dashboard, open your environment, and click **Download .env**.
      </Step>

      <Step title="Place the file in your project">
        Move the downloaded `.env` file to your project root directory.
      </Step>

      <Step title="Load variables in your config">
        ```bash theme={null}
        npm install -D dotenv
        ```

        Then add to the top of your `playwright.config.ts`:

        ```typescript theme={null}
        import dotenv from 'dotenv';
        import path from 'path';

        dotenv.config({ path: path.resolve(__dirname, '.env') });
        ```
      </Step>
    </Steps>

    Once loaded, variables are available as `process.env.VARIABLE_NAME` in your tests:

    ```typescript theme={null}
    import { test, expect } from '@stablyai/playwright-test';

    test('login flow', async ({ page }) => {
      await page.goto(process.env.BASE_URL!);
      await page.fill('#username', process.env.TEST_USERNAME!);
      await page.fill('#password', process.env.TEST_PASSWORD!);
    });
    ```
  </Tab>
</Tabs>

***

## Editing and Deleting Variables

To **edit** a variable, click on it in the variables list to expand the edit form. You can update its name, value, and environment scope. For sensitive variables, you can provide a new value but cannot view the current one.

To **delete** a variable, click the delete icon next to it in the variables list.

***

## Best Practices

| ✔️ Do                                                                     | ❌ Avoid                                              |
| ------------------------------------------------------------------------- | ---------------------------------------------------- |
| Store secrets and API keys as **Sensitive** variables.                    | Hard-coding credentials directly in tests.           |
| Use descriptive `UPPER_SNAKE_CASE` names (e.g., `API_KEY`, `BASE_URL`).   | Abbreviations that will be hard to remember (`akp`). |
| Create environment-specific variables for different staging environments. | Using the same credentials across all environments.  |
| Use environment variables for values shared across multiple tests.        | Creating duplicate variables with the same value.    |

<Tip>
  **See also:** [Variables](/platform-capabilities/variables) for information about local variables and other variable types.
</Tip>
