├── .env.example ├── .github ├── ISSUE_TEMPLATE │ ├── 1.bug_report.yml │ ├── 2.feature_request.yml │ └── config.yml ├── funding.yml └── workflows │ └── test.yml ├── .gitignore ├── .husky └── pre-commit ├── .npmrc ├── .prettierignore ├── .prettierrc.cjs ├── demos ├── demo-conversation.ts ├── demo-google-auth.ts ├── demo-on-progress.ts └── demo.ts ├── docs ├── .nojekyll ├── classes │ ├── AChatGPTAPI.md │ ├── ChatGPTAPI.md │ ├── ChatGPTAPIBrowser.md │ └── ChatGPTError.md ├── modules.md └── readme.md ├── license ├── media ├── demo.gif ├── demo.tape └── session-token.png ├── package.json ├── pnpm-lock.yaml ├── readme.md ├── src ├── abstract-chatgpt-api.ts ├── chatgpt-api-browser.ts ├── chatgpt-api.test.ts ├── chatgpt-api.ts ├── fetch-sse.ts ├── fetch.ts ├── index.ts ├── openai-auth.ts ├── stream-async-iterable.ts ├── types.ts └── utils.ts ├── third-party └── nopecha-chrome-extension │ ├── api.js │ ├── awscaptcha.js │ ├── background.js │ ├── content.js │ ├── font │ ├── plex-sans-bold.woff │ ├── plex-sans-bold.woff2 │ ├── plex-sans-regular.woff │ └── plex-sans-regular.woff2 │ ├── funcaptcha.js │ ├── funcaptcha_demo.js │ ├── funcaptcha_fast.js │ ├── funcaptcha_scrape.js │ ├── hcaptcha.js │ ├── hcaptcha_fast.js │ ├── hcaptcha_hook.js │ ├── hcaptcha_language.js │ ├── icon │ ├── 128.png │ ├── 128g.png │ ├── 16.png │ ├── 16g.png │ ├── 32.png │ ├── 32g.png │ ├── 48.png │ └── 48g.png │ ├── locate.js │ ├── manifest.json │ ├── popup.css │ ├── popup.html │ ├── popup.js │ ├── recaptcha.js │ ├── recaptcha_fast.js │ ├── recaptcha_speech.js │ ├── setup.js │ ├── textcaptcha.js │ ├── utils.js │ └── utils.mjs ├── tsconfig.json ├── tsup.config.ts └── typedoc.json /.env.example: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------------ 2 | # This is an example .env file. 3 | # 4 | # All of these environment vars must be defined either in your environment or in 5 | # a local .env file in order to run the demo for this project. 6 | # ------------------------------------------------------------------------------ 7 | 8 | # ----------------------------------------------------------------------------- 9 | # ChatGPT 10 | # ----------------------------------------------------------------------------- 11 | 12 | OPENAI_EMAIL= 13 | OPENAI_PASSWORD= 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/1.bug_report.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report 2 | description: Create a bug report 3 | labels: ['template: bug'] 4 | body: 5 | - type: markdown 6 | attributes: 7 | value: | 8 | Thanks for taking the time to fill out this bug report! 9 | - type: checkboxes 10 | attributes: 11 | label: Verify latest release 12 | description: '`chatgpt@latest` is the latest version of `chatgpt`. Some issues may already be fixed in the latest version, so please verify that your issue reproduces before opening a new issue.' 13 | options: 14 | - label: I verified that the issue exists in the latest `chatgpt` release 15 | required: true 16 | - type: checkboxes 17 | attributes: 18 | label: Verify webapp is working 19 | description: 'Verify that the [ChatGPT webapp](https://chat.openai.com/) is working for you locally using the same browser and account.' 20 | options: 21 | - label: I verify that the ChatGPT webapp is working properly for this account. 22 | required: true 23 | - type: textarea 24 | attributes: 25 | label: Environment details 26 | description: Please enter Node.js version, browser version, OS, and OS version. 27 | validations: 28 | required: true 29 | - type: textarea 30 | attributes: 31 | label: Describe the Bug 32 | description: A clear and concise description of what the bug is and as much detail as possible on how to reproduce it. 33 | validations: 34 | required: true 35 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/2.feature_request.yml: -------------------------------------------------------------------------------- 1 | name: Feature Request 2 | description: Create a feature rqeuest 3 | labels: ['template: enhancement'] 4 | body: 5 | - type: markdown 6 | attributes: 7 | value: | 8 | Thanks for taking the time to fill out this feature request. 9 | - type: textarea 10 | attributes: 11 | label: Describe the feature 12 | description: What would you like to see added / supported? 13 | validations: 14 | required: true 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Join the Discord 4 | url: https://discord.gg/HW3EFG6p 5 | about: Ask questions and discuss with other community members 6 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: [transitive-bullshit] 2 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | name: Test Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 19 14 | - 18 15 | - 16 16 | 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@v3 20 | 21 | - name: Install Node.js 22 | uses: actions/setup-node@v3 23 | with: 24 | node-version: ${{ matrix.node-version }} 25 | 26 | - name: Install pnpm 27 | uses: pnpm/action-setup@v2 28 | id: pnpm-install 29 | with: 30 | version: 7 31 | run_install: false 32 | 33 | - name: Get pnpm store directory 34 | id: pnpm-cache 35 | shell: bash 36 | run: | 37 | echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT 38 | 39 | - uses: actions/cache@v3 40 | name: Setup pnpm cache 41 | with: 42 | path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} 43 | key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} 44 | restore-keys: | 45 | ${{ runner.os }}-pnpm-store- 46 | 47 | - name: Install dependencies 48 | run: pnpm install --frozen-lockfile 49 | 50 | - name: Run test 51 | env: 52 | SESSION_TOKEN: 'fake-session-token-for-CI' 53 | run: pnpm run test 54 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | *.swp 4 | .idea 5 | 6 | # dependencies 7 | /node_modules 8 | /.pnp 9 | .pnp.js 10 | 11 | # testing 12 | /coverage 13 | 14 | # next.js 15 | /.next/ 16 | /out/ 17 | 18 | # production 19 | /build 20 | 21 | # misc 22 | .DS_Store 23 | *.pem 24 | 25 | # debug 26 | npm-debug.log* 27 | yarn-debug.log* 28 | yarn-error.log* 29 | .pnpm-debug.log* 30 | 31 | # local env files 32 | .env*.local 33 | 34 | # vercel 35 | .vercel 36 | 37 | # typescript 38 | *.tsbuildinfo 39 | next-env.d.ts 40 | 41 | # local env files 42 | .env 43 | .env.local 44 | .env.build 45 | .env.development.local 46 | .env.test.local 47 | .env.production.local 48 | 49 | # data dumps 50 | out/ 51 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | . "$(dirname -- "$0")/_/husky.sh" 3 | 4 | npm run pre-commit 5 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | enable-pre-post-scripts=true 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .snapshots/ 2 | build/ 3 | dist/ 4 | node_modules/ 5 | .next/ 6 | .vercel/ 7 | third-party/ -------------------------------------------------------------------------------- /.prettierrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: [require('@trivago/prettier-plugin-sort-imports')], 3 | singleQuote: true, 4 | jsxSingleQuote: true, 5 | semi: false, 6 | useTabs: false, 7 | tabWidth: 2, 8 | bracketSpacing: true, 9 | bracketSameLine: false, 10 | arrowParens: 'always', 11 | trailingComma: 'none', 12 | importOrder: ['^node:.*', '', '^[./]'], 13 | importOrderSeparation: true, 14 | importOrderSortSpecifiers: true, 15 | importOrderGroupNamespaceSpecifiers: true 16 | } 17 | -------------------------------------------------------------------------------- /demos/demo-conversation.ts: -------------------------------------------------------------------------------- 1 | import dotenv from 'dotenv-safe' 2 | import { oraPromise } from 'ora' 3 | 4 | import { ChatGPTAPIBrowser } from '../src' 5 | 6 | dotenv.config() 7 | 8 | /** 9 | * Demo CLI for testing conversation support. 10 | * 11 | * ``` 12 | * npx tsx demos/demo-conversation.ts 13 | * ``` 14 | */ 15 | async function main() { 16 | const email = process.env.OPENAI_EMAIL 17 | const password = process.env.OPENAI_PASSWORD 18 | 19 | const api = new ChatGPTAPIBrowser({ 20 | email, 21 | password, 22 | debug: false, 23 | minimize: true 24 | }) 25 | await api.initSession() 26 | 27 | const prompt = 'Write a poem about cats.' 28 | 29 | let res = await oraPromise(api.sendMessage(prompt), { 30 | text: prompt 31 | }) 32 | 33 | console.log('\n' + res.response + '\n') 34 | 35 | const prompt2 = 'Can you make it cuter and shorter?' 36 | 37 | res = await oraPromise( 38 | api.sendMessage(prompt2, { 39 | conversationId: res.conversationId, 40 | parentMessageId: res.messageId 41 | }), 42 | { 43 | text: prompt2 44 | } 45 | ) 46 | console.log('\n' + res.response + '\n') 47 | 48 | const prompt3 = 'Now write it in French.' 49 | 50 | res = await oraPromise( 51 | api.sendMessage(prompt3, { 52 | conversationId: res.conversationId, 53 | parentMessageId: res.messageId 54 | }), 55 | { 56 | text: prompt3 57 | } 58 | ) 59 | console.log('\n' + res.response + '\n') 60 | 61 | const prompt4 = 'What were we talking about again?' 62 | 63 | res = await oraPromise( 64 | api.sendMessage(prompt4, { 65 | conversationId: res.conversationId, 66 | parentMessageId: res.messageId 67 | }), 68 | { 69 | text: prompt4 70 | } 71 | ) 72 | console.log('\n' + res.response + '\n') 73 | 74 | // close the browser at the end 75 | await api.closeSession() 76 | } 77 | 78 | main().catch((err) => { 79 | console.error(err) 80 | process.exit(1) 81 | }) 82 | -------------------------------------------------------------------------------- /demos/demo-google-auth.ts: -------------------------------------------------------------------------------- 1 | import dotenv from 'dotenv-safe' 2 | import { oraPromise } from 'ora' 3 | 4 | import { ChatGPTAPIBrowser } from '../src' 5 | 6 | dotenv.config() 7 | 8 | /** 9 | * Demo CLI for testing basic functionality using Google auth. 10 | * 11 | * ``` 12 | * npx tsx demos/demo.ts 13 | * ``` 14 | */ 15 | async function main() { 16 | const email = process.env.OPENAI_EMAIL 17 | const password = process.env.OPENAI_PASSWORD 18 | 19 | const api = new ChatGPTAPIBrowser({ 20 | email, 21 | password, 22 | isGoogleLogin: true, 23 | debug: false, 24 | minimize: true 25 | }) 26 | await api.initSession() 27 | 28 | const prompt = 29 | 'Write a python version of bubble sort. Do not include example usage.' 30 | 31 | const res = await oraPromise(api.sendMessage(prompt), { 32 | text: prompt 33 | }) 34 | console.log(res.response) 35 | 36 | // close the browser at the end 37 | await api.closeSession() 38 | } 39 | 40 | main().catch((err) => { 41 | console.error(err) 42 | process.exit(1) 43 | }) 44 | -------------------------------------------------------------------------------- /demos/demo-on-progress.ts: -------------------------------------------------------------------------------- 1 | import dotenv from 'dotenv-safe' 2 | 3 | import { ChatGPTAPIBrowser } from '../src' 4 | 5 | dotenv.config() 6 | 7 | /** 8 | * Demo CLI for testing the `onProgress` handler. 9 | * 10 | * ``` 11 | * npx tsx demos/demo-on-progress.ts 12 | * ``` 13 | */ 14 | async function main() { 15 | const email = process.env.OPENAI_EMAIL 16 | const password = process.env.OPENAI_PASSWORD 17 | 18 | const api = new ChatGPTAPIBrowser({ 19 | email, 20 | password, 21 | debug: false, 22 | minimize: true 23 | }) 24 | await api.initSession() 25 | 26 | const prompt = 27 | 'Write a python version of bubble sort. Do not include example usage.' 28 | 29 | console.log(prompt) 30 | const res = await api.sendMessage(prompt, { 31 | onProgress: (partialResponse) => { 32 | console.log('p') 33 | console.log('progress', partialResponse?.response) 34 | } 35 | }) 36 | console.log(res.response) 37 | 38 | // close the browser at the end 39 | await api.closeSession() 40 | } 41 | 42 | main().catch((err) => { 43 | console.error(err) 44 | process.exit(1) 45 | }) 46 | -------------------------------------------------------------------------------- /demos/demo.ts: -------------------------------------------------------------------------------- 1 | import dotenv from 'dotenv-safe' 2 | import { oraPromise } from 'ora' 3 | 4 | import { ChatGPTAPIBrowser } from '../src' 5 | 6 | dotenv.config() 7 | 8 | /** 9 | * Demo CLI for testing basic functionality. 10 | * 11 | * ``` 12 | * npx tsx demos/demo.ts 13 | * ``` 14 | */ 15 | async function main() { 16 | const email = process.env.OPENAI_EMAIL 17 | const password = process.env.OPENAI_PASSWORD 18 | 19 | const api = new ChatGPTAPIBrowser({ 20 | email, 21 | password, 22 | debug: false, 23 | minimize: true 24 | }) 25 | await api.initSession() 26 | 27 | const prompt = 28 | 'Write a python version of bubble sort. Do not include example usage.' 29 | 30 | const res = await oraPromise(api.sendMessage(prompt), { 31 | text: prompt 32 | }) 33 | console.log(res.response) 34 | 35 | // close the browser at the end 36 | await api.closeSession() 37 | } 38 | 39 | main().catch((err) => { 40 | console.error(err) 41 | process.exit(1) 42 | }) 43 | -------------------------------------------------------------------------------- /docs/.nojekyll: -------------------------------------------------------------------------------- 1 | TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false. -------------------------------------------------------------------------------- /docs/classes/AChatGPTAPI.md: -------------------------------------------------------------------------------- 1 | [chatgpt](../readme.md) / [Exports](../modules.md) / AChatGPTAPI 2 | 3 | # Class: AChatGPTAPI 4 | 5 | ## Hierarchy 6 | 7 | - **`AChatGPTAPI`** 8 | 9 | ↳ [`ChatGPTAPI`](ChatGPTAPI.md) 10 | 11 | ↳ [`ChatGPTAPIBrowser`](ChatGPTAPIBrowser.md) 12 | 13 | ## Table of contents 14 | 15 | ### Constructors 16 | 17 | - [constructor](AChatGPTAPI.md#constructor) 18 | 19 | ### Methods 20 | 21 | - [closeSession](AChatGPTAPI.md#closesession) 22 | - [getIsAuthenticated](AChatGPTAPI.md#getisauthenticated) 23 | - [initSession](AChatGPTAPI.md#initsession) 24 | - [refreshSession](AChatGPTAPI.md#refreshsession) 25 | - [resetSession](AChatGPTAPI.md#resetsession) 26 | - [sendMessage](AChatGPTAPI.md#sendmessage) 27 | 28 | ## Constructors 29 | 30 | ### constructor 31 | 32 | • **new AChatGPTAPI**() 33 | 34 | ## Methods 35 | 36 | ### closeSession 37 | 38 | ▸ `Abstract` **closeSession**(): `Promise`<`void`\> 39 | 40 | Closes the active session. 41 | 42 | **`Throws`** 43 | 44 | An error if it fails. 45 | 46 | #### Returns 47 | 48 | `Promise`<`void`\> 49 | 50 | #### Defined in 51 | 52 | [src/abstract-chatgpt-api.ts:69](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/abstract-chatgpt-api.ts#L69) 53 | 54 | ___ 55 | 56 | ### getIsAuthenticated 57 | 58 | ▸ `Abstract` **getIsAuthenticated**(): `Promise`<`boolean`\> 59 | 60 | #### Returns 61 | 62 | `Promise`<`boolean`\> 63 | 64 | `true` if the client is authenticated with a valid session or `false` 65 | otherwise. 66 | 67 | #### Defined in 68 | 69 | [src/abstract-chatgpt-api.ts:39](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/abstract-chatgpt-api.ts#L39) 70 | 71 | ___ 72 | 73 | ### initSession 74 | 75 | ▸ `Abstract` **initSession**(): `Promise`<`void`\> 76 | 77 | Performs any async initialization work required to ensure that this API is 78 | properly authenticated. 79 | 80 | **`Throws`** 81 | 82 | An error if the session failed to initialize properly. 83 | 84 | #### Returns 85 | 86 | `Promise`<`void`\> 87 | 88 | #### Defined in 89 | 90 | [src/abstract-chatgpt-api.ts:10](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/abstract-chatgpt-api.ts#L10) 91 | 92 | ___ 93 | 94 | ### refreshSession 95 | 96 | ▸ `Abstract` **refreshSession**(): `Promise`<`any`\> 97 | 98 | Refreshes the current ChatGPT session. 99 | 100 | Useful for bypassing 403 errors when Cloudflare clearance tokens expire. 101 | 102 | **`Throws`** 103 | 104 | An error if it fails. 105 | 106 | #### Returns 107 | 108 | `Promise`<`any`\> 109 | 110 | Access credentials for the new session. 111 | 112 | #### Defined in 113 | 114 | [src/abstract-chatgpt-api.ts:49](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/abstract-chatgpt-api.ts#L49) 115 | 116 | ___ 117 | 118 | ### resetSession 119 | 120 | ▸ **resetSession**(): `Promise`<`any`\> 121 | 122 | Closes the current ChatGPT session and starts a new one. 123 | 124 | Useful for bypassing 401 errors when sessions expire. 125 | 126 | **`Throws`** 127 | 128 | An error if it fails. 129 | 130 | #### Returns 131 | 132 | `Promise`<`any`\> 133 | 134 | Access credentials for the new session. 135 | 136 | #### Defined in 137 | 138 | [src/abstract-chatgpt-api.ts:59](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/abstract-chatgpt-api.ts#L59) 139 | 140 | ___ 141 | 142 | ### sendMessage 143 | 144 | ▸ `Abstract` **sendMessage**(`message`, `opts?`): `Promise`<[`ChatResponse`](../modules.md#chatresponse)\> 145 | 146 | Sends a message to ChatGPT, waits for the response to resolve, and returns 147 | the response. 148 | 149 | If you want to receive a stream of partial responses, use `opts.onProgress`. 150 | 151 | #### Parameters 152 | 153 | | Name | Type | Description | 154 | | :------ | :------ | :------ | 155 | | `message` | `string` | The prompt message to send | 156 | | `opts?` | [`SendMessageOptions`](../modules.md#sendmessageoptions) | - | 157 | 158 | #### Returns 159 | 160 | `Promise`<[`ChatResponse`](../modules.md#chatresponse)\> 161 | 162 | The response from ChatGPT, including `conversationId`, `messageId`, and 163 | the `response` text. 164 | 165 | #### Defined in 166 | 167 | [src/abstract-chatgpt-api.ts:30](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/abstract-chatgpt-api.ts#L30) 168 | -------------------------------------------------------------------------------- /docs/classes/ChatGPTAPI.md: -------------------------------------------------------------------------------- 1 | [chatgpt](../readme.md) / [Exports](../modules.md) / ChatGPTAPI 2 | 3 | # Class: ChatGPTAPI 4 | 5 | ## Hierarchy 6 | 7 | - [`AChatGPTAPI`](AChatGPTAPI.md) 8 | 9 | ↳ **`ChatGPTAPI`** 10 | 11 | ## Table of contents 12 | 13 | ### Constructors 14 | 15 | - [constructor](ChatGPTAPI.md#constructor) 16 | 17 | ### Accessors 18 | 19 | - [clearanceToken](ChatGPTAPI.md#clearancetoken) 20 | - [sessionToken](ChatGPTAPI.md#sessiontoken) 21 | - [user](ChatGPTAPI.md#user) 22 | - [userAgent](ChatGPTAPI.md#useragent) 23 | 24 | ### Methods 25 | 26 | - [closeSession](ChatGPTAPI.md#closesession) 27 | - [getIsAuthenticated](ChatGPTAPI.md#getisauthenticated) 28 | - [initSession](ChatGPTAPI.md#initsession) 29 | - [refreshSession](ChatGPTAPI.md#refreshsession) 30 | - [resetSession](ChatGPTAPI.md#resetsession) 31 | - [sendMessage](ChatGPTAPI.md#sendmessage) 32 | - [sendModeration](ChatGPTAPI.md#sendmoderation) 33 | 34 | ## Constructors 35 | 36 | ### constructor 37 | 38 | • **new ChatGPTAPI**(`opts`) 39 | 40 | Creates a new client wrapper around the unofficial ChatGPT REST API. 41 | 42 | Note that your IP address and `userAgent` must match the same values that you used 43 | to obtain your `clearanceToken`. 44 | 45 | #### Parameters 46 | 47 | | Name | Type | Description | 48 | | :------ | :------ | :------ | 49 | | `opts` | `Object` | - | 50 | | `opts.accessToken?` | `string` | **`Default Value`** `undefined` * | 51 | | `opts.accessTokenTTL?` | `number` | **`Default Value`** 1 hour * | 52 | | `opts.apiBaseUrl?` | `string` | **`Default Value`** `'https://chat.openai.com/api'` * | 53 | | `opts.backendApiBaseUrl?` | `string` | **`Default Value`** `'https://chat.openai.com/backend-api'` * | 54 | | `opts.clearanceToken` | `string` | = **Required** Cloudflare `cf_clearance` cookie value (see readme for instructions) | 55 | | `opts.debug?` | `boolean` | **`Default Value`** `false` * | 56 | | `opts.headers?` | `Record`<`string`, `string`\> | **`Default Value`** `undefined` * | 57 | | `opts.markdown?` | `boolean` | **`Default Value`** `true` * | 58 | | `opts.sessionToken` | `string` | = **Required** OpenAI session token which can be found in a valid session's cookies (see readme for instructions) | 59 | | `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'` * | 60 | 61 | #### Overrides 62 | 63 | [AChatGPTAPI](AChatGPTAPI.md).[constructor](AChatGPTAPI.md#constructor) 64 | 65 | #### Defined in 66 | 67 | [src/chatgpt-api.ts:45](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L45) 68 | 69 | ## Accessors 70 | 71 | ### clearanceToken 72 | 73 | • `get` **clearanceToken**(): `string` 74 | 75 | Gets the current Cloudflare clearance token (`cf_clearance` cookie value). 76 | 77 | #### Returns 78 | 79 | `string` 80 | 81 | #### Defined in 82 | 83 | [src/chatgpt-api.ts:143](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L143) 84 | 85 | ___ 86 | 87 | ### sessionToken 88 | 89 | • `get` **sessionToken**(): `string` 90 | 91 | Gets the current session token. 92 | 93 | #### Returns 94 | 95 | `string` 96 | 97 | #### Defined in 98 | 99 | [src/chatgpt-api.ts:138](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L138) 100 | 101 | ___ 102 | 103 | ### user 104 | 105 | • `get` **user**(): [`User`](../modules.md#user) 106 | 107 | Gets the currently signed-in user, if authenticated, `null` otherwise. 108 | 109 | #### Returns 110 | 111 | [`User`](../modules.md#user) 112 | 113 | #### Defined in 114 | 115 | [src/chatgpt-api.ts:133](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L133) 116 | 117 | ___ 118 | 119 | ### userAgent 120 | 121 | • `get` **userAgent**(): `string` 122 | 123 | Gets the current user agent. 124 | 125 | #### Returns 126 | 127 | `string` 128 | 129 | #### Defined in 130 | 131 | [src/chatgpt-api.ts:148](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L148) 132 | 133 | ## Methods 134 | 135 | ### closeSession 136 | 137 | ▸ **closeSession**(): `Promise`<`void`\> 138 | 139 | Closes the active session. 140 | 141 | **`Throws`** 142 | 143 | An error if it fails. 144 | 145 | #### Returns 146 | 147 | `Promise`<`void`\> 148 | 149 | #### Overrides 150 | 151 | [AChatGPTAPI](AChatGPTAPI.md).[closeSession](AChatGPTAPI.md#closesession) 152 | 153 | #### Defined in 154 | 155 | [src/chatgpt-api.ts:470](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L470) 156 | 157 | ___ 158 | 159 | ### getIsAuthenticated 160 | 161 | ▸ **getIsAuthenticated**(): `Promise`<`boolean`\> 162 | 163 | #### Returns 164 | 165 | `Promise`<`boolean`\> 166 | 167 | `true` if the client has a valid acces token or `false` if refreshing 168 | the token fails. 169 | 170 | #### Overrides 171 | 172 | [AChatGPTAPI](AChatGPTAPI.md).[getIsAuthenticated](AChatGPTAPI.md#getisauthenticated) 173 | 174 | #### Defined in 175 | 176 | [src/chatgpt-api.ts:367](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L367) 177 | 178 | ___ 179 | 180 | ### initSession 181 | 182 | ▸ **initSession**(): `Promise`<`void`\> 183 | 184 | Refreshes the client's access token which will succeed only if the session 185 | is valid. 186 | 187 | #### Returns 188 | 189 | `Promise`<`void`\> 190 | 191 | #### Overrides 192 | 193 | [AChatGPTAPI](AChatGPTAPI.md).[initSession](AChatGPTAPI.md#initsession) 194 | 195 | #### Defined in 196 | 197 | [src/chatgpt-api.ts:156](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L156) 198 | 199 | ___ 200 | 201 | ### refreshSession 202 | 203 | ▸ **refreshSession**(): `Promise`<`string`\> 204 | 205 | Attempts to refresh the current access token using the ChatGPT 206 | `sessionToken` cookie. 207 | 208 | Access tokens will be cached for up to `accessTokenTTL` milliseconds to 209 | prevent refreshing access tokens too frequently. 210 | 211 | **`Throws`** 212 | 213 | An error if refreshing the access token fails. 214 | 215 | #### Returns 216 | 217 | `Promise`<`string`\> 218 | 219 | A valid access token 220 | 221 | #### Overrides 222 | 223 | [AChatGPTAPI](AChatGPTAPI.md).[refreshSession](AChatGPTAPI.md#refreshsession) 224 | 225 | #### Defined in 226 | 227 | [src/chatgpt-api.ts:386](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L386) 228 | 229 | ___ 230 | 231 | ### resetSession 232 | 233 | ▸ **resetSession**(): `Promise`<`any`\> 234 | 235 | Closes the current ChatGPT session and starts a new one. 236 | 237 | Useful for bypassing 401 errors when sessions expire. 238 | 239 | **`Throws`** 240 | 241 | An error if it fails. 242 | 243 | #### Returns 244 | 245 | `Promise`<`any`\> 246 | 247 | Access credentials for the new session. 248 | 249 | #### Inherited from 250 | 251 | [AChatGPTAPI](AChatGPTAPI.md).[resetSession](AChatGPTAPI.md#resetsession) 252 | 253 | #### Defined in 254 | 255 | [src/abstract-chatgpt-api.ts:59](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/abstract-chatgpt-api.ts#L59) 256 | 257 | ___ 258 | 259 | ### sendMessage 260 | 261 | ▸ **sendMessage**(`message`, `opts?`): `Promise`<[`ChatResponse`](../modules.md#chatresponse)\> 262 | 263 | Sends a message to ChatGPT, waits for the response to resolve, and returns 264 | the response. 265 | 266 | If you want to receive a stream of partial responses, use `opts.onProgress`. 267 | If you want to receive the full response, including message and conversation IDs, 268 | you can use `opts.onConversationResponse` or use the `ChatGPTAPI.getConversation` 269 | helper. 270 | 271 | #### Parameters 272 | 273 | | Name | Type | Description | 274 | | :------ | :------ | :------ | 275 | | `message` | `string` | The prompt message to send | 276 | | `opts` | [`SendMessageOptions`](../modules.md#sendmessageoptions) | - | 277 | 278 | #### Returns 279 | 280 | `Promise`<[`ChatResponse`](../modules.md#chatresponse)\> 281 | 282 | The response from ChatGPT 283 | 284 | #### Overrides 285 | 286 | [AChatGPTAPI](AChatGPTAPI.md).[sendMessage](AChatGPTAPI.md#sendmessage) 287 | 288 | #### Defined in 289 | 290 | [src/chatgpt-api.ts:180](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L180) 291 | 292 | ___ 293 | 294 | ### sendModeration 295 | 296 | ▸ **sendModeration**(`input`): `Promise`<[`ModerationsJSONResult`](../modules.md#moderationsjsonresult)\> 297 | 298 | #### Parameters 299 | 300 | | Name | Type | 301 | | :------ | :------ | 302 | | `input` | `string` | 303 | 304 | #### Returns 305 | 306 | `Promise`<[`ModerationsJSONResult`](../modules.md#moderationsjsonresult)\> 307 | 308 | #### Defined in 309 | 310 | [src/chatgpt-api.ts:324](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api.ts#L324) 311 | -------------------------------------------------------------------------------- /docs/classes/ChatGPTAPIBrowser.md: -------------------------------------------------------------------------------- 1 | [chatgpt](../readme.md) / [Exports](../modules.md) / ChatGPTAPIBrowser 2 | 3 | # Class: ChatGPTAPIBrowser 4 | 5 | ## Hierarchy 6 | 7 | - [`AChatGPTAPI`](AChatGPTAPI.md) 8 | 9 | ↳ **`ChatGPTAPIBrowser`** 10 | 11 | ## Table of contents 12 | 13 | ### Constructors 14 | 15 | - [constructor](ChatGPTAPIBrowser.md#constructor) 16 | 17 | ### Accessors 18 | 19 | - [isChatPage](ChatGPTAPIBrowser.md#ischatpage) 20 | 21 | ### Methods 22 | 23 | - [\_onRequest](ChatGPTAPIBrowser.md#_onrequest) 24 | - [\_onResponse](ChatGPTAPIBrowser.md#_onresponse) 25 | - [closeSession](ChatGPTAPIBrowser.md#closesession) 26 | - [getIsAuthenticated](ChatGPTAPIBrowser.md#getisauthenticated) 27 | - [initSession](ChatGPTAPIBrowser.md#initsession) 28 | - [refreshSession](ChatGPTAPIBrowser.md#refreshsession) 29 | - [resetSession](ChatGPTAPIBrowser.md#resetsession) 30 | - [resetThread](ChatGPTAPIBrowser.md#resetthread) 31 | - [sendMessage](ChatGPTAPIBrowser.md#sendmessage) 32 | 33 | ## Constructors 34 | 35 | ### constructor 36 | 37 | • **new ChatGPTAPIBrowser**(`opts`) 38 | 39 | Creates a new client for automating the ChatGPT webapp. 40 | 41 | #### Parameters 42 | 43 | | Name | Type | Description | 44 | | :------ | :------ | :------ | 45 | | `opts` | `Object` | - | 46 | | `opts.captchaToken?` | `string` | **`Default Value`** `undefined` * | 47 | | `opts.debug?` | `boolean` | **`Default Value`** `false` * | 48 | | `opts.email` | `string` | - | 49 | | `opts.executablePath?` | `string` | **`Default Value`** `undefined` * | 50 | | `opts.isGoogleLogin?` | `boolean` | **`Default Value`** `false` * | 51 | | `opts.isMicrosoftLogin?` | `boolean` | **`Default Value`** `false` * | 52 | | `opts.markdown?` | `boolean` | **`Default Value`** `true` * | 53 | | `opts.minimize?` | `boolean` | **`Default Value`** `true` * | 54 | | `opts.nopechaKey?` | `string` | **`Default Value`** `undefined` * | 55 | | `opts.password` | `string` | - | 56 | | `opts.proxyServer?` | `string` | **`Default Value`** `undefined` * | 57 | 58 | #### Overrides 59 | 60 | [AChatGPTAPI](AChatGPTAPI.md).[constructor](AChatGPTAPI.md#constructor) 61 | 62 | #### Defined in 63 | 64 | [src/chatgpt-api-browser.ts:44](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L44) 65 | 66 | ## Accessors 67 | 68 | ### isChatPage 69 | 70 | • `get` **isChatPage**(): `boolean` 71 | 72 | #### Returns 73 | 74 | `boolean` 75 | 76 | #### Defined in 77 | 78 | [src/chatgpt-api-browser.ts:741](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L741) 79 | 80 | ## Methods 81 | 82 | ### \_onRequest 83 | 84 | ▸ **_onRequest**(`request`): `void` 85 | 86 | #### Parameters 87 | 88 | | Name | Type | 89 | | :------ | :------ | 90 | | `request` | `HTTPRequest` | 91 | 92 | #### Returns 93 | 94 | `void` 95 | 96 | #### Defined in 97 | 98 | [src/chatgpt-api-browser.ts:249](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L249) 99 | 100 | ___ 101 | 102 | ### \_onResponse 103 | 104 | ▸ **_onResponse**(`response`): `Promise`<`void`\> 105 | 106 | #### Parameters 107 | 108 | | Name | Type | 109 | | :------ | :------ | 110 | | `response` | `HTTPResponse` | 111 | 112 | #### Returns 113 | 114 | `Promise`<`void`\> 115 | 116 | #### Defined in 117 | 118 | [src/chatgpt-api-browser.ts:286](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L286) 119 | 120 | ___ 121 | 122 | ### closeSession 123 | 124 | ▸ **closeSession**(): `Promise`<`void`\> 125 | 126 | Closes the active session. 127 | 128 | **`Throws`** 129 | 130 | An error if it fails. 131 | 132 | #### Returns 133 | 134 | `Promise`<`void`\> 135 | 136 | #### Overrides 137 | 138 | [AChatGPTAPI](AChatGPTAPI.md).[closeSession](AChatGPTAPI.md#closesession) 139 | 140 | #### Defined in 141 | 142 | [src/chatgpt-api-browser.ts:687](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L687) 143 | 144 | ___ 145 | 146 | ### getIsAuthenticated 147 | 148 | ▸ **getIsAuthenticated**(): `Promise`<`boolean`\> 149 | 150 | #### Returns 151 | 152 | `Promise`<`boolean`\> 153 | 154 | `true` if the client is authenticated with a valid session or `false` 155 | otherwise. 156 | 157 | #### Overrides 158 | 159 | [AChatGPTAPI](AChatGPTAPI.md).[getIsAuthenticated](AChatGPTAPI.md#getisauthenticated) 160 | 161 | #### Defined in 162 | 163 | [src/chatgpt-api-browser.ts:422](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L422) 164 | 165 | ___ 166 | 167 | ### initSession 168 | 169 | ▸ **initSession**(): `Promise`<`void`\> 170 | 171 | Performs any async initialization work required to ensure that this API is 172 | properly authenticated. 173 | 174 | **`Throws`** 175 | 176 | An error if the session failed to initialize properly. 177 | 178 | #### Returns 179 | 180 | `Promise`<`void`\> 181 | 182 | #### Overrides 183 | 184 | [AChatGPTAPI](AChatGPTAPI.md).[initSession](AChatGPTAPI.md#initsession) 185 | 186 | #### Defined in 187 | 188 | [src/chatgpt-api-browser.ts:119](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L119) 189 | 190 | ___ 191 | 192 | ### refreshSession 193 | 194 | ▸ **refreshSession**(): `Promise`<`void`\> 195 | 196 | Attempts to handle 403 errors by refreshing the page. 197 | 198 | #### Returns 199 | 200 | `Promise`<`void`\> 201 | 202 | #### Overrides 203 | 204 | [AChatGPTAPI](AChatGPTAPI.md).[refreshSession](AChatGPTAPI.md#refreshsession) 205 | 206 | #### Defined in 207 | 208 | [src/chatgpt-api-browser.ts:364](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L364) 209 | 210 | ___ 211 | 212 | ### resetSession 213 | 214 | ▸ **resetSession**(): `Promise`<`void`\> 215 | 216 | Attempts to handle 401 errors by re-authenticating. 217 | 218 | #### Returns 219 | 220 | `Promise`<`void`\> 221 | 222 | #### Overrides 223 | 224 | [AChatGPTAPI](AChatGPTAPI.md).[resetSession](AChatGPTAPI.md#resetsession) 225 | 226 | #### Defined in 227 | 228 | [src/chatgpt-api-browser.ts:345](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L345) 229 | 230 | ___ 231 | 232 | ### resetThread 233 | 234 | ▸ **resetThread**(): `Promise`<`void`\> 235 | 236 | #### Returns 237 | 238 | `Promise`<`void`\> 239 | 240 | #### Defined in 241 | 242 | [src/chatgpt-api-browser.ts:679](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L679) 243 | 244 | ___ 245 | 246 | ### sendMessage 247 | 248 | ▸ **sendMessage**(`message`, `opts?`): `Promise`<[`ChatResponse`](../modules.md#chatresponse)\> 249 | 250 | Sends a message to ChatGPT, waits for the response to resolve, and returns 251 | the response. 252 | 253 | If you want to receive a stream of partial responses, use `opts.onProgress`. 254 | 255 | #### Parameters 256 | 257 | | Name | Type | Description | 258 | | :------ | :------ | :------ | 259 | | `message` | `string` | The prompt message to send | 260 | | `opts` | [`SendMessageOptions`](../modules.md#sendmessageoptions) | - | 261 | 262 | #### Returns 263 | 264 | `Promise`<[`ChatResponse`](../modules.md#chatresponse)\> 265 | 266 | The response from ChatGPT, including `conversationId`, `messageId`, and 267 | the `response` text. 268 | 269 | #### Overrides 270 | 271 | [AChatGPTAPI](AChatGPTAPI.md).[sendMessage](AChatGPTAPI.md#sendmessage) 272 | 273 | #### Defined in 274 | 275 | [src/chatgpt-api-browser.ts:499](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/chatgpt-api-browser.ts#L499) 276 | -------------------------------------------------------------------------------- /docs/classes/ChatGPTError.md: -------------------------------------------------------------------------------- 1 | [chatgpt](../readme.md) / [Exports](../modules.md) / ChatGPTError 2 | 3 | # Class: ChatGPTError 4 | 5 | ## Hierarchy 6 | 7 | - `Error` 8 | 9 | ↳ **`ChatGPTError`** 10 | 11 | ## Table of contents 12 | 13 | ### Constructors 14 | 15 | - [constructor](ChatGPTError.md#constructor) 16 | 17 | ### Properties 18 | 19 | - [originalError](ChatGPTError.md#originalerror) 20 | - [response](ChatGPTError.md#response) 21 | - [statusCode](ChatGPTError.md#statuscode) 22 | - [statusText](ChatGPTError.md#statustext) 23 | 24 | ## Constructors 25 | 26 | ### constructor 27 | 28 | • **new ChatGPTError**(`message?`) 29 | 30 | #### Parameters 31 | 32 | | Name | Type | 33 | | :------ | :------ | 34 | | `message?` | `string` | 35 | 36 | #### Inherited from 37 | 38 | Error.constructor 39 | 40 | #### Defined in 41 | 42 | node_modules/.pnpm/typescript@4.9.3/node_modules/typescript/lib/lib.es5.d.ts:1059 43 | 44 | • **new ChatGPTError**(`message?`, `options?`) 45 | 46 | #### Parameters 47 | 48 | | Name | Type | 49 | | :------ | :------ | 50 | | `message?` | `string` | 51 | | `options?` | `ErrorOptions` | 52 | 53 | #### Inherited from 54 | 55 | Error.constructor 56 | 57 | #### Defined in 58 | 59 | node_modules/.pnpm/typescript@4.9.3/node_modules/typescript/lib/lib.es2022.error.d.ts:30 60 | 61 | ## Properties 62 | 63 | ### originalError 64 | 65 | • `Optional` **originalError**: `Error` 66 | 67 | #### Defined in 68 | 69 | [src/types.ts:297](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/types.ts#L297) 70 | 71 | ___ 72 | 73 | ### response 74 | 75 | • `Optional` **response**: `Response` 76 | 77 | #### Defined in 78 | 79 | [src/types.ts:296](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/types.ts#L296) 80 | 81 | ___ 82 | 83 | ### statusCode 84 | 85 | • `Optional` **statusCode**: `number` 86 | 87 | #### Defined in 88 | 89 | [src/types.ts:294](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/types.ts#L294) 90 | 91 | ___ 92 | 93 | ### statusText 94 | 95 | • `Optional` **statusText**: `string` 96 | 97 | #### Defined in 98 | 99 | [src/types.ts:295](https://github.com/transitive-bullshit/chatgpt-api/blob/4a0f780/src/types.ts#L295) 100 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Travis Fischer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /media/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/n-dev27/chatgpt-api/d0a256045639dfc2007fbfe621906245c4367837/media/demo.gif -------------------------------------------------------------------------------- /media/demo.tape: -------------------------------------------------------------------------------- 1 | # VHS documentation 2 | # 3 | # @see https://github.com/charmbracelet/vhs 4 | # 5 | # ``` 6 | # vhs < media/demo.tape 7 | # ``` 8 | # 9 | # Output: 10 | # Output .gif Create a GIF output at the given 11 | # Output .mp4 Create an MP4 output at the given 12 | # Output .webm Create a WebM output at the given 13 | # 14 | # Require: 15 | # Require Ensure a program is on the $PATH to proceed 16 | # 17 | # Settings: 18 | # Set FontSize Set the font size of the terminal 19 | # Set FontFamily Set the font family of the terminal 20 | # Set Height Set the height of the terminal 21 | # Set Width Set the width of the terminal 22 | # Set LetterSpacing Set the font letter spacing (tracking) 23 | # Set LineHeight Set the font line height 24 | # Set LoopOffset % Set the starting frame offset for the GIF loop 25 | # Set Theme Set the theme of the terminal 26 | # Set Padding Set the padding of the terminal 27 | # Set Framerate Set the framerate of the recording 28 | # Set PlaybackSpeed Set the playback speed of the recording 29 | # 30 | # Sleep: 31 | # Sleep