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

# Environments

> Define where your application runs (URL, credentials, and variables) and run the same tests against dev, staging, or production without touching a test.

![Environments list under Project Settings — the Active environment carries a badge](https://mintcdn.com/testspriteinc/NgwJ2RcRMRwjh7Vv/images/environment-Overview.png?fit=max\&auto=format\&n=NgwJ2RcRMRwjh7Vv\&q=85\&s=0527bce121efc7f2ef2c204a815747ce)

## What an Environment Is

An **environment** is one place your application runs. Every project has at least one, and each bundles what differs between deployments:

| Field | What it holds |
| :- | :- |
| **Name** | A label like `dev`, `staging`, or `production` |
| **Website URL** | The base URL tests run against |
| **Authentication** | The credentials tests authenticate with (backend projects: bearer token / API key / basic; frontend projects: test account login) |
| **Custom Variables** | Your own key–value pairs (environment variables) |

Tests describe *what* to verify; the environment supplies *where* and *with which values*. That's what lets one suite cover several deployments.

Environments live in **Project Settings → Test Execution → Environment**. Click any environment in the list to edit it.

## Creating an Environment

Click **Add Environment** in the top-right of the Environments page and fill in the deployment's details, typically one entry each for `dev`, `staging`, and `production`.

![Add Environment button on the Environments page](https://mintcdn.com/testspriteinc/NgwJ2RcRMRwjh7Vv/images/environment-create-1.png?fit=max\&auto=format\&n=NgwJ2RcRMRwjh7Vv\&q=85\&s=d34900280fc369d579987c6612fa9ff5)

![Add Environment dialog — name, URL, authentication type, custom variables, and Auto-refresh Login](https://mintcdn.com/testspriteinc/NgwJ2RcRMRwjh7Vv/images/environment-create-2.png?fit=max\&auto=format\&n=NgwJ2RcRMRwjh7Vv\&q=85\&s=e3c9de86508de92a564291ecc8eb23d0)

* **Authentication** sets how tests authenticate against this deployment: `None`, `Basic`, `Bearer Token`, or `API Key`. You can refine it per endpoint later (see below).
* **Auto-refresh Login** can be set up right away if the deployment's tokens expire; TestSprite then fetches a fresh token before every run. See [Auto-Auth](https://docs.testsprite.com/web-portal/core/api/auto-auth.md).
* The first environment a project gets becomes the **Active** one automatically.

## Environment Variables (Custom Variables)

### Where they're defined

Open an environment and scroll to **Custom Variables**. Click **Add Variable** and enter a `KEY = value` pair. Variables are defined **per environment**, so the same key usually holds a different value in each:

```
# staging                          # production
TENANT_ID = acme-staging           TENANT_ID = acme
SUPPORT_EMAIL = qa@acme.dev        SUPPORT_EMAIL = support@acme.com
```

![Custom Variables section of the Edit Environment dialog — Add Variable creates a KEY = value row](https://mintcdn.com/testspriteinc/NgwJ2RcRMRwjh7Vv/images/environment-custom-variable.png?fit=max\&auto=format\&n=NgwJ2RcRMRwjh7Vv\&q=85\&s=5d6a34918725831340dcc13d53b2e060)

### How tests reference them

You don't paste values into test cases. When TestSprite generates test code, values from the environment are stamped as `VAR_{key}` placeholders (for example `VAR_{TENANT_ID}`) and resolved from the **currently selected environment** right before each test runs. So:

* **Switching environments never requires regenerating tests.** Every run re-resolves the placeholders.
* **A variable a test references must exist in every environment you run against**, or the placeholder has nothing to resolve to.

> [!NOTE]
>
> Environment variables are **static configuration you define** (a tenant ID, a support email). [Dynamic Variables](https://docs.testsprite.com/web-portal/core/api/dynamic-variables.md) are **values captured at run time** from one test's response and fed into another (a freshly created `user_id`).

## Per-Endpoint Authentication and Headers (Backend)

A backend environment also has the **Customize Header** panel, the same tool you used when reviewing the API doc. It sets authentication and extra request headers **per endpoint**, for APIs where a login route is public, an admin group needs a different key, or every call needs an `x-org-id`.

![Edit Environment dialog for a backend project — Customize Header chips, the endpoint list, and the selected endpoint's credential](https://mintcdn.com/testspriteinc/NgwJ2RcRMRwjh7Vv/images/environment-custom-header.png?fit=max\&auto=format\&n=NgwJ2RcRMRwjh7Vv\&q=85\&s=f8d27c16dec40d220fed7bd5c24753d9)

Create a credential or header set once, then apply it to the endpoints that need it:

> [tab] Authentication Type
>
> A credential set: `Bearer` / `API Key` / `Basic` plus the credential value. Endpoints without one fall back to the environment-level authentication chosen at creation.
>
> ![Customize Header dialog — Authentication Type kind with Bearer credential](https://mintcdn.com/testspriteinc/NgwJ2RcRMRwjh7Vv/images/environment-Authentication-Type.png?fit=max\&auto=format\&n=NgwJ2RcRMRwjh7Vv\&q=85\&s=cb8efbe64ae754719d0ef160dd0bf5e7)

> [tab] Extra Header
>
> Plain request headers (e.g. `x-org-id`) sent alongside the credential. Different header sets can stack on the same endpoint.
>
> ![Customize Header dialog — Extra Header kind with a KEY : value row](https://mintcdn.com/testspriteinc/NgwJ2RcRMRwjh7Vv/images/environment-Extra-Header.png?fit=max\&auto=format\&n=NgwJ2RcRMRwjh7Vv\&q=85\&s=aac1666cf6429df705a06b7b56ffbfdf)

Each chip shows how many endpoints carry it (`61 in use`). **Apply** opens an endpoint picker, and selecting an endpoint in the list shows exactly which credential and headers its tests will send.

## Managing Multiple Environments

* **One environment is always Active** (marked with a badge). Runs you trigger from the portal execute against the Active environment.
* Open an environment and click **Set As Active** to switch.
* Every project keeps at least one environment; the last one can't be deleted.
* Deleting an environment doesn't affect existing tests; they resolve against whichever environment is Active when they next run.

> [!WARNING]
>
> Point non-production environments at real deployments you own, and be deliberate before making `production` the Active environment: API tests send real requests, and tests that create or delete records will do so against whatever the Active environment points at.

## Running the Same Suite Against Different Environments

Because tests store placeholders, "run against staging instead" is a configuration change, not a test change:

**1. Ad-hoc runs**

Set the target environment as **Active**, then trigger the run. Every URL, credential, and `VAR_{key}` resolves from that environment.

![Set As Active button in the Edit Environment dialog](https://mintcdn.com/testspriteinc/NgwJ2RcRMRwjh7Vv/images/environment-Ad-hoc.png?fit=max\&auto=format\&n=NgwJ2RcRMRwjh7Vv\&q=85\&s=93a7524c23f07e3069dd64ffe3ebdf25)

**2. Test Lists and schedules**

A [Test List](https://docs.testsprite.com/web-portal/maintenance/test-lists.md) can pin an environment **per project** it contains, and a schedule runs its list with those pins. Your nightly can run against staging while your Active environment stays on dev.

## What Belongs Where

Rule of thumb: **identity and secrets go in the authentication configuration; everything else that varies per deployment goes in variables or the URL field.**

| Value | Put it in | Why |
| :- | :- | :- |
| Base URL / domain | The environment's **Website URL** field | It's what every request is joined onto |
| Tenant ID, org ID, region, feature toggles | **Custom Variables** | Plain configuration, resolved into tests as `VAR_{key}` |
| Tokens, API keys, passwords | **Customize Header → Authentication Type** (or [Auto-Auth](https://docs.testsprite.com/web-portal/core/api/auto-auth.md)) | Masked in the UI, redacted from exported code, and rotatable in one place |
| A tenant header every request must carry (e.g. `x-org-id`) | **Customize Header → Extra Header** | A variable can't attach itself to a request on its own |

A token in a custom variable technically works, but it loses masking, export redaction, and one-click rotation, and anyone opening the environment dialog can read it.

## Related

> [card] [Auto-Auth](https://docs.testsprite.com/web-portal/core/api/auto-auth.md)
>
> Fetch a fresh token before every run

> [card] [Dynamic Variables](https://docs.testsprite.com/web-portal/core/api/dynamic-variables.md)
>
> Runtime values that flow between tests

> [card] [Test Lists](https://docs.testsprite.com/web-portal/maintenance/test-lists.md)
>
> Curated suites with per-project environment pins


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.