API Key implementation shows 401 (Unauthorized) image to text api

const API_KEY = “my_api_key_here”;
const submit = document.querySelector(“#submit_btn”);
const inputPrompt = document.querySelector(“#prompt_user”);
const getImages = async() => {
const options = {
method: “POST”,
header: {
“Authorization”: Bearer ${API_KEY},
“Content-Type”: “application/json”
},
body: JSON.stringify({
“prompt”: inputPrompt.value,
“n”: 2,
“size”: “1024x1024”
})
}
try {
const response = await fetch(‘api key generation link’, options)
const data = await response.json();
console.log(data)
} catch (error) {
console.error(error)
}
console.log(options)
}

    submit.addEventListener("click", function(e) {
        getImages();
        e.preventDefault();
    })

How can i get the text to image output it shows 401 unauthorized

You should get the full response to see the error.

Check your model list by API or on the rate limit page, the access to the vision model has been changing.

how can i do that sir with the documentation its all right, and my credit balance is on free 5$ without any spent,can you guide me please?

Hi, you have a great point and thanks for the extra information and your patience.

The first thing you’ll need in order to utilize computer vision from OpenAI is access to GPT-4 models by API. OpenAI reserves some models for developer that have started a paid account, so you won’t have access to the required "gpt-4-vision-preview" until you have made your first payment to purchase account credits and unlock models.

To do that, go to your account’s billing overview page. You’ll likely have an “add payment method” button there, where you can add the credit card that you will use to fund your OpenAI API developer account. After completing the next screens, then you will be able to purchase at least the minimum of $5 to add to your account on top of your free credits. That should within minutes or hours unlock access to GPT-4 models, although you may need to generate a new API key.

Then, your code above is quite incorrect. I suspect it was written by an AI that has no clue how the OpenAI API actually works these days. Throw that all away. You’ll need to at least specify the model, and the correct format of messages. In the link below, you can choose the “vision input” method to see sample code for either making requests in python or node.js that will include an image URL or base64 encoded image. You can start with basic chat examples to ensure you can communicate with gpt-4.

https://platform.openai.com/docs/api-reference/chat/create