ChatGPT API
Intro


Updates
Details Details
If you run into any issues, we do have a pretty active Discord with a bunch of ChatGPT hackers from the Node.js & Python communities.
Lastly, please consider starring this repo and
to help support the project.
Thanks && cheers, Travis
CLI
To run the CLI, you'll need an OpenAI API key:
By default, the response is streamed to stdout, the results are stored in a local config file, and every invocation starts a new conversation. You can use
-c
to continue the previous conversation and --no-stream
to disable streaming.Install
Make sure you're using
node >= 18
so fetch
is available (or node >= 14
if you install a fetch polyfill).Usage
To use this module from Node.js, you need to pick between two methods:
ChatGPTAPI
- Uses the gpt-3.5-turbo-0301
model with the official OpenAI chat completions API (official, robust approach, but it's not free). You can override the model, completion params, and system message to fully customize your assistant.ChatGPTUnofficialProxyAPI
- Uses an unofficial proxy server to access ChatGPT's backend API in a way that circumvents Cloudflare (uses the real ChatGPT and is pretty lightweight, but relies on a third-party server and is rate-limited)Both approaches have very similar APIs, so it should be simple to swap between them.
Note: We strongly recommend using
ChatGPTAPI
since it uses the officially supported API from OpenAI. We may remove support for ChatGPTUnofficialProxyAPI
in a future release.Usage - ChatGPTAPI
Sign up for an OpenAI API key and store it in your environment.
You can override the default
model
(gpt-3.5-turbo-0301
) and any OpenAI chat completion params using completionParams
:If you want to track the conversation, you'll need to pass the
parentMessageId
like this:You can add streaming via the
onProgress
handler:You can add a timeout using the
timeoutMs
option:If you want to see more info about what's actually being sent to OpenAI's chat completions API, set the
debug: true
option in the ChatGPTAPI
constructor:We default to a basic
systemMessage
. You can override this in either the ChatGPTAPI
constructor or sendMessage
:Note that we automatically handle appending the previous messages to the prompt and attempt to optimize for the available tokens (which defaults to
4096
).Details
Usage - ChatGPTUnofficialProxyAPI
The API for
ChatGPTUnofficialProxyAPI
is almost exactly the same. You just need to provide a ChatGPT accessToken
instead of an OpenAI API key.See demos/demo-reverse-proxy for a full example:
ChatGPTUnofficialProxyAPI
messages also contain a conversationid
in addition to parentMessageId
, since the ChatGPT webapp can't reference messages across different accounts & conversations.Reverse Proxy
You can override the reverse proxy by passing
apiReverseProxyUrl
:Known reverse proxies run by community members include:
Reverse Proxy URL | Author | Rate Limits | Last Checked |
https://bypass.churchless.tech/api/conversation | 5 req / 10 seconds by IP | 3/24/2023 | |
https://api.pawan.krd/backend-api/conversation | 50 req / 15 seconds (~3 r/s) | 3/23/2023 |
Note: info on how the reverse proxies work is not being published at this time in order to prevent OpenAI from disabling access.
Access Token
To use
ChatGPTUnofficialProxyAPI
, you'll need an OpenAI access token from the ChatGPT webapp. To do this, you can use any of the following methods which take an email
and password
and return an access token:- Node.js libs
- Python libs
These libraries work with email + password accounts (e.g., they do not support accounts where you auth via Microsoft / Google).
Alternatively, you can manually get an
accessToken
by logging in to the ChatGPT webapp and then opening https://chat.openai.com/api/auth/session
, which will return a JSON object containing your accessToken
string.Access tokens last for days.
Note: using a reverse proxy will expose your access token to a third-party. There shouldn't be any adverse effects possible from this, but please consider the risks before using this method.
Docs
See the auto-generated docs for more info on methods and parameters.
Demos
Most of the demos use
ChatGPTAPI
. It should be pretty easy to convert them to use ChatGPTUnofficialProxyAPI
if you'd rather use that approach. The only thing that needs to change is how you initialize the api with an accessToken
instead of an apiKey
.To run the included demos:
- clone repo
- install node deps
- set
OPENAI_API_KEY
in .env
A basic demo is included for testing purposes:
The on progress demo uses the optional
onProgress
parameter to sendMessage
to receive intermediary results as ChatGPT is "typing".A persistence demo shows how to store messages in Redis for persistence:
Any keyv adaptor is supported for persistence, and there are overrides if you'd like to use a different way of storing / retrieving messages.
Note that persisting message is required for remembering the context of previous conversations beyond the scope of the current Node.js process, since by default, we only store messages in memory. Here's an external demo of using a completely custom database solution to persist messages.
Note: Persistence is handled automatically when using
ChatGPTUnofficialProxyAPI
because it is connecting indirectly to ChatGPT.Projects
All of these awesome projects are built using the
chatgpt
package.
- Twitter Bot powered by ChatGPT
- Mention @ChatGPTBot on Twitter with your prompt to try it out
- ChatGPT API Server - API server for this package with support for multiple OpenAI accounts, proxies, and load-balancing requests between accounts.
- ChatGPT Prompts - A collection of 140+ of the best ChatGPT prompts from the community.
- Telegram Bot #3 (group privacy mode, ID-based auth)
- Telegram Bot #4 (queue system, ID-based chat thread)
- Telegram Bot #5 (group privacy mode, ID-based chat thread)
- WhatsApp Bot #1 (DALL-E + Whisper support )

- WhatsApp Bot #3 (multi-user support)
- WhatsApp Bot #4 (schedule periodic messages)
- WhatsApp Bot #5 (RaspberryPi + ngrok + Twilio)
- WhatsApp Bot #6 (Session and chat history storage with MongoStore)
- Slack Bot #2 (with queueing mechanism)
- Slack Bot #4 ( Serverless AWS Lambda )
- Clippy the Saleforce chatbot ClippyJS joke bot
- ai-assistant Chat assistant
- Julius GPT : Generate and publish your content from the command line with the help of AI (GPT).
If you create a cool integration, feel free to open a PR and add it to the list.
Compatibility
- This package is ESM-only.
- This package supports
node >= 14
.
- This module assumes that
fetch
is installed.
- If you want to build a website using
chatgpt
, we recommend using it only from your backend API
Credits
- Huge thanks to @waylaidwanderer, @abacaj, @wong2, @simon300000, @RomanHotsiy, @ElijahPepe, and all the other contributors
- The original browser version was inspired by this Go module by Daniel Gross
License
MIT © Travis Fischer
If you found this project interesting, please consider sponsoring me or