Integrating OpenAI LLM model to Your Nodejs Application

Posted By :Mohd Ubaish |25th January 2024

Integrating ChatGPT-3.5 Turbo into Your Node.js Application: A Step-by-Step Guide


Step 1: Set Up Your OpenAI Account:

Before you start integrating ChatGPT-3.5 Turbo, make sure you have an OpenAI account. Visit the OpenAI website ( to create an account and obtain API keys.


Step 2: Install the OpenAI Node.js Package:

To interact with ChatGPT-3.5 Turbo from your Node.js application, you'll need the OpenAI Node.js package. Install it using npm:


npm intall openai

Step 3: Get your api key:

Retrieve your OpenAI API key from your account dashboard on the OpenAI website. You'll need this key to authenticate your requests to the ChatGPT API.


Step 4: Set Up Your Node.js Project:

Create a new Node.js project or open an existing one. In your project directory, create a new file (e.g., app.js) to write your application code.


Step 5: Configure OpenAI Package:

Configure the OpenAI package in your Node.js project by entering your API key. Add the following code snippet into your app.js file:


const openai = require('openai');
const apiKey = 'YOUR_OPENAI_API_KEY';

const chatGPT = new openai.ChatCompletion({

Replace 'YOUR_OPENAI_API_KEY' with your actual API key.


Step 6: Create a Function to Interact with ChatGPT:

Define a function to interact with the ChatGPT-3.5 Turbo API. This function will take a user's message as input and return ChatGPT's response. Add the following code to your app.js file:


const generateChatResponse = (userMessage) => {
  const response = await chatGPT.create({
    model: 'gpt-3.5-turbo',
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: userMessage },

  return response.choices[0].message.content;


Step 7: Implement ChatGPT in Your Application Logic:

Now, you can use the generateChatResponse function in your application logic. For example, in an Express.js route, you can handle user messages and send ChatGPT responses:

const express = require('express');
const app = express();

app.use(express.json());'/chat', async (req, res) => {
  const userMessage = req.body.message;
  const chatGPTResponse = await generateChatResponse(userMessage);

  res.json({ response: chatGPTResponse });

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


In this example, a POST request to the '/chat' endpoint expects a JSON object with a 'message' property. The server will respond with the ChatGPT-generated message.

About Author

Mohd Ubaish

Mohd Ubaish is a highly skilled Backend Developer with expertise in a wide range of technologies, including React, Node.js, Express.js, MongoDB, and JavaScript.With a deep understanding of both front-end and back-end development, he is currently dedicated to the development of TripCongo Web Discovery.He is committed to creating a user-friendly experience on the frontend while ensuring the seamless functionality of the backend. By staying up-to-date with the latest industry trends and advancements, he strives to provide innovative solutions and optimize the performance of the application.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us