Netlify: env vars not loading in functions [2026 fix]
Netlify functions can't read env vars because they're not deployed or the function runtime doesn't have access—redeploy with netlify.toml or use process.env correctly.
Netlify: env vars not loading in functions [2026 fix]
TL;DR
Cause: Environment variables defined in Netlify UI aren't automatically available to serverless functions unless explicitly configured in netlify.toml or you're running functions locally without proper setup.
Fix: Add environment block to netlify.toml OR rebuild/redeploy after setting vars in the UI (cache invalidation issue).
---
Real Console Error Messages
``` Error: Cannot read property 'DATABASE_URL' of undefined at Object.<anonymous> (/var/task/api/db.js:5:15) ```
``` TypeError: process.env.STRIPE_KEY is undefined at runtime Stack: at handler (/var/task/functions/checkout.js:12:8) ```
``` [Function] Error: Missing required environment variable: API_TOKEN Message: env var passed but function doesn't see it (null check failed) ```
``` Warning: Accessing undefined environment variable 'NEXT_PUBLIC_API_URL' at Runtime.handler (/var/task/.next/server/chunks/pages_api_route.js) ```
``` 502 Bad Gateway - Your function returned an error: ReferenceError: DB_USER is not defined ```
---
The Problem: Broken vs. Fixed Code
❌ BROKEN: netlify.toml doesn't declare function env vars
```toml [build] command = "npm run build" functions = "netlify/functions"
Missing: environment variables not declared here!
```Function code tries to use it:
```javascript // netlify/functions/api.js exports.handler = async (event) => { const dbUrl = process.env.DATABASE_URL; // Returns undefined! const stripe = process.env.STRIPE_KEY; // Returns undefined! return { statusCode: 500, body: JSON.stringify({ error: 'Env vars missing' }) }; }; ```
---
✅ FIXED: Declare env vars in netlify.toml
```toml [build] command = "npm run build" functions = "netlify/functions"
[functions] environment = { DATABASE_URL = "postgres://...", STRIPE_KEY = "sk_live_..." } ```
Or use UI AND redeploy:
1. Go to Site settings → Environment variables
2. Add your vars (e.g., DATABASE_URL, STRIPE_KEY)
3. Trigger a rebuild: Go to Deploys → Trigger deploy
Function code (now works):
```javascript // netlify/functions/api.js exports.handler = async (event) => { const dbUrl = process.env.DATABASE_URL; // ✅ Works! const stripe = process.env.STRIPE_KEY; // ✅ Works! if (!dbUrl || !stripe) { return { statusCode: 500, body: JSON.stringify({ error: 'Check netlify.toml or UI settings' }) }; } return { statusCode: 200, body: JSON.stringify({ success: true }) }; }; ```
---
Why This Happens
Netlify functions have TWO separate env var scopes:
1. Build-time (site build process) 2. Runtime (when function executes)
Variables set only in the UI don't always propagate to the function runtime without a redeploy. The netlify.toml approach explicitly binds them.
Local development caveat: If you're testing locally with netlify dev, make sure you have a .env.local file:
```bash
.env.local (only for local dev, never commit)
DATABASE_URL=postgres://localhost/test STRIPE_KEY=sk_test_123 ```---
Still broken? Check these too
1. [Serverless function timeouts and cold starts](/?guide=netlify-timeout) — Your function might crash before logging env vars. Add explicit error handling and check function duration.
2. [Next.js API routes vs. Netlify Functions confusion](/?guide=nextjs-netlify-functions) — If using Next.js on Netlify, NEXT_PUBLIC_* vars work differently than in standard functions. Rebuild required.
3. Function deployment cache issue — Sometimes Netlify doesn't flush the function bundle cache. Force clear: delete the .netlify folder locally, commit changes, and push to trigger a clean rebuild.
---
Verification Checklist
✓ Restart netlify dev after changing .env.local
✓ Redeploy after changing UI environment variables
✓ Check that netlify.toml isn't in .gitignore (must be committed)
✓ Verify env var names match exactly (case-sensitive!)
✓ Don't mix NEXT_PUBLIC_ prefix rules with standard function vars
✓ Check function logs in Netlify dashboard → Functions tab → Recent logs
---
Official Resources
---
Found a different variation? Drop it in the comments
This guide covers the most common scenario (functions not seeing UI-defined vars), but Netlify environment handling varies by deployment method, build configuration, and function runtime. If you hit a different error or workaround, share it below—community knowledge saves the next person at 2am.