0Pricing
Sveltejs Academy · Lesson

Environment Variables: $env/static and $env/dynamic

Access environment variables safely on the server and client in SvelteKit.

Environment Variables: $env/static and $env/dynamic is a free Sveltejs Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

SvelteKit env API

SvelteKit exposes env vars through four modules: static/private, static/public, dynamic/private, dynamic/public.

Static Private

Read at build time, server-only. Stripped from client bundle.

import { DB_URL } from "$env/static/private";

Static Public

Read at build time, must start with PUBLIC_, included in client bundle.

import { PUBLIC_API_URL } from "$env/static/public";

Dynamic Private

Read at runtime on the server. Useful for changing config without rebuilds.

import { env } from "$env/dynamic/private";
console.log(env.SECRET);

Dynamic Public

Read at runtime, available on both client and server. Must start with PUBLIC_.

Choosing the Right One

Use static when values do not change at runtime; dynamic when they do (e.g. feature flags).

Secrets Never Public

Never prefix secrets with PUBLIC_; they will leak to the client bundle.

Env File

Store values in .env (gitignored) and .env.example (committed).

Type Safety

Static vars are typed automatically. Dynamic vars are typed via Record.

Vercel and Cloudflare

Each platform has its own env var UI. Sync across environments deliberately.

Tests

Mock env modules in tests if you depend on them.

Quick Check

What prefix is required for public env vars?

Recap

Use $env/static/private for secrets, PUBLIC_-prefixed vars for client-safe values. Choose static vs dynamic by need.

Frequently asked questions

Is the “Environment Variables: $env/static and $env/dynamic” lesson free?

Yes — the full text of “Environment Variables: $env/static and $env/dynamic” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.

What will I learn in “Environment Variables: $env/static and $env/dynamic”?

Access environment variables safely on the server and client in SvelteKit. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Environment Variables: $env/static and $env/dynamic” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. CSRF Protection in Form Actions
  2. XSS Prevention in {#html}
  3. Environment Variables: $env/static and $env/dynamic
  4. Rate Limiting with Hooks
← Back to Sveltejs Academy