This library may or may not still work, ChatGPT has antibot measures in place and managing the session requires browsers.
To use ChatGPT in your application, run:
npm i unofficial-chatgpt-api# or `yarn add unofficial-chatgpt-api`# or `pnpm i unofficial-chatgpt-api`
// dual tokenconst { ChatGPTClient } = require('unofficial-chatgpt-api');const gpt = new ChatGPTClient({ clearanceToken: '<--BOT_CLEARANCE_TOKEN-->', sessionToken0: '<--SESSION_TOKEN_0-->', sessionToken1: '<--SESSION_TOKEN_1-->',});
// single tokenconst { ChatGPTClient } = require('unofficial-chatgpt-api');const gpt = new ChatGPTClient({ clearanceToken: '<--BOT_CLEARANCE_TOKEN-->', sessionToken0: '<--SESSION_TOKEN-->',});
Library manages auto-refreshing tokens.
Dual tokens
Some accounts require dual tokens:
Visit: https://chat.openai.com/chat
Open devtools in chrome: visit the application tab
Click on cookies in the left under storage, click on the chat.openai.com domain
Copy the value of the first cookie and paste it in the client (Name: __Secure-next-auth.session-token.0
)
Copy the value of the second cookie and paste it in the client (Name: __Secure-next-auth.session-token.1
)
Single token
Some accounts require single token:
Visit: https://chat.openai.com/chat
Open devtools in chrome: visit the application tab
Click on cookies in the left under storage, click on the chat.openai.com domain
Copy the value of the first cookie and paste it in the client (Name: __Secure-next-auth.session-token
)
const convo = await gpt.startConversation();const m1 = await convo.chat('show me some javascript code:');const m2 = await convo.chat('who created you?');console.log(m2.message.content.parts);
const convo1 = await gpt.startConversation();const convo2 = await gpt.startConversation();const m1 = await convo1.chat('show me some javascript code:');const m2 = await convo2.chat('who created you?');console.log(m2.message.content.parts);
convo.reset();