Grok2API
中文 | English
Note
This project is for learning and research only. You must comply with Grok's Terms of Use and applicable laws. Do not use it for illegal purposes.
Grok2API rebuilt with FastAPI, fully aligned with the latest web call format. Supports streaming and non-streaming chat, image generation/editing, deep thinking, token pool concurrency, and automatic load balancing.
Usage
How to start
- Local development
uv sync
uv run main.py
- Deployment
git clone https://github.com/chenyme/grok2api
docker compose up -d
Admin panel
URL: http://<host>:8000/admin
Default password: grok2api (config key app.app_key, change it in production).
Environment variables
| Variable | Description | Default | Example |
|---|---|---|---|
LOG_LEVEL |
Log level | INFO |
DEBUG |
SERVER_HOST |
Bind address | 0.0.0.0 |
0.0.0.0 |
SERVER_PORT |
Service port | 8000 |
8000 |
SERVER_WORKERS |
Uvicorn worker count | 1 |
2 |
SERVER_STORAGE_TYPE |
Storage type (local/redis/mysql/pgsql) |
local |
pgsql |
SERVER_STORAGE_URL |
Storage URL (empty for local) | "" |
postgresql+asyncpg://user:password@host:5432/db |
Usage limits
- Basic account: 80 requests / 20h
- Super account: not tested by the author
Models
| Model | Cost | Account | Chat | Image | Video |
|---|---|---|---|---|---|
grok-3 |
1 | Basic/Super | Yes | Yes | - |
grok-3-fast |
1 | Basic/Super | Yes | Yes | - |
grok-4 |
1 | Basic/Super | Yes | Yes | - |
grok-4-mini |
1 | Basic/Super | Yes | Yes | - |
grok-4-fast |
1 | Basic/Super | Yes | Yes | - |
grok-4-heavy |
4 | Super | Yes | Yes | - |
grok-4.1 |
1 | Basic/Super | Yes | Yes | - |
grok-4.1-thinking |
4 | Basic/Super | Yes | Yes | - |
grok-imagine-1.0 |
4 | Basic/Super | - | Yes | - |
grok-imagine-1.0-video |
- | Basic/Super | - | - | Yes |
API
POST /v1/chat/completions
Generic endpoint: chat, image generation, image editing, video generation, video upscaling
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-d '{
"model": "grok-4",
"messages": [{"role":"user","content":"Hello"}]
}'
Supported request parameters
| Field | Type | Description | Allowed values |
|---|---|---|---|
model |
string | Model ID | - |
messages |
array | Message list | developer, system, user, assistant |
stream |
boolean | Enable streaming | true, false |
thinking |
string | Thinking mode | enabled, disabled, null |
video_config |
object | Video model only | - |
└─ aspect_ratio |
string | Video aspect ratio | 16:9, 9:16, 1:1, 2:3, 3:2 |
└─ video_length |
integer | Video length (seconds) | 5 - 15 |
└─ resolution |
string | Resolution | SD, HD |
└─ preset |
string | Style preset | fun, normal, spicy |
Note: any other parameters will be discarded and ignored.
POST /v1/images/generations
Image endpoint: image generation, image editing
curl http://localhost:8000/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-d '{
"model": "grok-imagine-1.0",
"prompt": "A cat floating in space",
"n": 1
}'
Supported request parameters
| Field | Type | Description | Allowed values |
|---|---|---|---|
model |
string | Image model ID | grok-imagine-1.0 |
prompt |
string | Prompt | - |
n |
integer | Number of images | 1 - 10 (streaming: 1 or 2 only) |
stream |
boolean | Enable streaming | true, false |
Note: any other parameters will be discarded and ignored.
Configuration
Config file: data/config.toml
Note
In production or behind a reverse proxy, make sure
app.app_urlis set to the public URL. Otherwise file links may be incorrect or return 403.
| Module | Field | Key | Description | Default |
|---|---|---|---|---|
| app | app_url |
App URL | External access URL for Grok2API (used for file links). | http://127.0.0.1:8000 |
app_key |
Admin password | Password for the Grok2API admin panel. | grok2api |
|
api_key |
API key | Bearer token required to call Grok2API. | "" |
|
image_format |
Image format | Output image format (url or base64). |
url |
|
video_format |
Video format | Output video format (url only). | url |
|
| grok | temporary |
Temporary chat | Enable temporary conversation mode. | true |
stream |
Streaming | Enable streaming by default. | true |
|
thinking |
Thinking chain | Enable model thinking output. | true |
|
dynamic_statsig |
Dynamic fingerprint | Enable dynamic Statsig value generation. | true |
|
filter_tags |
Filter tags | Auto-filter special tags in Grok responses. | ["xaiartifact", "xai:tool_usage_card", "grok:render"] |
|
timeout |
Timeout | Timeout for Grok requests (seconds). | 120 |
|
base_proxy_url |
Base proxy URL | Base service address proxying Grok official site. | "" |
|
asset_proxy_url |
Asset proxy URL | Proxy URL for Grok static assets (images/videos). | "" |
|
cf_clearance |
CF Clearance | Cloudflare clearance cookie for verification. | "" |
|
max_retry |
Max retries | Max retries on Grok request failure. | 3 |
|
retry_status_codes |
Retry status codes | HTTP status codes that trigger retry. | [401, 429, 403] |
|
| token | auto_refresh |
Auto refresh | Enable automatic token refresh. | true |
refresh_interval_hours |
Refresh interval | Token refresh interval (hours). | 8 |
|
fail_threshold |
Failure threshold | Consecutive failures before a token is disabled. | 5 |
|
save_delay_ms |
Save delay | Debounced save delay for token changes (ms). | 500 |
|
reload_interval_sec |
Consistency refresh | Token state refresh interval in multi-worker setups (sec). | 30 |
|
| cache | enable_auto_clean |
Auto clean | Enable cache auto clean; cleanup when exceeding limit. | true |
limit_mb |
Cleanup threshold | Cache size threshold (MB) that triggers cleanup. | 1024 |
|
| performance | assets_max_concurrent |
Assets concurrency | Concurrency cap for assets upload/download/list. Recommended 25. | 25 |
media_max_concurrent |
Media concurrency | Concurrency cap for video/media generation. Recommended 50. | 50 |
|
usage_max_concurrent |
Usage concurrency | Concurrency cap for usage queries. Recommended 25. | 25 |
|
assets_delete_batch_size |
Asset cleanup batch | Batch concurrency for online asset deletion. Recommended 10. | 10 |
|
admin_assets_batch_size |
Admin cleanup batch | Batch concurrency for admin asset stats/cleanup. Recommended 10. | 10 |