| `opts.sessionToken` | `string` | = **Required** OpenAI session token which can be found in a valid session's cookies (see readme for instructions) |
| `opts.sessionToken` | `string` | = **Required** OpenAI session token which can be found in a valid session's cookies (see readme for instructions) |
| `opts.userAgent?` | `string` | **`Default Value`**`'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'`* |
| `opts.userAgent?` | `string` | **`Default Value`**`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'`* |
> As of December 11, 2022 ~2pm CST, OpenAI has enabled additional Cloudflare restrictions that are currently preventing us from refreshing access tokens. This is affecting _all_ ChatGPT API wrappers at the moment, including the Python ones. See [this issue](https://github.com/transitive-bullshit/chatgpt-api/issues/96) for an ongoing discussion until I release a viable workaround.
Yesterday, OpenAI added additional Cloudflare protections that make it more difficult to access the unofficial API.
The demos have been updated to use Puppeteer to log in to ChatGPT and extract the Cloudflare `cf_clearance` cookie and OpenAI session token. 🔥
To use the updated version, make sure you're using the latest version of this package and Node.js >= 18. Then update your code to use the examples below, paying special attention to the sections on [Authentication](#authentication) and [Restrictions](#restrictions).
We're working hard in [this issue](https://github.com/transitive-bullshit/chatgpt-api/issues/96) to improve this process. Keep in mind that this package will be updated to use the official API as soon as it's released. 💪
@@ -127,39 +141,50 @@ See the [auto-generated docs](./docs/classes/ChatGPTAPI.md) for more info on met
...
@@ -127,39 +141,50 @@ See the [auto-generated docs](./docs/classes/ChatGPTAPI.md) for more info on met
### Demos
### Demos
A [basic demo](./src/demo.ts) is included for testing purposes:
To run the included demos:
1. clone repo
2. install node deps
3. set `EMAIL` and `PASSWORD` in .env
A [basic demo](./demos/demo.ts) is included for testing purposes:
```bash
```bash
# 1. clone repo
# 2. install node deps
# 3. set `SESSION_TOKEN` in .env
# 4. run:
npx tsx src/demo.ts
npx tsx src/demo.ts
```
```
A [conversation demo](./src/demo-conversation.ts) is also included:
A [conversation demo](./demos/demo-conversation.ts) is also included:
```bash
```bash
# 1. clone repo
# 2. install node deps
# 3. set `SESSION_TOKEN` in .env
# 4. run:
npx tsx src/demo-conversation.ts
npx tsx src/demo-conversation.ts
```
```
### Session Tokens
### Authentication
#### Restrictions
**This package requires a valid session token from ChatGPT to access it's unofficial REST API.**
**Please read carefully**
To get a session token:
- You must use `node >= 18`. I'm using `v19.2.0` in my testing, but for some reason, all `fetch` requests using Node.js `v16` and `v17` fail at the moment (these use `undici` under the hood, whereas Node.js v18 and above use a built-in `fetch` based on `undici`).
- Cloudflare `cf_clearance`**tokens expire after 2 hours**, so right now we recommend that you refresh your `cf_clearance` token every hour or so.
- Your `user-agent` and `IP address`**must match** from the real browser window you're logged in with to the one you're using for `ChatGPTAPI`.
- This means that you currently can't log in with your laptop and then run the bot on a server or proxy somewhere.
- Cloudflare will still sometimes ask you to complete a CAPTCHA, so you may need to keep an eye on it and manually resolve the CAPTCHA. Automated CAPTCHA bypass is coming soon.
- You should not be using this account while the bot is using it, because that browser window may refresh one of your tokens and invalidate the bot's session.
<details>
<summary>Getting tokens manually</summary>
To get a session token manually:
1. Go to https://chat.openai.com/chat and log in or sign up.
1. Go to https://chat.openai.com/chat and log in or sign up.
2. Open dev tools.
2. Open dev tools.
3. Open `Application` > `Cookies`.
3. Open `Application` > `Cookies`.


4. Copy the value for `__Secure-next-auth.session-token` and save it to your environment.
4. Copy the value for `__Secure-next-auth.session-token` and save it to your environment. This will be your `sessionToken`.
5. Copy the value for `cf_clearance` and save it to your environment. This will be your `clearanceToken`.
If you want to run the built-in demo, store this value as `SESSION_TOKEN` in a local `.env` file.
</details>
> **Note**
> **Note**
> This package will switch to using the official API once it's released.
> This package will switch to using the official API once it's released.
...
@@ -197,6 +222,7 @@ All of these awesome projects are built using the `chatgpt` package. 🤯
...
@@ -197,6 +222,7 @@ All of these awesome projects are built using the `chatgpt` package. 🤯
-[QQ Bot (plugin for KiviBot)](https://github.com/KiviBotLab/kivibot-plugin-chatgpt)
-[QQ Bot (plugin for KiviBot)](https://github.com/KiviBotLab/kivibot-plugin-chatgpt)
If you create a cool integration, feel free to open a PR and add it to the list.
If you create a cool integration, feel free to open a PR and add it to the list.
...
@@ -215,11 +243,8 @@ If you create a cool integration, feel free to open a PR and add it to the list.
...
@@ -215,11 +243,8 @@ If you create a cool integration, feel free to open a PR and add it to the list.
This package is ESM-only. It supports:
This package is ESM-only. It supports:
- Node.js >= 16.8
- Node.js >= 18
- If you need Node.js 14 support, use [`v1.4.0`](https://github.com/transitive-bullshit/chatgpt-api/releases/tag/v1.4.0)
- Node.js 17, 16, and 14 were supported in earlier versions, but OpenAI's Cloudflare update caused a bug with `undici` on v17 and v16 that we need to debug. So for now, use `node >= 18`
- Edge runtimes like CF workers and Vercel edge functions
- Modern browsers
- Mainly meant for chrome extensions where your code is protected to a degree
- We recommend against using `chatgpt` from client-side browser code because it would expose your private session token
- We recommend against using `chatgpt` from client-side browser code because it would expose your private session token
- If you want to build a website using `chatgpt`, we recommend using it only from your backend API
- If you want to build a website using `chatgpt`, we recommend using it only from your backend API