Something is wrong but not able to figure out what it is

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.

Getting this error-
Error: Request failed with status code 400
at createError (C:\ss\Image-Generation-MERN-App\server\node_modules\openai\node_modules\axios\lib\core\createError.js:16:15)
at settle (C:\ss\Image-Generation-MERN-App\server\node_modules\openai\node_modules\axios\lib\core\settle.js:17:12)
at IncomingMessage.handleStreamEnd (C:\ss\Image-Generation-MERN-App\server\node_modules\openai\node_modules\axios\lib\adapters\http.js:322:11)
at IncomingMessage.emit (node:events:532:35)
at endReadableNT (node:internal/streams/readable:1346:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
},
adapter: [Function: httpAdapter],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
xsrfCookieName: ‘XSRF-TOKEN’,
xsrfHeaderName: ‘X-XSRF-TOKEN’,
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: {
Accept: ‘application/json, text/plain, /’,
‘Content-Type’: ‘application/json’,
‘User-Agent’: ‘OpenAI/NodeJS/3.2.1’,
Authorization: ‘Bearer ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■NeuYOk5P’,
‘Content-Length’: 154
},
method: ‘post’,
data: ‘{“prompt”:“3D render of a cute tropical fish in an aquarium on a dark blue background, digital art”,“n”:1,“size”:“1024x1024”,“response_format”:“b64_json”}’,
url: ‘https://api.openai.com/v1/images/generations

Hey champ, and welcome to the forum

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 :hugs:

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.

Hope that will help keep you safe

2 Likes

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 :laughing:

1 Like

@N2U is correct, 400 has to to with invalid request, you should console.log() to check you request structure.

Most likely the prompt is undefined when the api call is made.

2 Likes

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.

1 Like

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.

hey , my code is exactly same (traversy media yt),i changed my api too with new account but still facing error 404, URL is not defined