SvelteKit 3 wants your env vars declared in src/env.ts
I added Spotify to this site and spent an embarrassing amount of time staring at a 503. The .env file was fine, the dev server had been restarted, and the keys were right there. The route still said they were missing.
What I tried
$env/dynamic/private: empty, plus a deprecation warning.$env/static/private: also empty, same warning.process.env: empty too. Vite doesn't copy.envinto it for server code.
The warning said to use $app/env/private instead, but importing from it alone gave a 500.
The fix
In SvelteKit 3 the variables have to be declared in src/env.ts. Nothing is exposed until you list it.
import { defineEnvVars } from '@sveltejs/kit/env';
export const variables = defineEnvVars({
SPOTIFY_CLIENT_ID: {},
SPOTIFY_CLIENT_SECRET: {},
SPOTIFY_REFRESH_TOKEN: {}
});Then import them from the new module in any server file:
import { SPOTIFY_CLIENT_ID } from '$app/env/private';Variables are private by default. Add public: true to a variable and it moves to $app/env/public instead. You can also attach a schema or a validator function, so a missing key fails loudly at startup rather than as a mystery 503 later.
The lesson I keep relearning: when a framework deprecates an API, read what replaced it before assuming your config is broken.