Get your API key

Lesson 2 of 5

Environment variables for each environment

Test values for previews, live values for production, public prefixes baked in at build time, and why you redeploy.

8 min Log in to open
One step at a time, like flipping pages.

By the end of this lesson your previews will run on test data and your live site on live data, each with its own environment variables, and you will know exactly when a change needs a redeploy.

You need: the branch-and-preview habit from Lesson 1, a .env.example file (Keep secrets secret), access to your Vercel project's settings, your coding agent · Time: 8 minutes

  1. An environment variable is a setting your code reads by name, like NEXT_PUBLIC_SUPABASE_URL, instead of having the value written inside the code. You already keep secrets this way. Now the twist: the same name can have a different value in each environment.

    Vercel has three:

    • Development: your laptop. Values come from your .env.local file.
    • Preview: every preview deployment from Lesson 1.
    • Production: the live site from main.

    For the Sharma Coaching fee tracker, make two Supabase projects: feetracker-test with fake students, and feetracker-live with the real ones. Development and Preview point at test; only Production points at live.

    A table. Four variable names in rows and three columns Development, Preview and Production. Development and Preview use the test project, test key, localhost or preview URL and a dev AI key with a 2 dollar cap. Production uses the live project, live key, sharmacoaching.in and a live AI key with its own cap.
    Same names everywhere. Only Production gets live values.
    Why?

    If previews used the live database, testing a "delete student" button would delete a real student. Separate values make previews safe to break.

Log in free to read the rest of this lesson

Every lesson is free. A free account unlocks all of them and saves your progress.

Still to come in this lesson:

  1. Write an environment sheet first
  2. Add the values in Vercel, per environment
  3. Public prefixes are baked in at build time
  4. Changed a value? Redeploy
  5. Recap