Before dive into the article, you have to make sure that you have the following prerequisities.
- Open API Key: You need an API key from OpenAI to access ChatGPT. If you haven't already, sign up for one on the OpenAI platform.
- Node.js and npm or yarn: Ensure that Node.js is installed on your machine, along with npm (Node Package Manager) or yarn for managing dependencies.
Steps to generate OpenAI API Key
Follow these stepts to generate an OpenAI API Key. This is the key that you will use to authenticate and make API requests to OpenAI services.
- 1. Sign Up or Log In to the OpenAI account
If you do not have an OpenAI account, go to the OpenAI website and sign up for an account by providing your email address.
If you already have an account, log in to your OpenAI account using the email and password you provided during registration.
- 2. Navigate to the API section
Once you logged into your OpenAI account, you have to navigate to the API section to generate the key.
- 3. Create a new API Key
Go to the profile section and click View API keys option.
There you have the option to create new API key. Click that option.
After you click the option, you will be prompted to provide a name for the key. It is optional. Then your secret key will be displayed.
- 4. Store your API Key securely
Once your API key is generated, it is essential to store it securely. Be cautious with your API key and avoid sharing it publicly or committing it to version control systems like Git in your code repositories. Use environment variables or other secure methods to protect your API key.
Now that you have your API key, you can use it in your applications to make authenticated requests to OpenAI's services, such as ChatGPT.
- 2. Navigate inside the created directory
- Initialize a Node.js project inside your project directory by running the following command. This command will guide you through setting up the project. Press
Enterto accept the default values or provide your own information.
- 3. Install Dependencies
Depending on your project requirements, you may need to install certain dependencies. Most commonly you will need a library to make HTTP requests to the ChatGPT API. You can choose between libraries like
fetch API. Here, we'll use
axios with npm:
npm install axios
axios with yarn:
yarn add axios
Step 2: Write API Request
Let us build a sample application that sends a request to the OpenAI API and display the response.
You can use any text editor or an integrated development environment (IDE) to write the code. I have used Visual Studio Code IDE for writing the code.
I have stored the API Key in the environment variable file called ".env" and imported the API Key from that file to the code.
Provide the actual OpenAI API key in the
Step 3: Run the program
Save the changes and run the program using the following command.
The program will make a request to the ChatGPT API with the provided conversation, and you will see the response printed to the console.
Author of Get Basic Idea / Undergraduate at University of Moratuwa.