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.
command = "npm run build"
publish = "public/"
functions = "functions"
package = "@netlify/plugin-gatsby"
MY_VAR = "some value"
MY_VAR = "some other value"
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.
Can anyone steer me in the right direction?
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.
If you’re prefixing variables with
GATSBY, I believe Gatsby is automatically injecting it in the code just like it does for client-side scripts.
Yep, your 100% correct. However, in this case I have no variables prefixed with GATSBY, just regular ole variables.
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
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.