I'm trying to set an environment variable on my Netlify CMS through Netlify's Deploy build environment

Hey there, I’m trying to set an environment variable on my Netlify CMS through Netlify’s Deploy build environment.
I have my GOOGLE_API_KEY in there with my key as value. I’d love to get that back in order to use it on my website.

I have tried to follow this : [Support Guide] Using environment variables on Netlify correctly - #5 by talves

I have installed dotenv-webpack with npm i

I have created a file called scripts/create-env.js

I made sure I put a .env in my gitignore

scripts/create-env.js :

const fs = require('fs')

fs.writeFileSync('./.env', `GOOGLE_API_KEY=${process.env.GOOGLE_API_KEY}\n`)

But I have no idea on how to make netlify integrate that variable and that value : it is supposed to be part of my build but I don’t know where to put it.
node ./scripts/create-env.js && <your_existing_webpack_build_command>

Where is it supposed to be?? What is my existing webpack build command ?

Thank you a thousand times !!

This process of getting the environment variable in to a .env file is based on the requirement for needing it to be used by the build. To clear up for yourself or anyone else reading about this solution, I’ll try to explain the reasoning first. The OP needed a way to get environment variables out using the dotenv library but wanted to not store the variables in their repository for obvious reasons (security). This allowed the pulling of these values from process.env.

Bringing secret keys into the browser (client) code is NOT a good practice and should be avoided 100%. If you need to process an API call to Google using the key, you should make a call to a lambda function (Netlify functions) that uses the value to make the API call for you and returns the resulting data to the client.

@FCS I’m not sure this will help you but maybe it will clear up some things. We need more information on what you’re trying to do here to be able to answer your questions.

2 Likes

That’s actually very clear and I didn’t understand it was this specific. Thank you.

I’m all up for using the lambdas which looks totally cool, although I’m definitely not used to it.
Do you have any documentation concerning the usage of Environement Variables in lambdas ?
Or rather than a doc, a reproductible similar function I could use for it ?

Thank you for all your precision, I appreciate that deeply.

hi there @FCS - did you read this guide already? it may contain some useful information for you!