logo
0
0
WeChat Login
Chenyme<118253778+chenyme@users.noreply.github.com>
feat: Introduce Imagine Public Image Proxy feature
Grok2API

Python FastAPI Version License English DeepWiki 项目文档

NOTE

本项目仅供学习与研究交流。请务必遵循 Grok 的使用条款及当地法律法规,不得用于非法用途!二开与 PR 请保留原作者与前端标识。


Grok2API 是一个基于 FastAPI 构建的 Grok 网关,支持将 Grok Web 能力以 OpenAI 兼容 API 的方式转换。核心特性:

  • OpenAI 兼容接口:/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 兼容接口:/v1/messages
  • 支持流式与非流式对话、显式思考输出、函数工具结构透传,以及统一的 token / usage 统计
  • 支持多账号池、层级选号、失败反馈、额度同步与自动维护
  • 支持本地缓存图片、视频与本地代理链接返回
  • 支持文生图、图像编辑、文生视频、图生视频
  • 内置 Admin 后台管理、Web Chat、Masonry 生图、ChatKit 语音页面

服务架构


快速开始

本地部署

git clone https://github.com/chenyme/grok2api
cd grok2api
cp .env.example .env
uv sync
uv run granian --interface asgi --host 0.0.0.0 --port 8000 --workers 1 app.main:app

Docker Compose

git clone https://github.com/chenyme/grok2api
cd grok2api
cp .env.example .env
docker compose up -d

Vercel

Deploy with Vercel

Render

Deploy to Render

首次启动

  1. 修改 app.app_key
  2. 设置 app.api_key
  3. 设置 app.app_url(否则图片、视频的链接会 403 无权访问)

WebUI

页面入口

页面路径
Admin 登录页/admin/login
账号管理/admin/account
配置管理/admin/config
缓存管理/admin/cache
WebUI 登录页/webui/login
Web Chat/webui/chat
Masonry/webui/masonry
ChatKit/webui/chatkit

鉴权规则

