I’ve read a lot of posts about the use of netlify.toml for environment variables, but what I can’t seem to get a handle on is if I can use the environment variables defined in a netlify.toml within Netlify serverless functions.
For example, I’ve created a netlify.toml file with the as you see below.
When I deploy this and try to access the MY_VAR variable within the serverless function it shows as undefined. However, I did nearly the same thing only used a Gatsby function and the variable seems to be accessible.
It’s mentioned in a lot of posts that it’s not possible to use variables from netlify.toml in the Functions. Those variables are just build-time accessible and not runtime-accessible. But you could use a plugin: netlify-plugin-inline-functions-env - npm
That definitely did the trick. I have a few sites that use Netlify functions and one that uses Gatsby functions that get deployed in Netlify. What’s strange is that the Gatsby function has no environment variables defined in the UI and I’m not using the netlify-plugin-inline-functions-env - npm, yet the environment variables are getting picked up from within netlify.toml and available from within functions.
That’s interesting then. Never used Gatsby functions, so don’t know for sure but then my guess would be that it’s simply able to inject any variable it needs from process.env.
But that’s the only way it could work - either the variables ned to exist in UI, or they need to be injected during the build time.