The thing is when I try to call openAI API for image generation it gives me 400 bad request, but my code is clean and correct don’t know what is wrong going on. I’ll be providing screenshots also.
You unfortunately leaked your own API key in your previous image, you should delete your API key immediately and create a new on to avoid someone abusing your account
You’re not using dotenv as as intended, you are supposed to store your OpenAI API key in a separate file and load it into your Python script:
1 Create a new file in your project’s root directory and name it .env.
2 Open the .env file in a text editor and add the following line:
OPENAI_API_KEY=your_api_key_here
Replace your_api_key_here with your actual OpenAI API key.
3 In your Python script, add the following code at the top:
from dotenv import load_dotenv
import os
load_dotenv() # load the environment variables from the .env file
openai_api_key = os.getenv('OPENAI_API_KEY') # get the value of the OPENAI_API_KEY environment variable
# use the openai_api_key variable in your code
The load_dotenv() function loads the environment variables from the .env file into the script’s environment.
The os.getenv('OPENAI_API_KEY') function gets the value of the OPENAI_API_KEY environment variable from the script’s environment.
You can then use the openai_api_key variable in your code to authenticate your OpenAI API requests.
Note: Make sure that the .env file is not committed to your version control system (e.g. Git) as it contains sensitive information. Add it to your .gitignore file to ensure it is not accidentally committed.
Thanks for the advice. But actually, I was using dotenv from the start. When I was facing this issue I thought for a second that this issue of mine might be related to some problem in .env file. That’s why I used the API key in my code wide open. I’ll use another API key for sure though.
Also I’m using node.js and the problem still is there.
I’m happy to hear that,
I’m not super familiar with axios, but your error is a malformed request error, if i where you, I would add a line to print() the request to figure out why it’s malformed
Thanks for your help, the issue is solved. I checked it later and found out that all the credits were used. So I created a new account and new API key and the issue was solved.
Thanks for your help, the issue is solved. I checked it later and found out that all the credits were used. So I created a new account and new API key and the issue was solved.