Confusion about environment variables blog post

I’m reading the blog post about environment variables: Environment variables in Next.js and Netlify

Part “Setting environment variables in the Netlify UI” appears to present the UI environment variables as an alternative to .env.local? Aren’t these UI environment variables only for build time?

As you should never push .env files as they can contain secrets, you can store your env variables in the UI after deployment. You can use environment variables during build time (no secret keys) and in Netlify functions, for example.

My current task is to set a send grid api key, which locally I load like this:
sgMail.setApiKey(process.env.SENDGRID_API_KEY)

I guess that I can set it at build time, but it appears not to work. I added it to Build & Deploy > Environment > Environment variables.

I also don’t know where to see the runtime api logs to confirm that this is the problem (opened a separate ticket for that).

Edit: noting also there was a new build and deployment after adding this environment variable.

Ok, it’s working now with the build time environment variable. No idea what it was - I didn’t change anything other than uploading the .env file (I’ll change the api key) and removing it again. Maybe there’s some delay and it wasn’t available for the first deployments I did. Thanks for the help in any case.

You can use environment variables during build time (no secret keys) and in Netlify functions, for example

Wait a moment, this is a secret key - and it has to be available to send emails. How do I make it available if not with a build time environment variable?

All variables added to UI are available for all uses. However, if you use the new service: Manage scopes and deploy contexts for environment variables | Netlify Blog, you can specify where an variable would be accessible.

1 Like