{
  "doc": {
    "id": "api/commands/env",
    "title": "cy.env()",
    "description": "Securely read environment variables in Cypress tests using cy.env(). Learn syntax, examples, security benefits, and how to migrate from Cypress.env().",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/env.md",
    "version": "29f95bf8bb06f320986f3749f5bf09a35a409eab",
    "updated_at": "2026-09-04T10:49:54.630Z",
    "headings": [
      {
        "id": "api/commands/env#env",
        "text": "env",
        "level": 1
      },
      {
        "id": "api/commands/env#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/env#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/env#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/env#yields",
        "text": "Yields",
        "level": 3
      },
      {
        "id": "api/commands/env#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/env#single-variable",
        "text": "Single Variable",
        "level": 3
      },
      {
        "id": "api/commands/env#multiple-variables",
        "text": "Multiple Variables",
        "level": 3
      },
      {
        "id": "api/commands/env#use-environment-variables-across-tests",
        "text": "Use environment variables across tests",
        "level": 3
      },
      {
        "id": "api/commands/env#handle-required-vs-optional-variables",
        "text": "Handle required vs optional variables",
        "level": 3
      },
      {
        "id": "api/commands/env#chaining-with-other-commands",
        "text": "Chaining with other commands",
        "level": 3
      },
      {
        "id": "api/commands/env#use-cy-env-in-custom-commands",
        "text": "Use cy.env in custom commands",
        "level": 3
      },
      {
        "id": "api/commands/env#suppress-command-logging",
        "text": "Suppress command logging",
        "level": 3
      },
      {
        "id": "api/commands/env#handling-the-yielded-value-safely",
        "text": "Handling the yielded value safely",
        "level": 2
      },
      {
        "id": "api/commands/env#assert-on-a-derived-value-not-on-the-secret",
        "text": "Assert on a derived value, not on the secret",
        "level": 3
      },
      {
        "id": "api/commands/env#avoid-its-and-invoke-on-the-yielded-object",
        "text": "Avoid .its() and .invoke() on the yielded object",
        "level": 3
      },
      {
        "id": "api/commands/env#keep-the-value-out-of-downstream-command-logs",
        "text": "Keep the value out of downstream command logs",
        "level": 3
      },
      {
        "id": "api/commands/env#why-use-cy-env",
        "text": "Why use cy.env()?",
        "level": 2
      },
      {
        "id": "api/commands/env#secure-access-to-sensitive-values",
        "text": "Secure access to sensitive values",
        "level": 3
      },
      {
        "id": "api/commands/env#when-to-use-cy-env-vs-cypress-expose",
        "text": "When to use cy.env() vs Cypress.expose()",
        "level": 3
      },
      {
        "id": "api/commands/env#use-cypress-expose-for-public-configuration",
        "text": "Use Cypress.expose() for public configuration",
        "level": 4
      },
      {
        "id": "api/commands/env#use-cy-env-for-sensitive-or-secret-values",
        "text": "Use cy.env() for sensitive or secret values",
        "level": 4
      },
      {
        "id": "api/commands/env#migrate-from-cypress-env",
        "text": "Migrate from Cypress.env()",
        "level": 2
      },
      {
        "id": "api/commands/env#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/env#read-only-behavior",
        "text": "Read-only behavior",
        "level": 3
      },
      {
        "id": "api/commands/env#test-configuration-overrides",
        "text": "Test configuration overrides",
        "level": 3
      },
      {
        "id": "api/commands/env#case-sensitivity",
        "text": "Case sensitivity",
        "level": 3
      },
      {
        "id": "api/commands/env#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/env#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "env"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Securely read environment variables set in your Cypress configuration, `cypress.env.json`, or `CYPRESS_*` environment variables from within Cypress tests."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Read-Only Command"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.env()` is read-only and can only retrieve environment variables. It cannot set or modify environment variables. To set environment variables, use one of the supported methods described in the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/guides/environment-variables.md",
            "children": [
              {
                "type": "text",
                "value": "Environment Variables & Secrets"
              }
            ]
          },
          {
            "type": "text",
            "value": " guide."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.env()` logs the key names you ask for and never the values. That protection ends at the command boundary. The object `cy.env()` yields is an ordinary JavaScript object, and Cypress does not mask, redact, or track the values inside it. Assertions, "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/its.md",
            "children": [
              {
                "type": "text",
                "value": "`.its()`"
              }
            ]
          },
          {
            "type": "text",
            "value": ", "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/invoke.md",
            "children": [
              {
                "type": "text",
                "value": "`.invoke()`"
              }
            ]
          },
          {
            "type": "text",
            "value": ", and any chained command that fails can all print a value to the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/open-mode.md#Command-Log",
            "children": [
              {
                "type": "text",
                "value": "Command Log"
              }
            ]
          },
          {
            "type": "text",
            "value": " and the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/open-mode.md#Console-output",
            "children": [
              {
                "type": "text",
                "value": "console output"
              }
            ]
          },
          {
            "type": "text",
            "value": ". What happens to a value after `cy.env()` yields it is up to you. See "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/env.md#Handling-the-yielded-value-safely",
            "children": [
              {
                "type": "text",
                "value": "Handling the yielded value safely"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Syntax"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(keys)\ncy.env(keys, options)"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Usage"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.js"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.ts"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "const { defineConfig } = require('cypress')\n\nmodule.exports = defineConfig({\n  env: {\n    apiUrl: 'https://api.example.com',\n    apiKey: 'secret-key-12345',\n  },\n  expose: {\n    environment: 'staging', // Public configuration value\n  },\n})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { defineConfig } from 'cypress'\n\nexport default defineConfig({\n  env: {\n    apiUrl: 'https://api.example.com',\n    apiKey: 'secret-key-12345',\n  },\n  expose: {\n    environment: 'staging', // Public configuration value\n  },\n})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// Get a single environment variable\ncy.env(['apiUrl']).then(({ apiUrl }) => {\n  cy.request(`${apiUrl}/users`).its('status').should('eq', 200)\n})\n\n// Get multiple environment variables\ncy.env(['apiUrl', 'apiKey']).then(({ apiUrl, apiKey }) => {\n  cy.request({\n    url: `${apiUrl}/users`,\n    headers: { Authorization: `Bearer ${apiKey}` },\n  })\n    .its('status')\n    .should('eq', 200)\n})\n\n// With options\ncy.env(['apiUrl'], { log: false }).then(({ apiUrl }) => {\n  // Use apiUrl\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Arguments"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "keys (String[])"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "An array of environment variable keys to retrieve from Cypress. These environment variables can be set via any of the methods described in the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/guides/environment-variables.md",
            "children": [
              {
                "type": "text",
                "value": "Environment Variables & Secrets"
              }
            ]
          },
          {
            "type": "text",
            "value": " guide."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Environment variables cannot be set using "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/configuration.md#Test-Configuration",
            "children": [
              {
                "type": "text",
                "value": "test configuration"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "If a variable is not defined, its value will return `undefined` in the returned object."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "options (Object)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pass an options object to change the default behavior of `cy.env()`."
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Option"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Default"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Description"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`log`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`true`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Displays the command in the "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/open-mode.md#Command-Log",
                    "children": [
                      {
                        "type": "text",
                        "value": "Command log"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": ". Only variable names are logged, never values."
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`timeout`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "4000"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Time to wait for `cy.env()` to resolve before timing out."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Yields"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.env()` yields an object with the values found for the environment variable keys requested."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['apiUrl', 'apiKey']).then((env) => {\n  // env = { apiUrl: 'https://api.example.com', apiKey: 'secret-key-12345' }\n})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Examples"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Single Variable"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Get a single environment variable:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['apiUrl']).then(({ apiUrl }) => {\n  cy.visit(apiUrl)\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Multiple Variables"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Get multiple environment variables at once:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['apiUrl', 'apiKey', 'timeout']).then(({ apiUrl, apiKey, timeout }) => {\n  cy.request({\n    url: `${apiUrl}/users`,\n    headers: { Authorization: `Bearer ${apiKey}` },\n    timeout: timeout || 5000,\n  })\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Use environment variables across tests"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Store environment variables for use across multiple tests by using a `before()` hook:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "describe('API tests', () => {\n  let apiBaseUrl\n\n  before(() => {\n    cy.env(['apiBaseUrl']).then(({ apiBaseUrl: url }) => {\n      apiBaseUrl = url\n    })\n  })\n\n  it('can make requests', () => {\n    cy.request(`${apiBaseUrl}/users`).its('status').should('eq', 200)\n  })\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Handle required vs optional variables"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Variables that are not set will be `undefined`:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['requiredVar', 'optionalVar']).then(({ requiredVar, optionalVar }) => {\n  if (requiredVar === undefined) {\n    throw new Error('requiredVar must be set in Cypress configuration')\n  }\n\n  // Use optionalVar only if it's defined\n  if (optionalVar) {\n    // Use optionalVar\n  }\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Chaining with other commands"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['baseUrl']).then(({ baseUrl }) => {\n  cy.visit(baseUrl)\n  cy.get('h1').should('be.visible')\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Use cy.env in custom commands"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Create custom commands that use `cy.env()`:"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress/support/commands.js"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress/support/commands.ts"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// cypress/support/commands.js\nCypress.Commands.add('apiRequest', (endpoint, options = {}) => {\n  cy.env(['apiUrl', 'apiKey']).then(({ apiUrl, apiKey }) => {\n    cy.request({\n      url: `${apiUrl}${endpoint}`,\n      headers: {\n        Authorization: `Bearer ${apiKey}`,\n        ...options.headers,\n      },\n      ...options,\n    })\n  })\n})\n\n// In your test\ncy.apiRequest('/users').its('status').should('eq', 200)"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "declare global {\n  namespace Cypress {\n    interface Chainable {\n      apiRequest(\n        endpoint: string,\n        options?: Partial<Cypress.RequestOptions>\n      ): Chainable<Cypress.Response<any>>\n    }\n  }\n}\n\n// cypress/support/commands.js\nCypress.Commands.add('apiRequest', (endpoint, options = {}) => {\n  cy.env(['apiUrl', 'apiKey']).then(({ apiUrl, apiKey }) => {\n    cy.request({\n      url: `${apiUrl}${endpoint}`,\n      headers: {\n        Authorization: `Bearer ${apiKey}`,\n        ...options.headers,\n      },\n      ...options,\n    })\n  })\n})\n\n// In your test\ncy.apiRequest('/users').its('status').should('eq', 200)"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Suppress command logging"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Hide the `cy.env()` entry from the Command Log. Because `cy.env()` logs key names and never values, use this option when you don't want the key names visible in the Command Log:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['acmeMigrationKey'], { log: false }).then(({ acmeMigrationKey }) => {\n  // The command and the key name don't appear in the Command Log\n})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Handling the yielded value safely"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.env()` logs the key names you ask for and never the values. That guarantee stops the moment the command yields. The object you receive is an ordinary JavaScript object, and Cypress does not mask, redact, or track the values inside it."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Keep the value inside a `.then()` callback and pass it straight to the command that needs it. "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/then.md",
            "children": [
              {
                "type": "text",
                "value": "`.then()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " and "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/spread.md",
            "children": [
              {
                "type": "text",
                "value": "`.spread()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " add no entry to the Command Log, so the value stays out of it."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Assert on a derived value, not on the secret"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Every assertion writes to the Command Log, and the entry contains the values being compared. Assertions accept no logging options, so you cannot suppress them. Assert on a boolean you derive from the value instead."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Incorrect Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['apiKey']).should('deep.include', { apiKey: 'secret-key-12345' })\n// ❌ Command Log: assert expected { apiKey: 'secret-key-12345' } to deep\n// include { apiKey: 'secret-key-12345' }"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['apiKey']).then(({ apiKey }) => {\n  expect(Boolean(apiKey)).to.be.true\n})\n// ✅ Command Log: assert expected true to be true"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Calling `expect()` inside a `.then()` callback still creates an assertion entry, so a `.then()` callback alone is not enough. What you assert on is what matters."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Avoid .its() and .invoke() on the yielded object"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/its.md",
            "children": [
              {
                "type": "text",
                "value": "`.its()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " and "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/invoke.md",
            "children": [
              {
                "type": "text",
                "value": "`.invoke()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " both add the subject they were applied to and the value they yield to the console output, so `cy.env(['apiKey']).its('apiKey')` prints the environment variable value twice. Read the property inside a `.then()` callback instead."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Incorrect Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// ❌ Prints the environment variable value to the console output twice\ncy.env(['apiKey']).its('apiKey')"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['apiKey']).then(({ apiKey }) => {\n  // ✅ Use apiKey here\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Keep the value out of downstream command logs"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Commands that accept a `log` option, such as "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/request.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.request()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " and "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/type.md",
            "children": [
              {
                "type": "text",
                "value": "`.type()`"
              }
            ]
          },
          {
            "type": "text",
            "value": ", leave their entry out of the Command Log when you pass `{ log: false }`. Use it on any command you hand the value to:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.env(['apiKey']).then(({ apiKey }) => {\n  cy.request({\n    url: 'https://api.example.com/users',\n    headers: { Authorization: `Bearer ${apiKey}` },\n    log: false,\n  })\n\n  cy.get('[data-testid=\"token-field\"]').type(apiKey, { log: false })\n})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`{ log: false }` hides the entry from the Command Log. It does not redact the value, and it has no effect on assertions."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Why use cy.env()?"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.env()` was introduced to replace `Cypress.env()`, which was deprecated in Cypress 15.10.0 and removed in Cypress 16.0."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Secure access to sensitive values"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Unlike `Cypress.env()` (removed in Cypress 16.0), which hydrated all environment variables into the browser:"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.env()` only exposes the variables you explicitly request"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "Variables are not automatically serialized into browser state"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "Only requested variables are passed into `cy.origin()` contexts"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "Sensitive data is easier to audit and control"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "This reduces accidental exposure and limits the blast radius of secrets."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "When to use cy.env() vs Cypress.expose()"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Both "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/env.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.env()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " and "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/cypress-api/expose.md",
            "children": [
              {
                "type": "text",
                "value": "`Cypress.expose()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " provide access to configuration values in Cypress, but they serve different security and execution needs. Choosing the right API helps avoid accidental exposure of sensitive data and keeps configuration intent clear."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Use Cypress.expose() for public configuration"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Recommended when:"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "Values are public or non-sensitive - Examples include feature flags, API versions, environment labels, or plugin configuration that is safe to appear in browser state."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "Synchronous access is needed - `Cypress.expose()` returns values immediately, without requiring Cypress command chaining."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Use `cy.env()` for sensitive or secret values"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Choose `cy.env()` when security, scoping, and controlled access matter. Recommended when:"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "Values are sensitive - API keys, passwords, tokens, or any data that should not be broadly exposed to the browser."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "Security is a priority - `cy.env()` only exposes the variables you explicitly request and does not automatically serialize them into browser state."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "You're already working within Cypress command chains: `cy.env()` is asynchronous and designed to be used inside Cypress tests and hooks."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Example: choosing the right API"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// ✅ Use cy.env() for sensitive values\ncy.env(['apiKey']).then(({ apiKey }) => {\n  cy.request({\n    url: 'https://api.example.com/users',\n    headers: { Authorization: `Bearer ${apiKey}` },\n  })\n})\n\n// ✅ Use Cypress.expose() for public configuration\nconst apiVersion = Cypress.expose('apiVersion') // Synchronous, public value\nconst featureFlag = Cypress.expose('featureFlag') // Safe to expose in browser"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "See "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/cypress-api/expose.md",
            "children": [
              {
                "type": "text",
                "value": "`Cypress.expose()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " for more details."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Migrate from Cypress.env()"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.env()` replaces the removed `Cypress.env()` API, which was deprecated in Cypress 15.10.0 and removed in Cypress 16.0."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "On Cypress ^15.10.0, after migrating all usages, you can prevent future use of `Cypress.env()` by setting `allowCypressEnv: false` in your Cypress configuration. In Cypress 16.0, `allowCypressEnv` has been removed — `Cypress.env()` is no longer available regardless of configuration."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Cypress ^15.10.0 only:"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.js"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.ts"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "const { defineConfig } = require('cypress')\n\nmodule.exports = defineConfig({\n  allowCypressEnv: false,\n})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { defineConfig } from 'cypress'\n\nexport default defineConfig({\n  allowCypressEnv: false,\n})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "See the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/migration-guide.md#Migrating-away-from-Cypressenv",
            "children": [
              {
                "type": "text",
                "value": "Migration Guide"
              }
            ]
          },
          {
            "type": "text",
            "value": " for detailed migration instructions."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Notes"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Read-only behavior"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`cy.env()` is read-only and cannot set environment variables at runtime."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "See the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/guides/environment-variables.md",
            "children": [
              {
                "type": "text",
                "value": "Environment Variables & Secrets"
              }
            ]
          },
          {
            "type": "text",
            "value": " guide for more details on setting environment variables."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Test configuration overrides"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Environment variables cannot be set using "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/configuration.md#Test-Configuration",
            "children": [
              {
                "type": "text",
                "value": "test configuration"
              }
            ]
          },
          {
            "type": "text",
            "value": ". On Cypress ^15.10.0, this was enforced when `allowCypressEnv` was set to `false`. In Cypress 16.0, `env` in test configuration overrides has been removed entirely — if absolutely necessary, `expose` can be used instead."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Case sensitivity"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Variable names are case-sensitive and must match exactly how they are defined in your configuration:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// Configuration\n{\n  env: {\n    apiUrl: 'https://api.example.com',\n  }\n}\n\n// In test\ncy.env(['apiUrl'])  // ✅ Gets 'https://api.example.com'\ncy.env(['APIURL']) // ❌ Returns undefined"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "History"
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Version"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Changes"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#15-10-0",
                    "children": [
                      {
                        "type": "text",
                        "value": "15.10.0"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`cy.env()` command added"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "See also"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/cypress-api/expose.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`Cypress.expose()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/configuration.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "Configuration"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/migration-guide.md#Migrating-away-from-Cypressenv",
                    "children": [
                      {
                        "type": "text",
                        "value": "Migration Guide: migrating away from `Cypress.env()`"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "token_estimate": 2276
}