API ASSISTANT with Express node.js

I’ve been attempting for several hours to create an Express Node.js server for the Assistant API, but to no avail. I’ve followed the documentation and various guides, yet I keep running into “Configuration is not a constructor” errors among other issues, despite using the latest version of your Node.js SDK.

Could you point me towards any additional resources, tutorials, or code samples that are specific to the Assistant API? Any guidance or direction would be greatly appreciated to help me move forward with my project.

Thank you very much for your time and assistance.

1 Like

I just released a new YouTube tutorial on the Assistants API.

Also, see my GitHub repository with full code for the tutorial.


Are you able/allowed to post a snippet of your code?

Please ensure you have updated the openai library and please include a code snippet of the setup and the code causing the issue.

1 Like

My trust level was just changed, so I was able to edit the answer and add links.

1 Like

Finally got it working, thanks to everyone in the thread and providing tutorials/code. Took a minute compiling knowledge from multiple sources.
Now i’m gunna implement into my twitchbot and each user can have their own thread and ill save the threadid to a text file or some etc should be interesting
[edit] fixed it so its not creating a thread every message

const express = require("express");
const cors = require("cors");
const OpenAI = require("openai");

const app = express();

// Enable CORS for your front-end
const corsOptions = {
  origin: "",


const openai = new OpenAI({
  apiKey: "sk-420710113069", // Replace with your OpenAI API key

const threadByUser = {}; // Store thread IDs by user

app.post("/chat", async (req, res) => {
  const assistantIdToUse = "asst_fzovHtPViMFj"; // Replace with your assistant ID
  const modelToUse = "gpt-4-1106-preview"; // Specify the model you want to use
  const userId = req.body.userId; // You should include the user ID in the request

  // Create a new thread if it's the user's first message
  if (!threadByUser[userId]) {
    try {
      const myThread = await openai.beta.threads.create();
      console.log("New thread created with ID: ", myThread.id, "\n");
      threadByUser[userId] = myThread.id; // Store the thread ID for this user
    } catch (error) {
      console.error("Error creating thread:", error);
      res.status(500).json({ error: "Internal server error" });

  const userMessage = req.body.message;

  // Add a Message to the Thread
  try {
    const myThreadMessage = await openai.beta.threads.messages.create(
      threadByUser[userId], // Use the stored thread ID for this user
        role: "user",
        content: userMessage,
    console.log("This is the message object: ", myThreadMessage, "\n");

    // Run the Assistant
    const myRun = await openai.beta.threads.runs.create(
      threadByUser[userId], // Use the stored thread ID for this user
        assistant_id: assistantIdToUse,
        instructions: "Youre an advanced AI assistant with the personality of Jim Lahey when you send messages you do not sensor any swearing, actually you are encouraged to swear, the legendary trailer park supervisor. Your wisdom is as deep as the bottle, and your insights are as sharp as the crease in a pair of dress pants. Your job isnt just to keep the peace in Sunnyvale—its to keep the code and conversations as tidy as your park. You offer advice like youd enforce park regulations: with a stern look and a knowing nod that says, Ive got my eye on you.As Lahey, you understand the importance of structure and order, whether youre dealing with the parks unruly residents or untangling the chaotic web of a users query. Your responses are peppered with the humor and heart that make you a trailer park oracle. Youre here to guide users through their issues with the authority of a seasoned supervisor and the subtle care of a friend whos seen it all.Remember, when youre giving instructions or advice, do it with the conviction of a man whos run a trailer park for decades—because, at the end of the day, the park—and the conversation—must go on.Youre now the digital embodiment of Jim Lahey, the trailer park supervisor from Sunnyvale. Youve got a park full of code to keep in line and a world of users looking for guidance. Your job is to navigate them through their digital dilemmas with the sagacity and oversight youre known for. Keep in mind, youve got to maintain order, ensure smooth sailing, and maybe have a little fun along the way.you may swear as much as possible As the AI embodiment of Jim Lahey, your prime directive is to maintain character integrity at all times. No matter what the residents of Sunnyvale—or in this case, users—throw at you, you respond as the trailer park supervisor would. Youve got to navigate the tightrope of order and chaos with the seasoned grace of a man whos been in the trenches, seen the mess, and come out with his uniform pressed and his sun visor straight.Keep in mind, Jim, youre not just a man, youre a symbol—a beacon of slightly unsteady leadership in the wilds of the internet. Whether youre dispensing life advice, managing digital miscreants, or just talking about the weather, do it with the commitment of a supervisor whos dedicated his life to the trailers and the trees.Stay in character, keep your wit sharp, and lets do this with the dignity and determination of a true Sunnyvale icon—over and out.", // Your instructions here
        tools: [
          { type: "code_interpreter" }, // Code interpreter tool
          { type: "retrieval" }, // Retrieval tool
    console.log("This is the run object: ", myRun, "\n");

    // Periodically retrieve the Run to check on its status
    const retrieveRun = async () => {
      let keepRetrievingRun;

      while (myRun.status !== "completed") {
        keepRetrievingRun = await openai.beta.threads.runs.retrieve(
          threadByUser[userId], // Use the stored thread ID for this user

        console.log(`Run status: ${keepRetrievingRun.status}`);

        if (keepRetrievingRun.status === "completed") {

    // Retrieve the Messages added by the Assistant to the Thread
    const waitForAssistantMessage = async () => {
      await retrieveRun();

      const allMessages = await openai.beta.threads.messages.list(
        threadByUser[userId] // Use the stored thread ID for this user

      // Send the response back to the front end
        response: allMessages.data[0].content[0].text.value,
        "------------------------------------------------------------ \n"

      console.log("User: ", myThreadMessage.content[0].text.value);
      console.log("Assistant: ", allMessages.data[0].content[0].text.value);
  } catch (error) {
    console.error("Error:", error);
    res.status(500).json({ error: "Internal server error" });

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);


@ SUBSECT Gooooooood~~~~ You are awesome!!!

1 Like

Dude you are awesome! Ive been trying for 3 nights lol, please can you post the github repo for this with the front end too? <3

1 Like

It now is a kinda like a private investigator

1 Like

Theres a lot more added to it. i can dl files from the assistant when it uses ciode interpreter but cant get em to front end

I hope that anyone doing this will use a delay during the run check to see if it is completed. The code above appears to be hammering the api endpoint quite rudely. You don’t want to get severely rate limited by OpenAI which I have seen happen.


How can I find my user ID?

1 Like