Video Tutorial
Watch first if you want to follow along with integrating ChatGPT into a Node.js app using the OpenAI Node library.
Project Overview
Node.js + ChatGPT (OpenAI Node library): In this tutorial, you integrate ChatGPT into a Node.js application using the OpenAI Node library so your app can send a prompt and print the model response.
This pattern can be used to add a conversational interface to a Node.js app for customer support, personal assistant features, translation, content recommendations, e-commerce help, and educational assistance.
- Time: 10 to 20 minutes
- Skill level: Beginner
- What you will build: A simple Node.js script that calls the OpenAI API and logs a text response
Parts List
From ShillehTek
- None required for this software-only tutorial
External
- Node.js + npm (to run the app and install packages)
- OpenAI Node package:
openai(installed via npm) - OpenAI API key - generate here: https://platform.openai.com/account/api-keys
- OpenAI libraries documentation: https://platform.openai.com/docs/libraries
Note: Keep your API key secret. If someone else gains access, they can use your account and potentially run up charges.
Step-by-Step Guide
Step 1 - Install the OpenAI package
Goal: Add the OpenAI Node library to your project so you can call the API from Node.js.
What to do: Run the installation in your command line from your project folder.
Code:
npm install openai
Expected result: The openai package is installed and available to import/require in your Node.js code.
Step 2 - Create an OpenAI API key
Goal: Get an API key so your Node.js app can authenticate to the OpenAI API.
What to do: Generate your key here: https://platform.openai.com/account/api-keys, then store it securely.
Expected result: You have an API key ready to use in your configuration, and you understand it should be kept private.
Step 3 - Run the example code to send a prompt
Goal: Send a prompt to the model and print the response text in your console.
What to do: Create a Node.js file (for example, index.js), paste the code below, replace <your api key> with your key, then run the script with Node.
Code:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: <your api key>,
});
const openai = new OpenAIApi(configuration);
async function generateText() {
const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: "How to create a simple node app",
max_tokens: 4000,
});
console.log(completion.data.choices[0].text);
}
generateText()
Expected result: Your terminal prints the completion text returned by the API.
Step 4 - Adjust parameters and review the library docs
Goal: Understand what controls the output length and where to find more options.
What to do: In this example, createCompletion is used and max_tokens is set high to reduce truncation. Smaller values can truncate the response, and larger values can be more intensive and are subject to limits.
For more parameters, model choices, and other methods, use the official documentation: https://platform.openai.com/docs/libraries.
Expected result: You can modify the prompt and token limit confidently and know where to find the rest of the available options.
Conclusion
You set up a Node.js app that uses the OpenAI Node library to send a prompt to ChatGPT (via the OpenAI API) and print the returned text completion. This is a simple foundation you can extend into real conversational features inside your own Node.js applications.
Want parts and tools for your next build? Shop at ShillehTek.com. If you want help integrating AI into an IoT workflow or building a custom automation for your product, check out our IoT consulting services.


