This Node.js application integrates with the ChatGPT API to provide a conversational interface powered by AI. It accepts user queries through a POST request to the /api
endpoint, sends the query to the ChatGPT API, and returns the response from the AI model.
- Node.js installed on your machine
- An API key from RapidAPI for accessing the ChatGPT API
.env
file configured with your RapidAPI key (refer to.env.example
for the required format)
- Clone this repository.
- Navigate to the project directory in your terminal.
- Run
npm install
to install dependencies.
- Obtain an API key from RapidAPI by signing up for the ChatGPT API.
- Create a
.env
file in the project root. - Add your RapidAPI key to the
.env
file in the following format:
API_KEY=your_rapidapi_key
- Start the server by running
npm start
. - Send a POST request to
http://localhost:{port}/api
with a JSON object containing the query in the request body. Example:{ "query": "What is the capital of France?" }
The server forwards user queries to the ChatGPT API, retrieves the response, and sends it back to the client.
- POST /api:
- Description: Receives a user query in the request body and returns the response generated by the ChatGPT AI model.
- express:
- Description: Web server framework for Node.js
- node-fetch:
- Description: A lightweight module for making HTTP requests
- dotenv:
- Description: Loads environment variables from a
.env
file intoprocess.env
- Description: Loads environment variables from a
- PORT:
- Description: Port number for the server (default is 3000)
- API_KEY:
- Description: RapidAPI key for accessing the ChatGPT API
- Description: If there's an error during the API call or processing, the server returns a 500 Internal Server Error response.
- Ensure proper error handling and security measures are implemented before deploying this application to production.
- Refer to the official RapidAPI documentation for more details on the ChatGPT API.