Ssjs api get call to assistant on ssjs sfmc

I’m trying to make an API call to OpenAI, specifically to its assistant. I’ve already created the assistant and have the ID and updated API key.

I have an example code in SSJS that works well. I’m doing this through a CloudPage to test the SSJS.

My need is to take information from a Data Extension (DE), pass it to the OpenAI API in an array, and then take the response and save it in the same DE but in a column for that.

But I’m getting errors, and there’s no response from the API. I tried the same thing on Postman and the call is successful, with the retrieval of data.

Could someone help me with this? I’ve copied all the code for better understanding

This is the example that works ok. And based on this i start to make my code.

  Example 
    <script runat="server">
    Platform.Load("Core", "1"); 
    try {
        var interest = "dogs";  // Define your interest
        var contentType = 'application/json';
        var headerNames = ["Authorization"];
        var headerValues = ["Bearer "APIKEY"];  // Use a secure method to handle your API key

        var jsonBody = '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Write a marketing email about ' + interest + '."}], "temperature": 0.5, "max_tokens": 100}';
        var requestUrl = "https://api.openai.com/v1/chat/completions";

        var request = HTTP.Post(requestUrl, contentType, jsonBody, headerNames, headerValues);
        if (request.StatusCode != 200) {
            Write("HTTP Error: " + request.StatusCode + " - " + request.Response);
        } else {
            var respo = request.Response.toString();
            var json = Platform.Function.ParseJSON(respo);
            var content = json.choices[0].message.content;
            Write(content);  // Prints the content of the email
        }
    } catch (error) {
        Write(Stringify(error));
    }
</script>

So. Based on this code, i make my own. I separated this on Blocks so the Block1 its ok.
retrieve the Data extension and keep the data on and array.

<script runat="server">
    Platform.Load("Core", "1");
    try {
        var myDE = DataExtension.Init("DE EXTERNALKEY*");

        //
        var data = myDE.Rows.Retrieve();  
        var messageData = [];

        // 
        for(var i = 0; i < data.length; i++) {
            if(data[i].MessageData !== undefined) {
                messageData.push(data[i].MessageData);
            }
        }

        // 
        messageData.sort(function(a, b) {
            if (a > b) return -1;
            if (a < b) return 1;
            return 0;
        });
        
        for(var j = 0; j < messageData.length; j++) {
            Write(messageData[j] + "<br>");
        }

    } catch (error) {
        Write("<br>Error: " + Stringify(error));
    }
</script>

Ok this script, works well and i can see the data on the cloudpage for testing.
But on the 2cond Block. is the problem.

Openai tell me to retrieve a assistant i need this

   <Example Request>
    curl https://api.openai.com/v1/assistants/asst_abc123 \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer $OPENAI_API_KEY" \
      -H "OpenAI-Beta: assistants=v2"

So, i take this and make my request on ssjs, like this:

    <script runat="server">
      Platform.Load("Core", "1");
  
      var requestUrl = "https://api.openai.com/v1/assistants/asst_abc123";
      var contentType = "application/json"; //
      var authorizationToken = "Bearer YOUR_API_KEY";

      var request = new Script.Util.HttpRequest(requestUrl);
      request.method = "GET"; 
      request.contentType = contentType;
      request.setHeader("Authorization", authorizationToken);
      request.setHeader("OpenAI-Beta", "assistants=v2");
      
      var response = request.send();

      if (response.StatusCode != 200) {
        Write("HTTP Error: " + response.StatusCode + " - " + response.Response);
      } else {
        var responseContent = response.Response.toString();
        Write("Response: " + responseContent);
      }
    </script>

What im doing wrong? , because the example request is ok always. But this assistant api doesnt work. Someone can guide me, helpme or give me some sort of aidea of whats wrong.

Thnks.