No access: HTTP Authentication failed; no valid credentials available

Hi,

I get this error message:

       WebSocket connection to 'wss://api.openai.com/v1/engines/davinci-codex/completions/stream?prompt=test&max_tokens=2048&stop=%5Cn&temperature=0' failed: HTTP Authentication failed; no valid credentials available

with that code:

var socket;

function startStream() {
    var prompt = document.getElementById("prompt").value;
    var apiKey = "xxxxxxxxxxxxxx";

    socket = new WebSocket("wss://api.openai.com/v1/engines/davinci-codex/completions/stream?prompt=" + encodeURIComponent(prompt) + "&max_tokens=2048&stop=%5Cn&temperature=0");
    socket.onopen = function() {
        socket.send(apiKey);
    };
    socket.onmessage = function(event) {
        var response = JSON.parse(event.data);
        document.getElementById("response").innerHTML = response.text;
        saveResponse(response.text);
    };
    socket.onclose = function() {
        console.log("WebSocket closed");
    };
}

function saveResponse(response) {
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "save_response.php", true);
    xhttp.setRequestHeader
}

I checked the apiKey twice. The key is correct. Can you give me any hints what I am doing wrong, please?

Thank you, Bernd

Why are you using a websocket, and where is your authorization/bearer header ?

Simple example

Thank you. I just asked chatGPT for the best way to do a real time stream in JS and I got the code, but it does not work as expected. Thank you for the hint with the authorization/bearer header. I will look at this.