-3

I WANT TO CREATE AN AI IMAGE GENERATOR USING OPENAI HOW CAN I SOLVE THIS PROBLEM ??

const API_KEY = "KEY..."

const submitIcon = document.querySelector("#submit-icon")
const inputElement = document.querySelector("input")

const getImages = async () => {
    const options = {
        method : "POST",
        Headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${API_KEY}`,
        },
        body: JSON.stringify({
            
            "prompt": inputElement.value,
            "n":1,
            "size":"1024x1024"
        })
    }
    try{
       const response = await fetch(" https://api.openai.com/v1/images/generations ",options)
       const data = await response.json()
       console.log(data)
    
    } catch(error){
        console.error(error)
    }
}

submitIcon.addEventListener('click', getImages)
New contributor
Rishi Jain is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • This question is similar to: Setting authorization header in Fetch API. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
    – 0stone0
    Commented Jul 3 at 10:55
  • Start by not shouting at us (writing in all upper-case). Commented Jul 3 at 10:58
  • Typo --> Headers ---> headers
    – 0stone0
    Commented Jul 3 at 11:00

1 Answer 1

-2

First Issue: The Authorization header should be enclosed in backticks (``)

Second Issue: Headers should be lowercase (headers)

And then your options should be looking something like this as for final output

const options = {
        method: "POST",
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${API_KEY}`,
        },
        body: JSON.stringify({
            "prompt": inputElement.value,
            "n": 1,
            "size": "1024x1024"
        })
    };
3
  • First issue isn't an issue, op is already using backticks. For the second one please don't answer typo's, just comment those.
    – 0stone0
    Commented Jul 3 at 10:59
  • now my code is showing 'billing_hard_limit_reached', message: 'Billing hard limit has been reached', param: null, type: 'invalid_request_error'}
    – Rishi Jain
    Commented Jul 3 at 12:13
  • @RishiJain seems you've reached the limit of your billing. That's not a code problem.
    – VLAZ
    Commented Jul 3 at 14:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.