A powerful command-line interface for interacting with ChatGPT directly from your terminal. This tool allows you to have seamless conversations with ChatGPT without leaving your development environment.
- Browser-Based Authentication: Securely authenticate with ChatGPT using your existing browser session
- Session Management: Capture and store session data for future use without frequent re-logins
- Terminal Chat Interface: Chat with ChatGPT directly from your terminal
- Markdown Rendering: Beautiful rendering of ChatGPT responses with full Markdown support
- Persistent Sessions: Maintains your login state between sessions
- Flexible Authentication: Works with different combinations of authentication cookies
- Node.js (v14 or higher)
- npm or yarn
- Google Chrome browser
-
Clone this repository:
git clone https://github.com/perception30/chatgpt-cli.git cd chatgpt-cli
-
Run the install script:
./install.sh
-
Install dependencies:
npm install
-
Build the project:
npm run build
-
Link the CLI globally:
npm link
For the best experience, start Chrome with debugging enabled before using the CLI:
./start-chrome.sh
This allows the CLI to connect to your existing Chrome instance rather than launching a new one each time.
Before using the CLI, you need to authenticate with ChatGPT:
chatgpt login
This will connect to your existing Chrome browser (if available) or launch a new Chrome instance. It will open a new tab where you can log in to your ChatGPT account.
Important: You must complete the entire login process, including any CAPTCHA or verification challenges. The CLI will wait for up to 5 minutes for you to complete this process.
Start a chat session with ChatGPT:
chatgpt chat
Or simply:
chatgpt
Type your messages and press Enter to send. ChatGPT's responses will be displayed in the terminal with Markdown formatting.
Type exit
to end the chat session.
# Check if your session is valid
chatgpt check
# Save session data to current directory (useful for debugging)
chatgpt save-local
# Diagnose authentication issues
chatgpt diagnose
The CLI connects to your existing Chrome browser (if it's running with remote debugging enabled) or launches a new Chrome instance. It uses the Chrome DevTools Protocol to:
- Open a new tab for authentication with ChatGPT
- Capture authentication cookies and session data
- Use these credentials to interact with the ChatGPT API
This approach preserves your login state and avoids having to log in multiple times. The session data is stored locally in the ~/.chatgpt-cli
directory and also in the current directory.
To use an existing Chrome instance, start Chrome with the --remote-debugging-port
flag:
./start-chrome.sh
This script will:
- Check if Chrome is already running with the debugging port
- If not, start Chrome with the debugging port enabled
- Provide clear instructions on next steps
The tool saves two important files:
session.json
: Contains cookies and user agent informationheaders.json
: Contains HTTP headers needed for API requests
These files are saved both in the ~/.chatgpt-cli
directory and in the current directory.
The tool is designed to work with different combinations of authentication cookies:
- Primary method: Capture the
__Secure-next-auth.session-token
cookie - Alternative method: Use the
_puid
andcf_clearance
cookies
If you encounter login problems:
- Make sure you complete the entire login process until you see the ChatGPT interface
- Ensure you're using the correct OpenAI account credentials
- Complete any verification challenges that appear
- If the login times out, try running
chatgpt login
again - Use the
./start-chrome.sh
script to start Chrome with debugging enabled - Check the
session.json
file to see which cookies were captured
Request Header Or Cookie Too Large
If you see this error:
- Clear your browser cookies or use a fresh Chrome profile
- Run
./start-chrome.sh
to start Chrome with debugging enabled - Run
chatgpt login
to authenticate again
Cloudflare Challenge Detected
If you see a Cloudflare challenge error (403):
- Run
chatgpt login
again to refresh your session - Make sure you have the Cloudflare clearance cookie (
cf_clearance
)
Authentication Failed
If you see an authentication error (401 or 403), run chatgpt login
again to refresh your session.
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Commit your changes:
git commit -m 'Add some amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
# Install dependencies
npm install
# Build the project
npm run build
# Run in development mode
npm run dev
This project is licensed under the ISC License - see the LICENSE file for details.
Khaled - @perception30
Project Link: https://github.com/perception30/chatgpt-cli