- Overview
- Features
- Supported Models
- Quick Start
- API Usage
- Admin Dashboard
- Project Structure
- Configuration Reference
- Development
- Contributing
- License
- Acknowledgments
GrokToAPI is a high-performance API gateway that seamlessly bridges Grok (XAI's conversational AI) with an OpenAI-compatible interface. Built with Go for maximum reliability and speed, it provides:
- โ Enterprise-grade token management
- โ Intelligent media caching system
- โ Comprehensive admin dashboard
- โ Full OpenAI API compatibility
- โ Advanced TLS fingerprinting to avoid rate limits
Perfect for developers who want to integrate Grok AI into their applications using familiar OpenAI SDK patterns!
- OpenAI-Compatible API - Drop-in replacement for OpenAI clients
- Multiple Model Support - 6 Grok models including fast, expert, and video generation
- Streaming & Non-Streaming - Full support for Server-Sent Events (SSE)
- Token Pool Management - Intelligent quota tracking with automatic tier fallback
- ๐ธ Automatic caching of images and videos from Grok responses
- ๐๏ธ LRU-based cache management with configurable size limits
- ๐ URL rewriting for local media serving
- ๐ผ๏ธ Support for both URL and base64 image modes
- ๐ Web-based dashboard for token and system management
- ๐ Real-time statistics and usage tracking
- ๐ Bulk token operations (add/delete/test)
- โ๏ธ Live configuration updates
- ๐ต๏ธ TLS Fingerprinting - Advanced browser mimicry
- ๐ก๏ธ Bearer token authentication
- ๐ Rate limit handling and monitoring
- ๐ญ Model reasoning display (
show_thinking) - ๐ Web search integration
- ๐ Per-model usage statistics
- ๐ท๏ธ Response tag filtering
| Model | Description | Usage Multiplier | Super Token Required |
|---|---|---|---|
grok-3-fast |
โก Fast lightweight model | 1x | โ |
grok-4-fast |
๐ง Fast Grok 4 with mini thinking | 1x | โ |
grok-4-fast-expert |
๐ Expert mode with enhanced reasoning | 4x | โ |
grok-4-expert |
๐ Full Grok 4 expert capabilities | 4x | โ |
grok-4-heavy |
๐ Most powerful model | 1x | โ |
grok-imagine-0.9 |
๐ฅ Video generation model | - | โ |
- Go 1.22.0 or higher
- Valid Grok API tokens (Normal or Super tier)
# Clone the repository
git clone https://github.com/xiaoxu123195/GrokToAPI.git
cd GrokToAPI
# Build the binary
go build -o groktoapi ./cmd/groktoapiOr download pre-built binaries from the Releases page.
1๏ธโฃ Copy the configuration template:
cp data/setting.toml.example data/setting.toml2๏ธโฃ Edit data/setting.toml with your settings:
[grok]
api_key = "your-api-key" # Any string for API validation
proxy_url = "" # Optional HTTP proxy
show_thinking = true # Display model reasoning
stream_chunk_timeout = 120 # Per-chunk timeout (seconds)
stream_total_timeout = 600 # Total stream timeout (seconds)
[global]
base_url = "http://localhost:8000" # Your service URL
log_level = "INFO" # DEBUG, INFO, WARN, ERROR
image_mode = "url" # "url" or "base64"
admin_username = "admin" # Dashboard username
admin_password = "your-secure-password" # Dashboard password
image_cache_max_size_mb = 512 # Image cache limit (MB)
video_cache_max_size_mb = 1024 # Video cache limit (MB)3๏ธโฃ Add your Grok tokens:
- Manually edit
data/token.json, or - Use the Admin API after starting the service
# Default (listens on :8000)
./groktoapi
# Custom configuration
./groktoapi -config /path/to/setting.toml -addr :8080The service will be available at http://localhost:8000 ๐
Streaming Request:
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "grok-4-fast",
"messages": [
{
"role": "user",
"content": "What is the meaning of life?"
}
],
"stream": true
}'Non-Streaming Request:
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "grok-4-fast",
"messages": [
{
"role": "user",
"content": "Hello, Grok!"
}
],
"stream": false
}'curl http://localhost:8000/v1/models \
-H "Authorization: Bearer your-api-key"Response:
{
"object": "list",
"data": [
{
"id": "grok-4-fast",
"object": "model",
"created": 1677649963,
"owned_by": "xai"
}
]
}Access the admin dashboard at: http://localhost:8000/v1/admin/
Login to get a session token:
curl http://localhost:8000/v1/admin/api/login \
-H "Content-Type: application/json" \
-d '{
"username": "admin",
"password": "your-password"
}'Response:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}Add Tokens:
curl http://localhost:8000/v1/admin/api/tokens/add \
-H "Authorization: Bearer your-admin-token" \
-H "Content-Type: application/json" \
-d '{
"tokens": ["token1", "token2"],
"token_type": "Normal"
}'Get Statistics:
curl http://localhost:8000/v1/admin/api/stats \
-H "Authorization: Bearer your-admin-token"Response Example:
{
"pool_stats": {
"normal": {
"active": 10,
"limited": 2,
"expired": 1
}
},
"usage_stats": {
"grok-4-fast": 150,
"grok-4-expert": 45
},
"total_quota": 750
}๐ Full API Documentation: docs/admin_api.md
GrokToAPI/
โโโ ๐ cmd/
โ โโโ ๐ groktoapi/ # ๐ช Application entry point
โโโ ๐ internal/
โ โโโ ๐ api/ # ๐ HTTP routing and handlers
โ โโโ ๐ cache/ # ๐พ Media caching system
โ โโโ ๐ chat/ # ๐ฌ Chat service layer
โ โโโ ๐ config/ # โ๏ธ Configuration management
โ โโโ ๐ grok/ # ๐ค Grok API client
โ โโโ ๐ models/ # ๐ Model definitions and catalog
โ โโโ ๐ storage/ # ๐๏ธ Token persistence
โ โโโ ๐ token/ # ๐ซ Token pool management
โ โโโ ๐ logger/ # ๐ Logging utilities
โโโ ๐ data/
โ โโโ โ๏ธ setting.toml # Configuration file
โ โโโ ๐พ token.json # Token storage
โโโ ๐ docs/ # ๐ Documentation
โโโ ๐ web/ # ๐จ Frontend assets (future)
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key |
string | - | API authentication key (any string) |
base_endpoint |
string | - | Custom Grok API endpoint (optional) |
proxy_url |
string | - | HTTP proxy for API requests |
cache_proxy_url |
string | - | Separate proxy for media downloads |
cf_clearance |
string | - | Cloudflare clearance cookie |
x_statsig_id |
string | - | Browser fingerprinting ID (base64) |
dynamic_statsig |
bool | false | Auto-generate Statsig ID |
filtered_tags |
string | - | Comma-separated tags to filter |
stream_chunk_timeout |
int | 120 | Per-chunk timeout (seconds) |
stream_total_timeout |
int | 600 | Total stream timeout (seconds) |
stream_first_response_timeout |
int | 30 | Initial response timeout (seconds) |
show_thinking |
bool | true | Display model reasoning process |
| Parameter | Type | Default | Description |
|---|---|---|---|
base_url |
string | - | Base URL for media file serving |
log_level |
string | INFO | Logging level (DEBUG/INFO/WARN/ERROR) |
image_mode |
string | url | Media format ("url" or "base64") |
admin_username |
string | admin | Dashboard username |
admin_password |
string | - | Dashboard password |
image_cache_max_size_mb |
int | 512 | Image cache size limit (MB) |
video_cache_max_size_mb |
int | 1024 | Video cache size limit (MB) |
# Build for current platform
go build -o groktoapi ./cmd/groktoapi
# Build for Linux
GOOS=linux GOARCH=amd64 go build -o groktoapi-linux ./cmd/groktoapi
# Build for Windows
GOOS=windows GOARCH=amd64 go build -o groktoapi.exe ./cmd/groktoapi# Run all tests
go test ./...
# Run with coverage
go test -cover ./...
# Generate coverage report
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out| Package | Purpose |
|---|---|
gin-gonic/gin |
๐ HTTP web framework |
bogdanfinn/tls-client |
๐ Browser fingerprinting |
rs/zerolog |
๐ Structured logging |
spf13/viper |
โ๏ธ Configuration management |
google/uuid |
๐ UUID generation |
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 amazing feature') - ๐ค Push to the branch (
git push origin feature/amazing-feature) - ๐ Open a Pull Request
Please read CONTRIBUTING.md for details on our code of conduct.
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2025 GrokToAPI Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files...
- ๐ Originally ported from a Python implementation
- โก Built with Go for enhanced performance and reliability
- ๐ค Powered by XAI's Grok conversational AI
- ๐ Inspired by the open-source community
- ๐ Bug Reports: GitHub Issues
- ๐ก Feature Requests: GitHub Discussions
- ๐ง Email: Create an issue