Cant connect Api there are few error massege kindly assist me thank you

i am getting this error , but the usage shows 0.02 used and i am getting ‘‘internal server error’’ and this

{
    "error": {
        "message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

Welcome to the community @emilanthonyemilantho

Please share the code making the API call.

2 Likes

hi sure,here is the code kindly view it thank you in advance.

app.post('/api/chatGPT', async (req, res) => {
  const userText = req.body.text;
  console.log('User Text:', userText);

  const apiKey = 'Bearer ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■XNTdHjRk'; // Corrected API key format
  const maxRetries = 3; // Number of retry attempts
  let retries = 0;

  while (retries < maxRetries) {
    try {
      const gpt3Response = await axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', {
        prompt: userText,
        max_tokens: 100,
      }, {
        headers: {
          'Authorization': apiKey, // Corrected header format
        },
      });

      if (gpt3Response.status === 200) {
        console.log('GPT-3 Response:', gpt3Response.data.choices[0].text);

        const botResponse = gpt3Response.data.choices[0].text;
        res.json({ response: botResponse });
        return; // Successful response, exit the retry loop
      } else {
        console.error('Unexpected API response:', gpt3Response.statusText);
      }
    } catch (error) {
      console.error('Error occurred:', error);
    }

    retries++;
    console.log(`Retry attempt ${retries} of ${maxRetries}`);
    await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second before retrying
  }

  console.error('Max retries reached. Unable to get a valid response.');
  res.status(500).json({ response: 'Internal Server Error' });
});

Firstly: Please revoke your API key, and use a new one next time. You shouldn’t share your API key.

I noticed there are several discrepancies in your understanding of the API.

I’d recommend reading chat completion docs. It should help you get to speed.

Also it’s much better and convenient to use the OpenAI - nodeJS library.

1 Like

Working curl example

./curl “your message”

#!/bin/bash
curl -v https://api.openai.com/v1/chat/completions
-H “Content-Type: application/json”
-H "Authorization: Bearer $(cat /etc/keys/key.txt) "
-d '{
“model”: “gpt-3.5-turbo”,
“messages”: [
{
“role”: “system”,
“content”: "None "
},
{
“role”: “assistant”,
“content”: “None”

},
{
“role”: “assistant”,
“content”: “$(echo $1)1”
}
],
“temperature”: 1,
“max_tokens”: 256,
“top_p”: 1,
“frequency_penalty”: 0,
“presence_penalty”: 0
}’

Try this instead

Use it to ask ChatGPT 4.0 to write it in your language. Python3 works well