---
id: api/cypress-api/env
title: Cypress.env | Cypress Documentation
description: 'Deprecated: Get and set Cypress environment variables in your tests'
section: api
source_path: docs/api/cypress-api/env.mdx
version: 6a908a532b1fca4ed18538a4c1c5a9bc7f24f403
updated_at: '2026-05-01T19:25:18.656Z'
---
# Cypress.env | Cypress Documentation

**Deprecated**

`Cypress.env()` was deprecated in Cypress `15.10.0` and will be removed in a future major version.

Cypress recommends migrating to use [`cy.env()`](/llm/markdown/api/commands/env.md) or [`Cypress.expose()`](/llm/markdown/api/cypress-api/expose.md) instead.

Please see the [Migration Guide](/llm/markdown/app/references/migration-guide.md#Migrating-away-from-Cypressenv) for more information.

# Cypress.env

`get` and `set` environment variables _in your tests_.

**Scope**

Environment variables set using `Cypress.env` _are only in scope for the current spec file._

Cypress runs each spec file in isolation: the browser is exited between specs. Environment variables added or changed in one spec won't be visible in other specs.

## Syntax

```
Cypress.env()Cypress.env(name)Cypress.env(name, value)Cypress.env(object)
```

### Arguments

**name _(String)_**

The name of the environment variable to get or set.

**value _(String)_**

The value of the environment variable to set.

**object _(Object)_**

Set multiple environment variables with an object literal.

## Examples

### No Arguments

#### Get all environment variables from the Cypress configuration

*   cypress.config.js
*   cypress.config.ts

```
const { defineConfig } = require('cypress')module.exports = defineConfig({  env: {    apiUrl: 'https://api.example.com',    apiVersion: 'v1',  },})
```

```
import { defineConfig } from 'cypress'export default defineConfig({  env: {    apiUrl: 'https://api.example.com',    apiVersion: 'v1',  },})
```

```
Cypress.env() // => {apiUrl: 'https://api.example.com', apiVersion: 'v1'}
```

### Name

#### Return a single environment variable from the Cypress configuration

**Boolean**

We automatically normalize both the key and the value when passed via the command line. Cypress will automatically convert values into Number or Boolean.

```
CYPRESS_HOST=laura.dev CYPRESS_IS_CI=true CYPRESS_API_VERSION=123 cypress run
```

```
Cypress.env('HOST') // => "laura.dev"Cypress.env('IS_CI') // => trueCypress.env('API_VERSION') // => 123
```

### Name and Value

#### Change environment variables from the Cypress configuration from within your tests

**Scope**

Remember, any changes that you make to environment variables using this API will only be in effect for the remainder of the tests _in the same spec file._

*   cypress.config.js
*   cypress.config.ts

```
const { defineConfig } = require('cypress')module.exports = defineConfig({  env: {    host: 'laura.dev',  },})
```

```
import { defineConfig } from 'cypress'export default defineConfig({  env: {    host: 'laura.dev',  },})
```

```
Cypress.env('host', 'http://server.dev.local')Cypress.env('host') // => http://server.dev.local
```

### Object

#### Override multiple values from the Cypress configuration by passing an object

*   cypress.config.js
*   cypress.config.ts

```
const { defineConfig } = require('cypress')module.exports = defineConfig({  env: {    host: 'laura.dev',    apiVersion: 'v1',  },})
```

```
import { defineConfig } from 'cypress'export default defineConfig({  env: {    host: 'laura.dev',    apiVersion: 'v1',  },})
```

```
Cypress.env({  host: 'http://server.dev.local',  apiVersion: 'v2',})Cypress.env() // => {apiVersion: 'v2', host: 'http://server.dev.local'}
```

### From a plugin

Here's an example that uses `Cypress.env` to access an environment variable that's been [dynamically set in a plugin](/llm/markdown/app/plugins/plugins-guide.md).

Use this approach to grab the value of an environment variable _once_ before any of the tests in your spec run.

*   cypress.config.js
*   cypress.config.ts

```
const { defineConfig } = require('cypress')module.exports = defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      config.env.apiBaseUrl =        process.env.NODE_ENV === 'qa'          ? 'https://api-qa.example.com'          : 'https://api.example.com'      return config    },  },})
```

```
import { defineConfig } from 'cypress'export default defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      config.env.apiBaseUrl =        process.env.NODE_ENV === 'qa'          ? 'https://api-qa.example.com'          : 'https://api.example.com'      return config    },  },})
```

```
// cypress/e2e/api-tests.cy.jsdescribe('API tests', () => {  let apiBaseUrl  before(() => {    apiBaseUrl = Cypress.env('apiBaseUrl')  })  it('can make requests to the correct environment', () => {    cy.request({      method: 'GET',      url: `${apiBaseUrl}/users`,    }).then((response) => {      expect(response.status).to.eq(200)    })  })})
```

## Notes

### Why would I ever need to use environment variables?

The [Environment Variables & Secrets](/llm/markdown/app/guides/environment-variables.md) guide explains common use cases.

### Can I pass in environment variables from the command line?

Yes. You can do that and much more.

The [Environment Variables & Secrets](/llm/markdown/app/guides/environment-variables.md) guide explains the other ways you can set environment variables for your tests.

## History

| Version | Changes |
| --- | --- |
| [15.10.0](/llm/markdown/app/references/changelog.md#15-10-0) | `Cypress.env()` API deprecated |

## See also

*   The [Environment Variables & Secrets](/llm/markdown/app/guides/environment-variables.md) guide
*   [Cypress configuration](/llm/markdown/app/references/configuration.md)
*   [`cy.env()`](/llm/markdown/api/commands/env.md)
*   [Cypress.expose()](/llm/markdown/api/cypress-api/expose.md)
*   [Migration Guide to `cy.env()`](/llm/markdown/app/references/migration-guide.md#Migrating-away-from-Cypressenv)
