Netlify "Function invocation failed" (502): 6 causes, and why your function URL is public
Your Netlify site loads, but the API route fails: Function invocation failed or a 502 from /.netlify/functions/your-function, or Cannot find module. Here are the usual causes, plus the security check that AI-built apps
Your Netlify site loads, but the API route fails:
Function invocation failed or a 502 from /.netlify/functions/your-function, or Cannot find module.
Here are the usual causes, plus the security check that AI-built apps on proofforge.live's radar skip.
6 causes
-
Missing environment variable. The value is in your local
.envbut not in Netlify's site settings. Add it, then trigger a new deploy. - Variable scoped to the wrong context. Netlify lets you scope variables to builds, functions or runtime. A key set for builds only is undefined inside the function.
-
Dependency not bundled. The package sits in
devDependencies, or a native module is not supported. Move it todependenciesand check the function logs. -
Wrong path or export. The function must be in the configured functions folder and export a
handler(or a default export for the newer format). - Timeout. Functions have a short default time limit. A slow third-party call returns a timeout error, not a code error.
- Wrong HTTP method or CORS preflight. The browser sends OPTIONS first. If the function only handles POST, the real request never goes out.
Read the function logs in the Netlify dashboard first. The real error is almost always there, not in the browser.
The security check
Functions are public URLs. Anyone can call /.netlify/functions/your-function without your front end.
- Does the function check who is calling? A signed-in user token, not just a hidden button in the UI.
- Does it use a service key to read or write data for any caller? Then any caller can do the same.
- Does it have a limit? An open function that calls a paid API lets a stranger spend your money.
- Does it return secrets or full database rows in an error message?
A "fix" that works by removing the auth check to stop a 401 is the one to refuse.
An outside view
ProofForge at proofforge.live runs a free public-side scan of AI-built apps. It never signs in, so it can show what an anonymous visitor can reach, including exposed function routes and secret-shaped strings in your bundle. It cannot see your function code or your Netlify settings, so pair it with your own log review.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.