the-food-group / sweater-generator

https://sweater-generator.vercel.app
0 stars 0 forks source link

Move secrets into .env file #4

Open jonatfoodgroup opened 11 months ago

jonatfoodgroup commented 11 months ago

Of course! It's a good practice to store sensitive information like API keys in environment variables in your React app. Here's how you can move your OPENAI API key into an environment variable in your Create React App:

  1. Create a .env file in the root directory of your Create React App project if it doesn't already exist.

  2. Inside the .env file, add a variable to store your OPENAI API key. You can name it something like REACT_APP_OPENAI_API_KEY:

REACT_APP_OPENAI_API_KEY=your-api-key-here

Make sure to replace your-api-key-here with your actual API key.

  1. Save the .env file.

  2. In your React component where you need to access the API key, you can use process.env to access the environment variable you just created. For example, if it's in an Image component, you can do something like this:

const apiKey = process.env.REACT_APP_OPENAI_API_KEY;

// Now you can use apiKey in your component for API requests
  1. Make sure to restart your development server (if it's running) after adding or modifying environment variables to ensure they take effect.

By using environment variables, you can keep your API keys secure and separate from your code, which is a best practice for handling sensitive information in React apps.