To add the OpenAI API key into a .env file for a React application, follow these steps:
Create a new file called .env
in the root directory of your React application.
Open the .env
file and add the following line:
REACT_APP_OPENAI_API_KEY=your_api_key_here
Replace your_api_key_here
with your actual API key.
Save the .env
file.
In your React application code, use the API key by referencing the process.env.REACT_APP_OPENAI_API_KEY
variable.
For example, in a component that uses the OpenAI API, you could do something like this:
const response = await fetch('https://api.openai.com/v1/engines/davinci-codex/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.REACT_APP_OPENAI_API_KEY}`,
},
body: JSON.stringify({
prompt: '...',
temperature: 0.5,
max_tokens: 50,
n: 1,
}),
});
This will use the API key stored in the .env
file to authenticate with the OpenAI API.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-10-25 11:00:00 +0000
Seen: 14 times
Last updated: Mar 24 '22
How to debug NextJS application in VSCode?
In the WPF application, where can I find the shared resource that has been compiled?
What is the process of combining React with Django api?
Explain what the main file is, but do not give any details about the parameters it contains.
What is the process for utilizing HTTPS during development?
What is the process for incorporating the react-native-offline package into an application?
What is the process for dockerizing an Nx React JS application?
What is the process of uploading a file using NextJS and formidable?