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

# Environment management

> Learn how to manage test data and variables across different environments with Playmatic

## Overview

Playmatic has an environment manager for storing and accessing test variables and data across different environments.

Each test environment defines the different application instances your tests can target. Each environment includes a base URL and optional variables. Environments are stored in the `playmatic.config.ts` file. When these variables are collected during the planning phase, the Playmatic agent will automatically write them in this file.

Each test environment key (e.g., "production", "staging", "development") contains:

* `baseUrl` (string): The base URL for the application instance
* `vars` (object, optional): Environment-specific variables accessible via `env.vars` in tests. Supports nested objects for organizing related configuration.

```typescript playmatic.config.ts theme={null}
import { defineConfig } from '@playmatic/sdk';

export default defineConfig({
  defaultEnv: 'staging', // default environment when using npx playwright test
  env: {
    production: {
      baseUrl: "https://playmatic.ai",
      vars: {
        testEmail: "test@playmatic.ai",
        testPassword: "securePassword123",

        // Nested objects for organized configuration
        featureFlags: {
          newDashboard: true,
          betaFeatures: false
        }
      },
    },
    staging: {
      baseUrl: "https://staging.playmatic.ai",
      vars: {}
    },
    development: {
      baseUrl: "http://localhost:3000",
      vars: {}
    },
  },
});
```

Environments must first be initialized in the `playwright.config.ts` file, and afterwards, the `baseUrl` and `vars` will be accessible by any tests during runtime.

<Warning>It is required that you use the Playmatic environment manager to store your test data and variables. Otherwise, Playmatic does not have a way to test in its cloud-based VM.</Warning>

## Using Test Environment Variables

To initialize your environment when running locally, use the `setEnvironment` function in your `playwright.config.ts` file:

```typescript playwright.config.ts theme={null}
import { setEnvironment } from "@playmatic/sdk";
import { defineConfig, devices } from "@playwright/test";
import playmaticConfig from "./playmatic.config";

// Set environment globally (agent will export PLAYMATIC_TEST_ENV, fallback to default env)
const envName = process.env.PLAYMATIC_TEST_ENV || playmaticConfig.defaultEnv;
setEnvironment(playmaticConfig.env[envName]);

export default defineConfig({
  testDir: "./tests",
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined,
  reporter: "html",
  use: {
    baseURL: playmaticConfig.env[envName].baseUrl,
    trace: "on",
    screenshot: "on",
    video: "on",
  },
  projects: [
    {
      name: "chromium",
      use: { ...devices["Desktop Chrome"] },
    },
  ],
});

```

Once the environment has been initialized, tests have access to the environment's baseUrl and variables via the `env` object. The baseUrl can be referenced via `env.baseUrl`, and any variables can be referenced using `env.vars.variableName`. Nested objects can be accessed via dot notation.

```typescript theme={null}
import { env } from '@playmatic/sdk';
import { test } from '@playwright/test';

test('User can login successfully', async ({ page }) => {
  await test.step('Navigate to login page', async () => {
    // Navigate to the environment's baseURL
    await page.goto(env.baseUrl);
  });

  await test.step('Fill in login credentials', async () => {
    // Access environment variables
    await page.fill('[name="email"]', env.vars.testEmail);
    await page.fill('[name="password"]', env.vars.testPassword);
  });

  await test.step('Click login button', async () => {
    await page.click('button[type="submit"]');
  });

  await test.step('Verify login successful', async () => {
    // Access nested objects from environment
    if (env.vars.featureFlags.newDashboard) {
      await page.waitForSelector('[data-testid="new-dashboard"]');
    } else {
      await page.waitForSelector('[data-testid="legacy-dashboard"]');
    }
  });
});
```

## Next Steps

<Columns>
  <Card title="Running Tests Locally" icon="play" href="/running-tests/running-locally">
    Learn more about running tests locally.
  </Card>

  <Card title="AI actions" icon="sparkles" href="/test-development/ai-actions">
    Learn more about AI actions.
  </Card>
</Columns>
