Netlify einvironment variables

PLEASE help us help you by writing a good post!

The better the post - the faster the answer.

I tried to get the environment variable set in the Netlify page for the site and did not get the environment variable. This is a web app using javascript.

The environment variable was read with

//const { OPENAI_API_KEY } = process.env;
// also tried process.env.KEY
Then through another Discord channel
const OPENAI_API_KEY = import.meta.env.OPENAI_API_KEY;
// The helper was using typescipt

A console.log on the key reviels nothing.

You are not using Netlify Functions. Where are you using the variables? If you’re trying to use the variable in the client-side script, you’re basically exposing the variable to the users.

Here is part of the code that shows how and where the key is used and got.

try {
console.log(“in callai”);
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY // the key!
const response = await{
model: ‘gpt-3.5-turbo’,
messages: messages

Yes, but it looks like you’re doing this from the frontend? As mentioned, if you use environment variables in the frontend, there’s no point in using environment variables at all.

What am I then suppost to do. Please provide the code.

We’re not going to provide code - we don’t provide code-level support. You should look into using server side functionality like Long-running Edge Functions | Edge Functions on Netlify ( Inside that, you can then use your environment variables without exposing them to anyone.

The code line : const OPENAI_API_KEY = import.meta.env.OPENAI_API_KEY; had to have VITE_ prefixed with the key resp. import.meta.env.VITE_OPENAI_API_KEY. But as this is a frontend solution the key is exposed in the console.