Environment variables are values your app needs that exist separately from the app's source code. They allow you to use sensitive information like API keys and database credentials without storing them in version control. During development, and at build time, variables defined in a `.env` or `.env.local` file will be added to the environment: ```env /// file: .env.local API_KEY=19f401ba-e8b0-48c4-8c77-b0ebb26d97fe ``` By default, every environment variable is implicitly available inside your app via the following modules: - [`$env/static/private`]($env-static-private) - [`$env/static/public`]($env-static-public) - [`$env/dynamic/private`]($env-dynamic-private) - [`$env/dynamic/public`]($env-dynamic-public) ## Explicit environment variables As of SvelteKit 2.62, you can opt into _explicit_ environment variables, in which case you instead import environment variables from these modules: - [`$app/env/private`]($app-env-private) - [`$app/env/public`]($app-env-public) Additionally, the [`$app/environment`]($app-environment) module is renamed to [`$app/env`]($app-env). > [!NOTE] Explicit environment variables will become the default in SvelteKit 3. The `$env/*` modules, along with `$app/environment`, will be removed. ### Setup To opt in, update your configuration... ```js /// file: svelte.config.js export default { kit: { experimental: { +++explicitEnvironmentVariables: true+++ } } }; ``` ...and add a `src/env.ts` (or `src/env.js`) file that exports a `variables` object: ```ts /// file: src/env.ts import { defineEnvVars } from '@sveltejs/kit/hooks'; export const variables = defineEnvVars({ // ... }); ``` Each value in the object passed to [`defineEnvVars`](@sveltejs-kit-hooks#defineEnvVars) is an [`EnvVarConfig`](@sveltejs-kit#EnvVarConfig) object that configures the environment variable. > [!NOTE] `defineEnvVars` returns its argument unaltered — it exists purely to help with type safety. ### Private variables By default, all variables are considered private. For example, you don't want to reveal your `API_KEY`: ```ts /// file: src/env.ts import { defineEnvVars } from '@sveltejs/kit/hooks'; export const variables = defineEnvVars({ +++API_KEY: {}+++ }); ``` > [!NOTE] Since no configuration is needed for this variable, we can use an empty object (`{}`). Now that `API_KEY` is defined, it can be imported into app code via `$app/env/private`: ```js import { API_KEY } from '$app/env/private'; ``` The `$app/env/private` module cannot be imported into code that runs in the browser, so that you can't accidentally reveal your secrets in a JavaScript bundle. ### Public variables Some variables are perfectly safe — necessary, even — to expose to the browser. For these, we can specify `public: true`: ```ts /// file: src/env.ts import { defineEnvVars } from '@sveltejs/kit/hooks'; export const variables = defineEnvVars({ GOOGLE_ANALYTICS_ID: { +++public: true+++ } }); ``` `GOOGLE_ANALYTICS_ID` can now be imported from `$app/env/public`, or used in your `app.html` template as `%sveltekit.env.GOOGLE_ANALYTICS_ID%`: ```html
%sveltekit.head%