DEV Community

Joshua Akan-Etuk
Joshua Akan-Etuk

Posted on • Originally published at joshuaakanetuk.com on

How To Use Open AI API with Node.js

ChatGPT has become one of the most popular web applications of all-time. The firm, OpenAI, that created ChatGPT actually has an API that allow access to the same models that ChatGPT uses and more. This article will demonstrate quickstart usage for OpenAI's API for Node.js.

This requires a credit card, but you are provided $5 credits before you are change

  1. First you'll need to create an API account via OpenAI's platform. It's free to create.

  2. Create an API key and save it securely somewhere as it is required to make requests to OpenAI.

  3. Create a Nodejs project. Run npm init if you like.

  4. Install the openai package:

npm install openai@^4.0.0
Enter fullscreen mode Exit fullscreen mode

or

yarn install openai@^4.0.0
Enter fullscreen mode Exit fullscreen mode
  1. Create a index.js and paste the following code (sourced from OpenAI docs):
import OpenAI from "openai";

const openai = new OpenAI({
  organization: 'YOUR_ORG_ID',
});
Enter fullscreen mode Exit fullscreen mode

You are now ready to make other requests!

Top comments (0)