This is a proxy server that provides OpenAI-compatible API interface for Z.ai GLM-4.5 model. It allows you to interact with Z.ai's GLM-4.5 model using standard OpenAI API format, supporting both streaming and non-streaming responses.
Note: This project is forked from OpenAI-Compatible-API-Proxy-for-Z with additional development
- π OpenAI API Compatible: Fully compatible with OpenAI API format, no client code modification needed
- π Streaming Response Support: Supports real-time streaming output for better user experience
- π Authentication: Supports API key validation for service security
- π οΈ Flexible Configuration: Flexible configuration through environment variables
- π³ Docker Support: Provides Docker image for easy deployment
- π CORS Support: Supports cross-origin requests for frontend integration
- π Thinking Process Display: Intelligently processes and displays model's thinking process
- π Real-time Monitoring Dashboard: Provides web dashboard showing real-time API forwarding status and statistics
- Go 1.23 or higher
- Z.ai access token
-
Clone Repository
git clone https://github.com/kisworo/ztoapi.git cd ztoapi -
Configure Environment Variables
cp config.env .env.local # Edit .env.local file and set your ZAI_TOKEN -
Start Service
# Use startup script (recommended) ./start.sh # Or run directly go run main.go
-
Test Service
curl http://localhost:9090/v1/models
-
Access API Documentation
After starting the service, you can access the complete API documentation through your browser:
http://localhost:9090/docsThe API documentation provides:
- Detailed API endpoint descriptions
- Request parameters and response formats
- Usage examples in multiple programming languages (Python, cURL, JavaScript)
- Error handling explanations
-
Access Dashboard
After starting the service, you can access the real-time monitoring dashboard through your browser:
http://localhost:9090/dashboardThe dashboard provides:
- Real-time API request statistics (total requests, successful requests, failed requests, average response time)
- Detailed information of the latest 100 requests (time, method, path, status code, duration, client IP)
- Data automatically refreshes every 5 seconds
-
Build Image
docker build -t zto-api . -
Run Container
docker run -p 9090:9090 \ -e ZAI_TOKEN=your_z_ai_token \ -e DEFAULT_KEY=your_api_key \ zto-api
-
Fork this repository to your GitHub account
-
Create a new Web Service on Render:
- Connect your GitHub repository
- Select Docker as environment
- Set the following environment variables:
ZAI_TOKEN: Z.ai access token (optional, will use anonymous token if not provided)DEFAULT_KEY: Client API key (optional, default: sk-your-key)MODEL_NAME: Display model name (optional, default: GLM-4.5)PORT: Service listening port (Render will set automatically)
-
After deployment, use the URL provided by Render as the base_url for OpenAI API
This project supports configuration through environment variables, providing flexible deployment and runtime options.
macOS/Linux:
./start.shWindows:
start.batmacOS/Linux:
export ZAI_TOKEN="your_z_ai_token_here"
export DEFAULT_KEY="sk-your-custom-key"
export PORT="9090"
go run main.goWindows:
set ZAI_TOKEN=your_z_ai_token_here
set DEFAULT_KEY=sk-your-custom-key
set PORT=9090
go run main.godocker run -p 9090:9090 \
-e ZAI_TOKEN=your_z_ai_token_here \
-e DEFAULT_KEY=sk-your-custom-key \
-e PORT=9090 \
zto-apiNo required configuration. All configurations have reasonable default values.
| Variable | Description | Default | Example |
|---|---|---|---|
ZAI_TOKEN |
Z.ai access token | Empty (uses anonymous token) | eyJhbGciOiJFUzI1NiIs... |
| Variable | Description | Default | Example |
|---|---|---|---|
DEFAULT_KEY |
Client API key | sk-your-key |
sk-my-api-key |
MODEL_NAME |
Display model name | GLM-4.5 |
GLM-4.5-Pro |
PORT |
Service listening port | 9090 |
9000 |
DEBUG_MODE |
Debug mode switch | true |
false |
DEFAULT_STREAM |
Default streaming response | true |
false |
DASHBOARD_ENABLED |
Dashboard feature switch | true |
false |
| Variable | Description | Default | Example |
|---|---|---|---|
UPSTREAM_URL |
Upstream API address | https://chat.z.ai/api/chat/completions |
Custom URL |
.env.local- Local environment configuration (recommended).env- Environment configurationconfig.env- Configuration template
# Copy configuration file
cp config.env .env.local
# Edit configuration file
nano .env.local- Login to Z.ai
- Open browser developer tools (F12)
- Switch to Network tab
- Send a message
- Find the Bearer token in the
Authorizationheader of requests
- After logging into Z.ai, check Cookies in developer tools
- Find cookies containing authentication information
This project supports automatic anonymous token acquisition without manual configuration. When the ANON_TOKEN_ENABLED constant is true, the system will automatically acquire different anonymous tokens for each conversation, avoiding shared memory.
# .env.local
ZAI_TOKEN=eyJhbGciOiJFUzI1NiIs...
DEFAULT_KEY=sk-my-secret-key
MODEL_NAME=GLM-4.5-Pro
PORT=9000
DEBUG_MODE=false# .env.production
ZAI_TOKEN=your_production_token
DEFAULT_KEY=sk-production-key
MODEL_NAME=GLM-4.5
PORT=9090
DEBUG_MODE=false
DEFAULT_STREAM=true# .env.development
ZAI_TOKEN=your_dev_token
DEFAULT_KEY=sk-dev-key
MODEL_NAME=GLM-4.5-Dev
PORT=8080
DEBUG_MODE=true
DEFAULT_STREAM=true
DASHBOARD_ENABLED=trueThis project provides a web dashboard for real-time monitoring of API forwarding status and statistics.
- Real-time display of API request statistics (total requests, successful requests, failed requests, average response time)
- Shows detailed information of the latest 100 requests (time, method, path, status code, duration, client IP)
- Data automatically refreshes every 5 seconds
- Responsive design, supports access from various devices
After starting the service, access through your browser:
http://localhost:9090/dashboard
Control Dashboard feature on/off through the DASHBOARD_ENABLED environment variable:
# Enable Dashboard (default)
DASHBOARD_ENABLED=true
# Disable Dashboard
DASHBOARD_ENABLED=false- Development Debugging: Real-time view of API request status for debugging and troubleshooting
- Performance Monitoring: Monitor API response time and success rate to evaluate system performance
- Security Audit: View request sources and frequency to detect abnormal access patterns
After modifying environment variables, restart the service for configuration to take effect:
# Stop current service
Ctrl+C
# Restart
./start.sh- Token Security: Do not commit real Z.ai tokens to code repository
- Configuration Files: Recommend adding
.env.localto.gitignore - Permission Settings: Ensure startup scripts have execute permissions (
chmod +x start.sh) - Port Conflicts: Ensure configured port is not occupied by other services
- Anonymous Token: When using anonymous tokens, each conversation has independent context
- Thinking Process: Project automatically handles model's thinking process, display mode can be adjusted through
THINK_TAGS_MODEconstant
import openai
# Configure client
client = openai.OpenAI(
api_key="your-api-key", # corresponds to DEFAULT_KEY
base_url="http://localhost:9090/v1"
)
# Non-streaming request
response = client.chat.completions.create(
model="GLM-4.5",
messages=[{"role": "user", "content": "Hello, please introduce yourself"}]
)
print(response.choices[0].message.content)
# Streaming request
response = client.chat.completions.create(
model="GLM-4.5",
messages=[{"role": "user", "content": "Please write a poem about spring"}],
stream=True
)
for chunk in response:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")# Non-streaming request
curl -X POST http://localhost:9090/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "GLM-4.5",
"messages": [{"role": "user", "content": "Hello"}],
"stream": false
}'
# Streaming request
curl -X POST http://localhost:9090/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "GLM-4.5",
"messages": [{"role": "user", "content": "Hello"}],
"stream": true
}'const fetch = require('node-fetch');
async function chatWithGLM(message, stream = false) {
const response = await fetch('http://localhost:9090/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer your-api-key'
},
body: JSON.stringify({
model: 'GLM-4.5',
messages: [{ role: 'user', content: message }],
stream: stream
})
});
if (stream) {
// Handle streaming response
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') {
console.log('\nStreaming response completed');
return;
}
try {
const parsed = JSON.parse(data);
const content = parsed.choices[0]?.delta?.content;
if (content) {
process.stdout.write(content);
}
} catch (e) {
// Ignore parsing errors
}
}
}
}
} else {
// Handle non-streaming response
const data = await response.json();
console.log(data.choices[0].message.content);
}
}
// Usage example
chatWithGLM('Hello, please introduce JavaScript', false);-
Connection Failed
- Check if service is running normally:
curl http://localhost:9090/v1/models - Access API documentation:
http://localhost:9090/docs - Confirm port configuration is correct
- Check if service is running normally:
-
Authentication Failed
- Check
DEFAULT_KEYenvironment variable setting - Confirm
Authorizationheader format is correct in requests
- Check
-
Invalid Z.ai Token
- Check
ZAI_TOKENenvironment variable setting - Confirm token has not expired
- Check
-
Thinking Process Display Issues
- Check if
DEBUG_MODEis enabled - View service logs for detailed information
- Check if
-
Port Occupied: Modify
PORTenvironment variable or stop service occupying the port -
Insufficient Permissions: Ensure startup scripts have execute permissions
-
Configuration Not Taking Effect: Restart service or check configuration file syntax
-
Streaming Response Issues: Confirm
DEFAULT_STREAMsetting is correct, check if client supports streaming response
Enable debug mode to get detailed logs:
export DEBUG_MODE=true
go run main.goIf you encounter network connection issues, try:
- Check firewall settings
- Confirm
UPSTREAM_URLis accessible - Test network connectivity:
curl https://chat.z.ai/api/chat/completions
- Reduce Log Output: Set
DEBUG_MODE=false - Adjust Timeout: Modify
http.Clienttimeout settings in code - Use Reverse Proxy: Recommend using Nginx or similar reverse proxy in production
Welcome to submit Issues and Pull Requests! Please ensure:
- Code follows Go coding style
- Run tests before submitting
- Update related documentation
- Follow project code structure and naming conventions
- Fork this repository
- Create feature branch:
git checkout -b feature/new-feature - Commit changes:
git commit -am 'Add new feature' - Push branch:
git push origin feature/new-feature - Submit Pull Request
This project is licensed under the MIT License. See LICENSE file for details.
This project is not affiliated with Z.ai official. Please ensure compliance with Z.ai's terms of service before use. Developers are not responsible for any issues arising from the use of this project.
For questions or suggestions, please contact through:
- Submit Issue