Chat2API CLI is a fork of Niansuh/chat2api that adds a comprehensive command-line interface for managing ChatGPT access tokens, generating OpenAI-compatible API keys, and interacting with the Chat2API server. This project bridges the gap between ChatGPT's web interface and standard OpenAI API clients.
- Modern Terminal UI - Built with Rich library for beautiful, colorful output
- Interactive Commands - Intuitive slash commands with auto-completion
- Real-time Status - Live connection monitoring and server health checks
- Smart Prompts - Context-aware input with helpful suggestions
- Multi-Token Support - Store and manage multiple ChatGPT access tokens
- Token Labeling - Organize tokens with custom names
- Active Token Switching - Seamlessly switch between different accounts
- Secure Storage - Tokens stored locally in JSON format
- OpenAI-Compatible Keys - Generate
sk-xxxformat API keys - Token Mapping - Automatically map API keys to ChatGPT tokens
- External Integration - Use with any OpenAI-compatible client
- Key Management - List, test, and revoke API keys easily
- CLI-to-Server Sync - Automatically sync tokens and API keys to any remote server
- Middleware Integration - Transparent API key mapping at the server level
- Multi-Environment Support - Works with local and remote endpoints (any hosting provider)
- Endpoint Switching - Connect to different Chat2API servers on the fly
- Model Selection - Choose from GPT-3.5, GPT-4, GPT-4o, O1, and more
- Streaming Support - Toggle streaming mode for real-time responses
- Web Interface Launcher - Quick access to ChatGPT web UI
- Health Monitoring - Built-in server health checks and diagnostics
- ✅ Convert ChatGPT web access to OpenAI API format
- ✅ Support for all GPT models (3.5, 4, 4o, O1, etc.)
- ✅ Streaming and non-streaming responses
- ✅ Token rotation and management
- ✅ API key authentication
- ✅ CORS support for web applications
- ✅ Cloud deployment ready
/help- Show command reference/status- Display system status and configuration/clear- Clear conversation history/web- Open ChatGPT web interface/exit- Exit the application
/models- List available AI models/use <model>- Switch to a different model/stream- Toggle streaming mode
/token add- Add a new access token/token list- List all saved tokens/token use <name>- Switch to a specific token/token remove <name>- Remove a token
/endpoint <url>- Switch API endpoint/apikey generate- Generate OpenAI-compatible API key/apikey list- List all generated API keys/apikey test <name>- Test a specific API key/apikey remove <name>- Remove an API key
/reset- Reset all settings, tokens, and API keys
Download the latest release and run the executables:
-
Download the release
- Go to Releases
- Download
chat2api-CLI.zip - Extract to a folder
-
Start the server
app.exe
The server will start on
http://localhost:5005 -
Launch the CLI (in a new terminal)
chat.exe
-
Add your ChatGPT token
- In the CLI, type
/token add - Get your token from https://chatgpt.com/api/auth/session
- Paste it when prompted
- In the CLI, type
-
Start chatting!
- Type normally to chat with AI
- Use
/helpto see all commands
- Python 3.8 or higher
- pip package manager
- Clone the repository
git clone https://github.com/Kirazul/chat2api-cli.git
cd chat2api-cli- Install dependencies
pip install -r requirements.txt- Start the server
python app.py- Launch the CLI (in a new terminal)
python chat.py- Visit https://chatgpt.com/api/auth/session
- Copy the
accessTokenvalue - In the CLI, run
/token addand paste your token
- Add a ChatGPT access token first
- Run
/apikey generatein the CLI - Give your API key a name (e.g., "my-app")
- Copy the generated
sk-xxxkey - Use it with any OpenAI-compatible client
{
"models": [
{
"apiKey": "sk-your-generated-key",
"apiBase": "http://localhost:5005/v1",
"model": "gpt-4"
}
]
}import openai
openai.api_key = "sk-your-generated-key"
openai.api_base = "http://localhost:5005/v1"
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)curl http://localhost:5005/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-generated-key" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hello!"}]
}'# Server Configuration
HOST=0.0.0.0
PORT=5005
ENVIRONMENT=development
# Security
API_PREFIX=
AUTHORIZATION=
AUTH_KEY=
# ChatGPT Settings
CHATGPT_BASE_URL=https://chatgpt.com
PROXY_URL=
HISTORY_DISABLED=true
RANDOM_TOKEN=true
# Gateway
ENABLE_GATEWAY=falseThe CLI stores its configuration in:
config.json- General settings and active tokentokens.json- Stored ChatGPT access tokensapikeys.json- Generated API keys and mappings
The CLI sync feature works with any hosting provider. Simply deploy the server and configure the CLI endpoint:
Examples:
# Heroku
/endpoint https://your-app.herokuapp.com
# DigitalOcean / VPS
/endpoint https://your-domain.com
# Render
/endpoint https://your-app.onrender.com
# Railway
/endpoint https://your-app.up.railway.appThe CLI will automatically sync tokens and API keys to any configured endpoint.
# Build the image
docker build -t chat2api-cli .
# Run the container
docker run -p 5005:5005 -v $(pwd)/data:/app/data chat2api-cli- FastAPI Application - Main API server (
app.py) - Chat Service - ChatGPT interaction handler (
chatgpt/ChatService.py) - API Key Mapper - Middleware for key-to-token mapping (
middleware/apikey_mapper.py) - Sync API - CLI-to-server synchronization (
api/sync.py) - Token Management - Token rotation and validation (
chatgpt/authorization.py)
- Interactive Interface - Rich-based terminal UI (
chat.py) - Configuration Manager - Settings and storage (
Configclass) - API Client - HTTP client for server communication
- Command System - Slash command parser and executor
External App → API Key → Middleware → Token Mapping → ChatGPT Token → ChatGPT API
↓
CLI → Token Management → Server Sync → API Key Generation
POST /v1/chat/completions
GET /tokens
POST /tokens/upload
POST /tokens/clear
POST /tokens/error
GET /tokens/add/{token}
POST /admin/sync/tokens
POST /admin/sync/apikeys
GET /admin/sync/status
GET /admin/debug/apikey/{api_key}
GET /
GET /health
GET /test
- GPT-3.5:
gpt-3.5-turbo - GPT-4:
gpt-4,gpt-4-mobile,gpt-4-gizmo - GPT-4o:
gpt-4o,gpt-4o-mini,gpt-4o-canmore,gpt-4.5o - GPT-5:
gpt-5 - O1 Series:
o1-preview,o1-mini,o1 - Auto:
auto(automatic model selection)
# Check if server is running
curl http://localhost:5005/health
# Verify endpoint in CLI
/status
/endpoint http://localhost:5005# Test the API key
/apikey test <name>
# Check server logs for mapping issues
# Verify token is valid
/token list# Remove old token
/token remove <name>
# Add new token
/token addContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Original project: Niansuh/chat2api
- Built with FastAPI
- CLI powered by Rich
- Inspired by the OpenAI API
- 📫 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