范围配置项规则
/v1/*app.api_key为空则不额外鉴权
/admin/*app.app_key默认值 grok2api
/webui/*app.webui_enabled, app.webui_key默认关闭;webui_key 为空则不额外校验

配置体系

配置分层

位置用途生效时机
.env启动前配置服务启动时
${DATA_DIR}/config.toml运行时配置保存后即时生效
config.defaults.toml默认模板首次初始化时

环境变量

变量名说明默认值
TZ时区Asia/Shanghai
LOG_LEVEL日志级别INFO
LOG_FILE_ENABLED写入本地文件日志true
ACCOUNT_SYNC_INTERVAL账号目录增量同步间隔(秒)30
ACCOUNT_SYNC_ACTIVE_INTERVAL账号目录检测到变化后的活跃同步间隔(秒)3
SERVER_HOST服务监听地址0.0.0.0
SERVER_PORT服务监听端口8000
SERVER_WORKERSGranian worker 数量1
HOST_PORTDocker Compose 宿主机映射端口8000
DATA_DIR本地数据根目录(账号库、本地媒体文件、缓存索引统一位于此目录下)./data
LOG_DIR本地日志目录./logs
ACCOUNT_STORAGE账号存储后端local
ACCOUNT_LOCAL_PATHlocal 模式账号 SQLite 路径${DATA_DIR}/accounts.db
ACCOUNT_REDIS_URLredis 模式 Redis DSN""
ACCOUNT_MYSQL_URLmysql 模式 SQLAlchemy DSN""
ACCOUNT_POSTGRESQL_URLpostgresql 模式 SQLAlchemy DSN""
ACCOUNT_SQL_POOL_SIZESQL 连接池核心连接数5
ACCOUNT_SQL_MAX_OVERFLOWSQL 连接池最大溢出连接数10
ACCOUNT_SQL_POOL_TIMEOUT等待连接池空闲连接的超时时间(秒)30
ACCOUNT_SQL_POOL_RECYCLE连接最大复用时间(秒),超时后自动重连1800
CONFIG_LOCAL_PATHlocal 模式运行时配置文件路径${DATA_DIR}/config.toml

运行时配置也支持 GROK_ 前缀环境变量覆盖,例如 GROK_APP_API_KEY 会覆盖 app.api_keyGROK_FEATURES_STREAM 会覆盖 features.stream

系统配置项

分组关键项
appapp_key, app_url, api_key, webui_enabled, webui_key
loggingfile_level, max_files
featurestemporary, memory, stream, thinking, auto_chat_mode_fallback, thinking_summary, dynamic_statsig, enable_nsfw, show_search_sources, custom_instruction, image_format, imagine_public_image_proxy, video_format
proxy.egressmode, proxy_url, proxy_pool, resource_proxy_url, resource_proxy_pool, skip_ssl_verify
proxy.clearancemode, cf_cookies, user_agent, browser, flaresolverr_url, timeout_sec, refresh_interval
retryreset_session_status_codes, max_retries, on_codes
account.refreshbasic_interval_sec, super_interval_sec, heavy_interval_sec, usage_concurrency, on_demand_min_interval_sec
cache.localimage_max_mb, video_max_mb
chattimeout
imagetimeout, stream_timeout
videotimeout
voicetimeout
assetupload_timeout, download_timeout, list_timeout, delete_timeout
nsfwtimeout
batchnsfw_concurrency, refresh_concurrency, asset_upload_concurrency, asset_list_concurrency, asset_delete_concurrency

图片、视频格式

配置项可选值
features.image_formatgrok_url, local_url, grok_md, local_md, base64
features.imagine_public_image_proxytrue, false
features.video_formatgrok_url, local_url, grok_html, local_html

模型支持

可通过 GET /v1/models 获取当前支持模型列表。

Chat

模型名modetier
grok-4.20-0309-non-reasoningfastbasic
grok-4.20-0309autosuper
grok-4.20-0309-reasoningexpertsuper
grok-4.20-0309-non-reasoning-superfastsuper
grok-4.20-0309-superautosuper
grok-4.20-0309-reasoning-superexpertsuper
grok-4.20-0309-non-reasoning-heavyfastheavy
grok-4.20-0309-heavyautoheavy
grok-4.20-0309-reasoning-heavyexpertheavy
grok-4.20-multi-agent-0309heavyheavy
grok-4.20-fastfastbasic,优先使用高等级账号池
grok-4.20-autoautosuper,优先使用高等级账号池
grok-4.20-expertexpertsuper,优先使用高等级账号池
grok-4.20-heavyheavyheavy
grok-4.3-betagrok-420-computer-use-sasuper

Image

模型名modetier
grok-imagine-image-litefastbasic
grok-imagine-imageautosuper
grok-imagine-image-proautosuper

Image Edit

模型名modetier
grok-imagine-image-editautosuper

Video

模型名modetier
grok-imagine-videoautosuper

API 一览

接口是否鉴权说明
GET /v1/models列出当前启用模型
GET /v1/models/{model_id}获取单个模型信息
POST /v1/chat/completions对话 / 图像 / 视频统一入口
POST /v1/responsesOpenAI Responses API 兼容子集
POST /v1/messagesAnthropic Messages API 兼容接口
POST /v1/images/generations独立图像生成接口
POST /v1/images/edits独立图像编辑接口
POST /v1/videos异步视频任务创建
GET /v1/videos/{video_id}查询视频任务
GET /v1/videos/{video_id}/content获取最终视频文件
GET /v1/files/video?id=...获取本地缓存视频
GET /v1/files/image?id=...获取本地缓存图片

接口示例

以下示例默认使用 http://localhost:8000 地址。

GET /v1/models
curl http://localhost:8000/v1/models \
  -H "Authorization: Bearer $GROK2API_API_KEY"
字段说明
字段位置说明
AuthorizationHeaderapp.api_key 非空时必填,格式为 Bearer <api_key>


POST /v1/chat/completions

对话:

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":"你好"}
    ]
  }'

图像:

curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $GROK2API_API_KEY" \
  -d '{
    "model": "grok-imagine-image",
    "stream": true,
    "messages": [
      {"role":"user","content":"一只在太空漂浮的猫"}
    ],
    "image_config": {
      "n": 2,
      "size": "1024x1024",
      "response_format": "url"
    }
  }'

视频:

curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $GROK2API_API_KEY" \
  -d '{
    "model": "grok-imagine-video",
    "stream": true,
    "messages": [
      {"role":"user","content":"霓虹雨夜街头,电影感慢镜头追拍"}
    ],
    "video_config": {
      "seconds": 10,
      "size": "1792x1024",
      "resolution_name": "720p",
      "preset": "normal"
    }
  }'
字段说明
字段说明
messages支持文本与多模态内容块
stream是否流式输出;不传时使用 features.stream 默认值
reasoning_effortnone, minimal, low, medium, high, xhighnone 会关闭思考输出
temperature / top_p采样参数,默认 0.8 / 0.95
toolsOpenAI function tools 结构
tool_choiceauto, required 或指定函数工具
image_config图像模型参数
|_ nlite1-4,其他图像模型为 1-10,编辑模型为 1-2
|_ size1280x720, 720x1280, 1792x1024, 1024x1792, 1024x1024
|_ response_formaturl, b64_json
video_config视频模型参数
|_ seconds6, 10, 12, 16, 20
|_ size720x1280, 1280x720, 1024x1024, 1024x1792, 1792x1024
|_ resolution_name480p, 720p
|_ presetfun, normal, spicy, custom


POST /v1/responses
curl http://localhost:8000/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $GROK2API_API_KEY" \
  -d '{
    "model": "grok-4.20-auto",
    "input": "解释一下量子隧穿",
    "instructions": "用简洁的中文回答",
    "stream": true,
    "reasoning": {
      "effort": "high"
    }
  }'
字段说明
字段说明
model模型 ID,需为已启用模型
input用户输入;支持字符串或 Responses API 风格的消息数组
instructions可选系统指令,会作为 system 消息注入
stream是否流式输出;不传时使用 features.stream 默认值
reasoning可选思考配置
|_ effortnone 会关闭思考输出;其他值会开启思考输出
temperature / top_p采样参数,默认 0.8 / 0.95
tools / tool_choice支持函数工具;Responses API 的扁平工具格式会自动转换


POST /v1/messages
curl http://localhost:8000/v1/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $GROK2API_API_KEY" \
  -d '{
    "model": "grok-4.20-auto",
    "stream": true,
    "thinking": {
      "type": "enabled",
      "budget_tokens": 1024
    },
    "messages": [
      {
        "role": "user",
        "content": "用三句话解释量子隧穿"
      }
    ]
  }'
字段说明
字段说明
model模型 ID,需为已启用模型
messagesAnthropic Messages 格式消息,支持文本、图片、文档和工具结果块
system可选系统提示词,支持字符串或文本块数组
stream是否流式输出;不传时使用 features.stream 默认值
thinking可选思考配置
|_ typedisabled 会关闭思考输出;其他配置会开启思考输出
max_tokens接收但当前会忽略,Grok 上游不暴露该参数
tools / tool_choice支持 Anthropic 工具格式,会转换为内部 function tools


POST /v1/images/generations
curl http://localhost:8000/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $GROK2API_API_KEY" \
  -d '{
    "model": "grok-imagine-image",
    "prompt": "一只在太空漂浮的猫",
    "n": 1,
    "size": "1792x1024",
    "response_format": "url"
  }'
字段说明
字段说明
model图像模型:grok-imagine-image-lite, grok-imagine-image, grok-imagine-image-pro
prompt图片生成提示词
n生成数量;lite1-4,其他图像模型为 1-10
size支持 1280x720, 720x1280, 1792x1024, 1024x1792, 1024x1024
response_formaturlb64_json


POST /v1/images/edits
curl http://localhost:8000/v1/images/edits \
  -H "Authorization: Bearer $GROK2API_API_KEY" \
  -F "model=grok-imagine-image-edit" \
  -F "prompt=把这张图变清晰一些" \
  -F "image[]=@/path/to/image.png" \
  -F "n=1" \
  -F "size=1024x1024" \
  -F "response_format=url"
字段说明
字段说明
model图像编辑模型,目前为 grok-imagine-image-edit
prompt编辑指令
image[]参考图片,multipart 文件字段;最多使用 5 张
n生成数量,范围 1-2
size当前仅支持 1024x1024
response_formaturlb64_json
mask暂不支持;传入会返回校验错误


POST /v1/videos
curl http://localhost:8000/v1/videos \
  -H "Authorization: Bearer $GROK2API_API_KEY" \
  -F "model=grok-imagine-video" \
  -F "prompt=霓虹雨夜街头,电影感慢镜头追拍" \
  -F "seconds=10" \
  -F "size=1792x1024" \
  -F "resolution_name=720p" \
  -F "preset=normal" \
  -F "input_reference[]=@/path/to/reference.png"
curl http://localhost:8000/v1/videos/<video_id> \
  -H "Authorization: Bearer $GROK2API_API_KEY"

curl -L http://localhost:8000/v1/videos/<video_id>/content \
  -H "Authorization: Bearer $GROK2API_API_KEY" \
  -o result.mp4
字段说明
字段说明
model视频模型,目前为 grok-imagine-video
prompt视频生成提示词
seconds视频长度:6, 10, 12, 16, 20
size支持 720x1280, 1280x720, 1024x1024, 1024x1792, 1792x1024
resolution_name480p720p
presetfun, normal, spicy, custom
input_reference[]可选图生视频参考图,multipart 文件字段;最多使用前 7 张
video_idPOST /v1/videos 返回的视频任务 ID,用于查询任务或下载成片



Star History

Star History Chart