File size: 13,032 Bytes
ddebc3e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | <img alt="Grok2API" src="https://github.com/user-attachments/assets/037a0a6e-7986-41cc-b4af-04df612ee886" />
[](https://www.python.org/)
[](https://fastapi.tiangolo.com/)
[](../grok2api-main/grok2api-main/pyproject.toml)
[](../LICENSE)
[](https://github.com/jiujiu532/grok2api/pkgs/container/grok2api)
[](../README.md)
> [!NOTE]
> This project is for learning and research only. You must comply with Grok's Terms of Service and your local laws. Do not use it for unlawful purposes. Forks and PRs should preserve original author and frontend attribution.
<br>
Grok2API is a **FastAPI**-based Grok gateway that exposes Grok's web capabilities through OpenAI-compatible APIs. Highlights:
- OpenAI-compatible endpoints: `/v1/models`, `/v1/chat/completions`, `/v1/responses`, `/v1/images/generations`, `/v1/images/edits`, `/v1/videos`, `/v1/videos/{video_id}`, `/v1/videos/{video_id}/content`
- Anthropic-compatible endpoint: `/v1/messages`
- Streaming and non-streaming chat, explicit reasoning output, function tools passthrough, unified token / usage accounting
- Multi-account pool, tiered selection, failure feedback, quota sync and auto maintenance
- Local image / video caching with reverse-proxied URLs
- Text-to-image, image edit, text-to-video, image-to-video
- Built-in Admin console, Web Chat, Masonry image gallery, ChatKit voice page
- `console.x.ai` free account support with a dedicated `*-console` model family
<br>
## Image Info
This repository builds on top of [chenyme/grok2api](https://github.com/chenyme/grok2api) and ships a prebuilt Docker image:
| Field | Value |
| :-- | :-- |
| Image | `ghcr.io/jiujiu532/grok2api:latest` |
| Architecture | `linux/amd64` |
| Base image | `python:3.13-alpine` |
| Default port | `8000` |
| Data dir | `/app/data` |
| Logs dir | `/app/logs` |
<br>
## Quick Start
### Option 1: Docker Compose (recommended)
```bash
git clone https://github.com/jiujiu532/grok2api
cd grok2api/grok2api-main/grok2api-main
cp .env.example .env
docker compose up -d
```
Tail logs:
```bash
docker compose logs -f grok2api
```
> The included `docker-compose.yml` already pulls `ghcr.io/jiujiu532/grok2api:latest`. No local build is required.
### Option 2: Plain Docker
```bash
docker run -d \
--name grok2api \
-p 8000:8000 \
-e TZ=Asia/Shanghai \
-e LOG_LEVEL=INFO \
-e ACCOUNT_STORAGE=local \
-v $(pwd)/data:/app/data \
-v $(pwd)/logs:/app/logs \
--restart unless-stopped \
ghcr.io/jiujiu532/grok2api:latest
```
Windows PowerShell:
```powershell
docker run -d `
--name grok2api `
-p 8000:8000 `
-e TZ=Asia/Shanghai `
-e LOG_LEVEL=INFO `
-e ACCOUNT_STORAGE=local `
-v ${PWD}/data:/app/data `
-v ${PWD}/logs:/app/logs `
--restart unless-stopped `
ghcr.io/jiujiu532/grok2api:latest
```
### Option 3: From source
Prerequisites: Python 3.13+ and [uv](https://docs.astral.sh/uv/getting-started/installation/).
```bash
git clone https://github.com/jiujiu532/grok2api
cd grok2api/grok2api-main/grok2api-main
cp .env.example .env
uv sync
uv run granian --interface asgi --host 0.0.0.0 --port 8000 --workers 1 app.main:app
```
### First-time setup
After the service is up, open `http://localhost:8000/admin/login`. Default password is `grok2api`. Then:
1. Change `app.app_key` (Admin console password)
2. Set `app.api_key` (API auth key; leave empty to disable auth)
3. Set `app.app_url` (publicly reachable base URL; otherwise image / video links return 403)
> Runtime config is persisted to `${DATA_DIR}/config.toml` and applied immediately. No container restart is required.
<br>
## Upgrade and Rollback
```bash
# Upgrade to latest
docker compose pull
docker compose up -d
# Pull a specific tag (see GHCR for available versions)
docker pull ghcr.io/jiujiu532/grok2api:latest
# Rollback
docker run -d ... ghcr.io/jiujiu532/grok2api:<tag>
```
<br>
## Reverse Proxy (Nginx example)
```nginx
server {
listen 443 ssl http2;
server_name your.domain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Required for streaming
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
}
```
After enabling the reverse proxy, set `app.app_url` to `https://your.domain.com` in the Admin console.
<br>
## WebUI
| Page | Path |
| :-- | :-- |
| Admin login | `/admin/login` |
| Account management | `/admin/account` |
| Config management | `/admin/config` |
| Cache management | `/admin/cache` |
| WebUI login | `/webui/login` |
| Web Chat | `/webui/chat` |
| Masonry | `/webui/masonry` |
| ChatKit | `/webui/chatkit` |
### Authentication
| Scope | Config | Rule |
| :-- | :-- | :-- |
| `/v1/*` | `app.api_key` | No auth when empty |
| `/admin/*` | `app.app_key` | Default `grok2api` |
| `/webui/*` | `app.webui_enabled`, `app.webui_key` | Disabled by default; no extra check when `webui_key` is empty |
<br>
## Account Management
### Account types
| Type | Description | Models |
| :-- | :-- | :-- |
| **Paid account** | Official x.ai paid account | All `grok-4.20-*`, `grok-4.3-beta` |
| **Free account** | Free account via `console.x.ai` | All `*-console` models |
### Free account setup
To use free accounts you need both an SSO Token and a CF Clearance:
1. Open browser DevTools (F12)
2. Visit `https://console.x.ai/`
3. In the Network tab, inspect any request's cookies and copy:
- the `sso` value
- the `cf_clearance` value
4. In Admin → Account → Add account, paste both values into the matching fields
> SSO Token and CF Clearance are sensitive credentials. Never commit them to source control.
<br>
## Environment Variables
Bootstrap-time variables (`.env` / Compose / `docker run -e`):
| Name | Description | Default |
| :-- | :-- | :-- |
| `TZ` | Timezone | `Asia/Shanghai` |
| `LOG_LEVEL` | Log level | `INFO` |
| `LOG_FILE_ENABLED` | Write file logs | `true` |
| `ACCOUNT_SYNC_INTERVAL` | Account directory sync interval (s) | `30` |
| `ACCOUNT_SYNC_ACTIVE_INTERVAL` | Active sync interval after a change (s) | `3` |
| `SERVER_HOST` | Listen host | `0.0.0.0` |
| `SERVER_PORT` | Listen port | `8000` |
| `SERVER_WORKERS` | Granian workers | `1` |
| `HOST_PORT` | Compose host port mapping | `8000` |
| `DATA_DIR` | Data root | `./data` |
| `LOG_DIR` | Logs dir | `./logs` |
| `ACCOUNT_STORAGE` | Backend: `local` / `redis` / `mysql` / `postgresql` | `local` |
| `ACCOUNT_LOCAL_PATH` | SQLite path for `local` mode | `${DATA_DIR}/accounts.db` |
| `ACCOUNT_REDIS_URL` | DSN for `redis` mode | `""` |
| `ACCOUNT_MYSQL_URL` | DSN for `mysql` mode | `""` |
| `ACCOUNT_POSTGRESQL_URL` | DSN for `postgresql` mode | `""` |
| `ACCOUNT_SQL_POOL_SIZE` | SQL pool core size | `5` |
| `ACCOUNT_SQL_MAX_OVERFLOW` | SQL pool max overflow | `10` |
| `ACCOUNT_SQL_POOL_TIMEOUT` | Pool checkout timeout (s) | `30` |
| `ACCOUNT_SQL_POOL_RECYCLE` | Connection recycle time (s) | `1800` |
| `CONFIG_LOCAL_PATH` | Runtime config file path | `${DATA_DIR}/config.toml` |
Runtime config can also be overridden via `GROK_`-prefixed env vars, e.g. `GROK_APP_API_KEY` overrides `app.api_key`, `GROK_FEATURES_STREAM` overrides `features.stream`.
<br>
## Models
> Use `GET /v1/models` to fetch the live list.
### Chat (paid)
| Model | mode | tier |
| :-- | :-- | :-- |
| `grok-4.20-0309-non-reasoning` | `fast` | `basic` |
| `grok-4.20-0309` | `auto` | `super` |
| `grok-4.20-0309-reasoning` | `expert` | `super` |
| `grok-4.20-0309-non-reasoning-super` | `fast` | `super` |
| `grok-4.20-0309-super` | `auto` | `super` |
| `grok-4.20-0309-reasoning-super` | `expert` | `super` |
| `grok-4.20-0309-non-reasoning-heavy` | `fast` | `heavy` |
| `grok-4.20-0309-heavy` | `auto` | `heavy` |
| `grok-4.20-0309-reasoning-heavy` | `expert` | `heavy` |
| `grok-4.20-multi-agent-0309` | `heavy` | `heavy` |
| `grok-4.20-fast` | `fast` | `basic`, prefers higher-tier accounts |
| `grok-4.20-auto` | `auto` | `super`, prefers higher-tier accounts |
| `grok-4.20-expert` | `expert` | `super`, prefers higher-tier accounts |
| `grok-4.20-heavy` | `heavy` | `heavy` |
| `grok-4.3-beta` | `grok-420-computer-use-sa` | `super` |
### Chat (console.x.ai free)
| Model | reasoning effort | Notes |
| :-- | :-- | :-- |
| `grok-4-console` | default | Free account |
| `grok-4.3-console` | medium | Free account |
| `grok-4.3-low-console` | low | Free account |
| `grok-4.3-medium-console` | medium | Free account |
| `grok-4.3-high-console` | high | Free account |
| `grok-4.20-0309-console` | default | Free account |
| `grok-4.20-0309-reasoning-console` | fixed reasoning | Free account |
| `grok-4.20-multi-agent-console` | default | Free account, multi-agent |
### Image / Image Edit / Video
| Model | mode | tier |
| :-- | :-- | :-- |
| `grok-imagine-image-lite` | `fast` | `basic` |
| `grok-imagine-image` | `auto` | `super` |
| `grok-imagine-image-pro` | `auto` | `super` |
| `grok-imagine-image-edit` | `auto` | `super` |
| `grok-imagine-video` | `auto` | `super` |
<br>
## API Reference
| Endpoint | Auth | Description |
| :-- | :-- | :-- |
| `GET /v1/models` | yes | List enabled models |
| `GET /v1/models/{model_id}` | yes | Get a single model |
| `POST /v1/chat/completions` | yes | Unified chat / image / video entry |
| `POST /v1/responses` | yes | OpenAI Responses API subset |
| `POST /v1/messages` | yes | Anthropic Messages API |
| `POST /v1/images/generations` | yes | Standalone image generation |
| `POST /v1/images/edits` | yes | Standalone image editing |
| `POST /v1/videos` | yes | Async video job creation |
| `GET /v1/videos/{video_id}` | yes | Query a video job |
| `GET /v1/videos/{video_id}/content` | yes | Download the final video |
| `GET /v1/files/video?id=...` | no | Locally cached video |
| `GET /v1/files/image?id=...` | no | Locally cached image |
<br>
## Examples
### Paid account chat
```bash
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-d '{
"model": "grok-4.20-auto",
"stream": true,
"reasoning_effort": "high",
"messages": [
{"role":"user","content":"Hello"}
]
}'
```
### Free account chat
```bash
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-d '{
"model": "grok-4.3-high-console",
"stream": true,
"messages": [
{"role":"user","content":"Hello"}
]
}'
```
### Image generation
```bash
curl http://localhost:8000/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-d '{
"model": "grok-imagine-image",
"prompt": "A cat floating in outer space",
"n": 1,
"size": "1792x1024",
"response_format": "url"
}'
```
### Video generation
```bash
curl http://localhost:8000/v1/videos \
-H "Authorization: Bearer $GROK2API_API_KEY" \
-F "model=grok-imagine-video" \
-F "prompt=Neon rainy night street, cinematic slow-motion tracking shot" \
-F "seconds=10" \
-F "size=1792x1024" \
-F "resolution_name=720p" \
-F "preset=normal"
```
For full field references see the upstream [API docs](https://github.com/chenyme/grok2api#api-%E4%B8%80%E8%A7%88).
<br>
## FAQ
**Q: `/admin/login` is unreachable after the container starts.**
Check the port mapping with `docker compose ps` (expect `0.0.0.0:8000->8000/tcp`) and verify your host firewall allows it.
**Q: Image / video URLs return 403.**
`app.app_url` is missing or wrong. It must be a fully qualified URL that clients can reach (e.g. `https://api.example.com`).
**Q: Cloudflare keeps blocking requests.**
In Admin → Config → Proxy, switch `proxy.clearance.mode` to `manual` and provide matching `cf_cookies` + `user_agent`, or deploy FlareSolverr and switch to the `flaresolverr` mode.
**Q: Multi-worker deployment.**
When `SERVER_WORKERS > 1`, the account refresh scheduler elects a single leader via a file lock; other workers only run lightweight syncing. On Windows, single-worker mode is recommended.
<br>
## Credits
- Upstream: [chenyme/grok2api](https://github.com/chenyme/grok2api)
- DeepWiki: [chenyme/grok2api](https://deepwiki.com/chenyme/grok2api)
- Project blog: [blog.cheny.me](https://blog.cheny.me/blog/posts/grok2api)
<br>
## License
MIT
|