Files
grok2api/docs

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.

image

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

MySQL example: mysql+aiomysql://user:password@host:3306/db (if you set mysql://, it will be normalized to mysql+aiomysql://)

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_url is 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]
retry_backoff_base Backoff base Base delay for retry backoff (seconds). 0.5
retry_backoff_factor Backoff factor Exponential multiplier for retry backoff. 2.0
retry_backoff_max Backoff max Max wait per retry (seconds). 30.0
retry_budget Backoff budget Max total retry time per request (seconds). 90.0
stream_idle_timeout Stream idle timeout Idle timeout for streaming responses (seconds). 45.0
video_idle_timeout Video idle timeout Idle timeout for video generation (seconds). 90.0
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 nsfw_max_concurrent NSFW enable concurrency Concurrency cap for enabling NSFW in batch. Recommended 10. 10
nsfw_batch_size NSFW enable batch size Batch size for enabling NSFW. Recommended 50. 50
nsfw_max_tokens NSFW enable max tokens Max tokens per NSFW batch to avoid mistakes. Recommended 1000. 1000
usage_max_concurrent Token usage refresh concurrency Concurrency cap for batch usage refresh. Recommended 25. 25
usage_batch_size Token usage refresh batch size Batch size for usage refresh. Recommended 50. 50
usage_max_tokens Token usage refresh max tokens Max tokens per usage refresh batch. Recommended 1000. 1000
assets_max_concurrent Online assets find/delete concurrency Concurrency cap for online asset find/delete. Recommended 25. 25
assets_batch_size Online assets find/delete batch size Batch size for online asset find/delete. Recommended 10. 10
assets_max_tokens Online assets find/delete max tokens Max tokens per online asset find/delete batch. Recommended 1000. 1000
assets_delete_batch_size Online assets delete batch Batch concurrency for online asset deletion. Recommended 10. 10
media_max_concurrent Media concurrency Concurrency cap for video/media generation. Recommended 50. 50

Star History

Star History Chart