Install and Configure the OpenAI SDK in a Node.js Project

Colby Fayock
InstructorColby Fayock
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated a year ago

OpenAI is an AI research lab that has developed a number of tools for researchers and developers including natural language processing, robotics, and reinforcement learning. By leveraging the OpenAI APIs, we can build faster and smarter, while bringing new and interesting capabilities to help enhance existing solutions and solve new challenges.

Let's get started building with AI using the OpenAI SDK by installing and configuring the SDK in a Node.js project. Here you'll learn how to install the openai npm package into an existing project, import the openai dependency, and configure the SDK with your OpenAI API Key to make authenticated requests to the OpenAI API.

Instructor: [0:00] The OpenAI platform provides a lot of capabilities for how we can tap into that AI power. Whether we're trying to create chat completions like ChatGPT or use DALL-E to generate images, we can get started in a Node application or environment by first running npm install openai.

[0:16] Inside of my terminal, I'm going to run npm install openai. Once it's installed, I want to then configure OpenAI inside of my Node.js environment, where here, I'm going to use a Next.js serverless function.

[0:28] If we look back inside of the documentation, under Authentication, we can see that the way that we can configure it is by first importing the configuration and the OpenAI API modules from openai.

[0:40] With that imported, we can create a new instance of configuration and the OpenAI API, where here, I'm just going to go ahead and copy all of these lines and paste it right in at the top of my serverless function, where as we can probably see, actually using the OpenAI ListEngines method probably needs to happen inside of the handler itself.

[0:58] Now, it appears that ListEngines may be deprecated, or something along those lines, but we're really only going to use it to test that this end-point and configuration works in the first place. What we can do is we can define a data point, where we're going to set it equal to

[1:13] Now there's only one thing left to do, and that's set up our OpenAI API key. I'm going to copy that value, and inside the root of my project, I'm going to create a new file, .env.local, where inside, I'm going to set that new API key.

[1:26] We're back inside of the OpenAI dashboard, I'm going to head up and click the Personal tab, or the organization that you're currently under, and I'm going to select View API Keys. We can see that I already have two created for my own use, but I'm going to go ahead and create a new secret key. I'm going to copy this value and paste it in as my OpenAI API key.

[1:44] As we can see, we're already referencing that variable, so we don't have to do anything more at that. As a personal preference, I like to set all of my configuration into an environment variable, so I'm going to also set this organization as an environment variable as well.

[1:56] I'm going to set this as OpenAI Organization, where back inside of my .env.local, I'm going to set that OpenAI Organization to that organization value. Now our serverless function is ready, and we should see a response of all the available engines inside of the data response.

[2:14] Once our environment is ready again, we can head to the browser and visit localhost:3000/api/image, or depending on where you're accessing your environment. Now we can see that we are successfully listing out all those different engines by connecting to the OpenAI API.

[2:29] In review, in order to install and configure OpenAI in a Node environment, whether that is a Next.js serverless function or a Node server, you want to first install OpenAI as a package, where after importing the configuration and OpenAI API, we can then create those new configuration and OpenAI API instances where then we have access to invoke the different methods of OpenAI to ultimately get our results.

~ 2 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today