Netlify: env vars not loading in functions [2026 fix]

Netlify functions can't access env vars because they're not deployed in netlify.toml or .env.production is missing from build context.

Netlify: env vars not loading in functions [2026 fix]

TL;DR

Cause: Environment variables defined in Netlify UI aren't automatically passed to serverless functions without explicit configuration in netlify.toml. Fix: Add environment block to netlify.toml under [functions] and redeploy.

---

Real Console Error Messages

``` 1. TypeError: Cannot read property 'apiKey' of undefined at /var/task/functions/api.js:12:15

2. process.env.DATABASE_URL is undefined in Netlify Function at Object.<anonymous> (/var/task/functions/db.js:5:3)

3. ReferenceError: STRIPE_SECRET_KEY is not defined at Runtime.handler (/var/task/functions/checkout.js:18:12)

4. Error: Missing required environment variable: NEXT_PUBLIC_API_ENDPOINT at Object.handler (/var/task/functions/query.js:3:7)

5. Warning: Function environment build succeeded but no env vars detected [Functions runtime] Environment not injected into function scope ```

---

The Problem: Broken vs. Fixed Code

❌ What's NOT Working

netlify.toml (incomplete): ```toml [build] command = "npm run build" publish = "dist" ```

functions/api.js: ```javascript const stripe = require('stripe'); const client = stripe(process.env.STRIPE_SECRET_KEY);

exports.handler = async (event) => { // STRIPE_SECRET_KEY is undefined here ❌ return { statusCode: 200, body: JSON.stringify({ key: process.env.STRIPE_SECRET_KEY }) }; }; ```

Why it fails: Environment variables from Netlify UI are injected into the build process but NOT automatically forwarded to function runtime scope.

---

✅ The Fix

netlify.toml (corrected): ```toml [build] command = "npm run build" publish = "dist"

[functions] environment = { STRIPE_SECRET_KEY = "$STRIPE_SECRET_KEY", DATABASE_URL = "$DATABASE_URL" } ```

functions/api.js (no changes needed): ```javascript const stripe = require('stripe'); const client = stripe(process.env.STRIPE_SECRET_KEY);

exports.handler = async (event) => { // STRIPE_SECRET_KEY is now defined ✅ return { statusCode: 200, body: JSON.stringify({ key: process.env.STRIPE_SECRET_KEY }) }; }; ```

Alternative approach using explicit function-level config: ```toml [[functions]] name = "api" environment = { STRIPE_SECRET_KEY = "$STRIPE_SECRET_KEY" }

[[functions]] name = "checkout" environment = { STRIPE_SECRET_KEY = "$STRIPE_SECRET_KEY", DATABASE_URL = "$DATABASE_URL" } ```

---

Step-by-Step Fix (2am emergency mode)

1. Open netlify.toml in your repo root 2. Add this section (or update existing [functions]): ```toml [functions] environment = { STRIPE_SECRET_KEY = "$STRIPE_SECRET_KEY", DATABASE_URL = "$DATABASE_URL" } ``` Replace variable names with YOUR actual env var keys from Netlify UI.

3. Verify in Netlify UI: Site Settings → Environment variables. Confirm they exist. 4. Git commit & push your netlify.toml change 5. Trigger redeploy: Deployments tab → Trigger deploy (or wait for auto-deploy) 6. Check function logs: Functions → View logs to confirm vars are loaded

Note on version behavior: We're assuming Netlify Functions runtime 2024+ with [functions] block support. If using Netlify CLI < v13, netlify.toml syntax may differ slightly—check your CLI version with netlify --version.

---

Still broken? Check these too

1. Build env vars vs. runtime env vars confusion Environment variables set in Netlify UI are available during BUILD but not automatically in FUNCTION runtime. The [functions] block explicitly maps them. [Related: understanding build context](/?guide=netlify-build-context).

2. Typo in variable name or missing dollar sign ```toml # WRONG environment = { STRIPE_KEY = "STRIPE_SECRET_KEY" } # RIGHT environment = { STRIPE_KEY = "$STRIPE_SECRET_KEY" } ``` The $ prefix references the UI-defined variable.

3. Function not using Node.js 16+ Older runtimes may not support environment injection. Check your function's runtime in Netlify UI or [see runtime settings](/?guide=netlify-function-runtimes). Update to latest Node LTS if possible.

---

Debugging Checklist

  • [ ] Confirmed env var exists in Netlify UI (Site Settings → Environment variables)
  • [ ] Added [functions] block to netlify.toml with correct syntax
  • [ ] Used $VARIABLE_NAME format (dollar sign required)
  • [ ] Committed and pushed netlify.toml to git
  • [ ] Triggered fresh deploy (not a cached build)
  • [ ] Checked function logs: Netlify UI → Functions → View logs
  • [ ] Tested with simple console.log: console.log('Var:', process.env.MY_VAR)
  • ---

    Official Documentation

    [Netlify Functions Environment Variables - Official Docs](https://docs.netlify.com/functions/overview/#environment-variables)

    ---

    Found a different variation? Drop it in the comments—this might save someone else's 2am panic.

    🔥 0d
    LIVE
    ↓ PlanetScale rage spiking •⚠ Vercel pricing complaints •↑ Railway gaining fast •↑ Supabase happiness rising •↑ Resend loved by devs •↓ PlanetScale rage spiking •⚠ Vercel pricing complaints •↑ Railway gaining fast •↑ Supabase happiness rising
    DEVELOPER PAIN RADAR // Loading...

    Developers complain.
    Opportunities appear.

    We track what developers are struggling with today — and what opportunities that creates.

    —
    guides today
    —
    avg happiness
    🔥 Pain
    📖 Guides
    🔭 Explore
    👤 Mine
    🔥 Pain Radar — rage scores today
    ↗ share
    💡 Opportunity Feed — pain = market gap
    📈 Tool Momentum
    all scores →
    📖 Latest Guide
    all guides →
    📖 All Guides
    📊 Tool Scores
    + Submit
    📰 Hacker News
    ➕ Submit a Tool
    ← back