You didn't provide an API key (i.e. Authorization: Bearer YOUR_KEY) ... but I did

I’m using typescript in Excel.
I call this:

    // Set the HTTP headers
    const headers: Headers = new Headers();
    headers.append("Content-Type", "application/json");
    headers.append("Authorization", "Bearer "+ apiKey);
    headers.forEach(console.log);

and they correctly echo as

  • “Bearer sk-j… etc”

  • “authorization”

  • {}

  • “application/json”

  • “content-type”

  • {}

Then I add the rest:
endpoint is https://api.OpenAI.com/v1/chat/completions

body is
{“model”:“gpt-3.5-turbo”,“messages”:[{“role”:“user”,“content”:“What is your version number”}],“temperature”:0.5}

As a response I get {“error”:{“message”:"You didn’t provide an API key…etc

    // Create the messages collection
    var messages:object[] = new Array();
    if (systemPrompt.length > 0) { 
        messages.push({ role: "system", content: systemPrompt });
    }
    messages.push({ role: "user", content: userPrompt})

    // Set the HTTP body. Leave max_tokens at default 4096
    const body: string = JSON.stringify({
    model: model,
    messages: messages,
    temperature: temperature
    });

    console.log(body); //  shows correctly

    // Send the HTTP request
    const response: Response = await fetch(endpoint, {
    method: "POST",
    headers: headers,
    body: body,
    });

When I use the same calls in VBA using the XMLHttp methods, the key works fine.

And when I use the same key with the GPT-3 endpoint and its body structure, it works fine,

I’d better answer my question. After some experimenting, I find that this works:
method: “POST”,
headers: {
‘content-type’: ‘application/json;charset=UTF-8’,
‘authorization’: 'Bearer ’ + apiKey
},

I don’t know why. I got this from Using fetch with TypeScript

Capitalization maybe? Authentication vs authentication

Not recommend but when you hard code the api instead of pulling it from the .env it works