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
API_KEY=19f401ba-e8b0-48c4-8c77-b0ebb26d97fe
After following the setup below, they can be imported via the following modules:
The $env/* modules, along with $app/environment were deprecated in SvelteKit 3 (and will be removed in SvelteKit 4) in favour of explicit environment variables that were added in SvelteKit 2.62 as an experimental option.
Setup
Add a src/env.ts (or src/env.js) file that exports a variables object:
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
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:
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Hint: Be careful with the global flag g in your regex pattern, as it can lead to unexpected results. See MDN for more information.
requirement
The regex pattern.
A regex action.
(/G-[A-Z0-9]+/))}});
If you don’t want to bring in a schema library, you can pass a function that returns the (possibly transformed) value, or throws an error explaining the problem:
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Returns true if the sequence of elements of searchString converted to a String is the
same as the corresponding elements of this object (converted to a String) starting at
position. Otherwise returns false.
('G-'))thrownewErrorvarError:ErrorConstructornew(message?:string,options?:ErrorOptions)=>Error(+1overload)('expected a Google Analytics ID');returnvalue(parameter)value:string;}}});
If a value is invalid, the app will fail to start (or build). To opt out of one or the other, use building from $app/env along with a validator that accepts an optional value:
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
reference({SECRET(property)SECRET:{schema:v.StringSchema<undefined>|v.OptionalSchema<v.StringSchema<undefined>,undefined>;}:{ // optional when building but required when starting the appschema(property)schema:v.StringSchema<undefined>|v.OptionalSchema<v.StringSchema<undefined>,undefined>:building(alias)constbuilding:booleanimportbuilding
SvelteKit analyses your app during the build step by running it. During this process, building is true. This also applies during prerendering.
You can use validators to make values optional, or transform them (such as turning a string into a boolean, or parsing JSON) — see your validation library’s documentation to learn how.
Static variables
By default, variables are dynamic. If a variable is configured with static: true, it will be inlined into your application code, enabling optimisations like dead-code elimination:
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
reference({SHOW_DEBUG_OVERLAY(property)SHOW_DEBUG_OVERLAY:{public:true;static:true;schema:v.SchemaWithPipe<readonly[v.OptionalSchema<v.StringSchema<undefined>,"">,v.TransformAction<string,boolean>]>;}:{public(property)public:true:true,static(property)static:true:true, // coerce to true/falseschema(property)schema:v.SchemaWithPipe<readonly[v.OptionalSchema<v.StringSchema<undefined>,"">,v.TransformAction<string,boolean>]>:vimportv.pipe(alias)pipe<v.OptionalSchema<v.StringSchema<undefined>,"">,v.TransformAction<string,boolean>>(schema:v.OptionalSchema<v.StringSchema<undefined>,"">,item1:v.TransformAction<string,boolean>|v.PipeAction<string,boolean,never>):v.SchemaWithPipe<readonly[v.OptionalSchema<v.StringSchema<undefined>,"">,v.TransformAction<string,boolean>]>(+20overloads)exportpipe
Adds a pipeline to a schema, that can validate and transform its input.
Because this variable is static, the <DebugOverlay> component shown here will be excluded from the JavaScript bundle unless SHOW_DEBUG_OVERLAY is truthy:
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
Utility for defining environment variables,
which are made available via $app/env/public and $app/env/private.
import{defineEnvVars}from'@sveltejs/kit/env';import*asvfrom'valibot';exportconstvariables=defineEnvVars({API_URL:{schema:v.pipe(v.string(),v.url())},PORT:{schema:(value)=>{if(value===undefined)return3000;constport=Number(value);if(!Number.isInteger(port))thrownewError('PORT must be an integer');returnport;}}});
reference({CACHE_TTL_SECONDS(property)CACHE_TTL_SECONDS:{description:string;}:{description(property)description:string:'How long to cache responses, in seconds'}});
Hovering over CACHE_TTL_SECONDS in your app code will show the description.