grok2api is an open source project based on a refactoring of the FastAPI framework, aiming to bring the X (formerly Twitter) Grok The web call format for large models is converted into an interface that is fully compatible with the OpenAI API. This allows developers to easily integrate Grok's powerful features into a variety of existing applications and services. The project not only supports streaming conversations, networked search and deep thinking modes, but also implements multimedia features such as image generation, image editing and video generation. In order to effectively manage multiple Grok accounts, grok2api has a built-in account pooling concurrent invocation and automatic load balancing mechanism that monitors and allocates the number of invocations for each account in real time, thus maximizing the use of resources. The project also provides a visual management backend for users to configure parameters, manage accounts and monitor service status.
Function List
- Compatible with OpenAI interface: The API endpoints provided by the project are fully compatible with the official OpenAI interface, making it easy for developers to migrate and integrate.
- multimedia processing: Support for generating pictures directly in a dialog, or generating videos based on pictures and prompt words through a specific model.
- Multiple model support: Support
grok-3-fast,grok-4-fast,grok-4-expertetc. Users can choose different models to call according to their needs. - Account Pooling and Load Balancing: Supports batch adding Grok accounts, the system will automatically load balance the number of calls to all accounts to avoid individual accounts being limited due to overuse.
- structured dialog: Streaming response is supported, allowing you to output content verbatim like an official website, improving the interactive experience.
- caching mechanism: The system automatically caches the generated image and video files locally to solve the problem that the official Grok link cannot be accessed directly, and by configuring the
Base Urlto ensure that media files are displayed properly. [cite:1-2] - Visual Management Back Office: Provides a web management interface that allows users to log in to the backend to configure the system, manage account tokens, view usage statistics, and clear the cache.
- Flexible storage options: Support the use of local files, MySQL databases or Redis as the storage backend for account and configuration information, adapting to different deployment environments.
Using Help
grok2api offers a variety of ways to deploy and use it, with Docker Compose being the easiest and fastest way.
1. Deployment methodology
Recommended docker-compose Perform one-click deployments, which can simplify the complexity of environment configuration.
First, create a file named docker-compose.yml file and copy the following into it:
services:
grok2api:
image: ghcr.io/chenyme/grok2api:latest
container_name: grok2api
ports:
- "8000:8000"
volumes:
- grok_data:/app/data
- ./logs:/app/logs
environment:
# 存储模式,可选值为 file, mysql 或 redis,默认为 file
- STORAGE_MODE=file
# 如果使用 mysql 或 redis,需要配置下面的数据库连接URL
# - DATABASE_URL=mysql://user:password@host:3306/grok2api
volumes:
grok_data:
Next, in the directory containing the file, open a terminal and run the following command to start the service:
docker-compose up -d
The service will be started in the background and will listen to the local 8000 Ports.
2. Initialization configuration
After the service is started for the first time, you need to initialize the configuration through the management backend.
- Access to the management background
Open in your browserhttp://<你的服务器地址>:8000/loginThe
The default administrator username isadminThe password isadminThe - Change administrator password
After logging in, please change the default administrator user name and password in the "System Configuration" page of the management background to ensure the security of the service. - Configuring the Base URL
This is a very important step, especially if you need to use the image or video generation feature.base_urlis for the generated media files to be accessed properly. Please set it to an address that your server can access, such ashttp://192.168.1.10:8000maybehttps://your-domain.comThe - Add Grok account
On the "Token Management" page in the administration background, click the "Add Token in Bulk" button, and add your Grok account information (e.g., your Token, Token, Token, Token, Token, Token, Token, Token, Token, Token, Token, Token).cf_clearancecap (a poem)auth_token) added in. This information needs to be obtained by the user from the browser developer tools.
3. How to call the API
grok2api interfaces with OpenAI's chat/completions The interfaces are fully compatible. You can call it using any client or library that supports the OpenAI API.
Initiate a chat request
The following is an example of how to use the curl Example of a command to initiate a streaming chat request:
curl https://你的服务器地址/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <你在后台设置的API_KEY>" \
-d '{
"model": "grok-4-fast",
"messages": [
{
"role": "user",
"content": "你好,请介绍一下你自己"
}
],
"stream": true
}'
- commander-in-chief (military)
你的服务器地址Replace with the IP address or domain name where the grok2api service is deployed. - commander-in-chief (military)
<你在后台设置的API_KEY>Replace it with the one you set in the admin backendapi_key. If not set, there is no need toAuthorizationRequest Header.
Generate Image
Include "Draw one" or similar command in the dialog to trigger the image generation function.
curl https://你的服务器地址/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <你在后台设置的API_KEY>" \
-d '{
"model": "grok-4-fast",
"messages": [
{
"role": "user",
"content": "请帮我画一只正在写代码的猫"
}
]
}'
The system returns two images in Markdown format and consumes four credits. The image links returned are based on your configured base_urlThe
Generate Video
To generate the video, you need to select grok-imagine-0.9 model and pass in a picture and corresponding cue word.
curl https://你的服务器地址/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <你在后台设置的API_KEY>" \
-d '{
"model": "grok-imagine-0.9",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "让画面中的风车转动起来"
},
{
"type": "image_url",
"image_url": {
"url": "https://your-image-url.jpg"
}
}
]
}
]
}'
The return content will be an HTML containing the video link <video> Tags.
application scenario
- Application Expansion
For all kinds of applications that have already integrated the OpenAI API (e.g., chatbots, content creation tools, etc.), developers can use grok2api to quickly integrate Grok modeling capabilities with virtually no code changes, just changes to the API's base address and key, providing users with an additional large selection of models with networked searches and a unique style of humor. - Efficient management of multiple accounts
For users or teams with multiple Grok accounts, it is very tedious to manage and rotate the accounts manually. grok2api's number pooling and automatic load balancing features can solve this problem. Users just need to add all accounts to the background, the system will automatically allocate requests to ensure that the frequency of use of each account is balanced, to avoid service interruptions due to exceeding the limit, and maximize the use of all account resources. - Low-cost API alternatives
For individual developers, researchers or startups who need to use a big model API but have a limited budget, grok2api offers a very cost-effective solution. It allows users to access the power of Grok for experimentation, prototyping or small-scale applications at close to no cost by converting the web-side operations into an API interface. - Multimedia content generation
Content creators or automation applications that need to generate images or short videos can take advantage of grok2api's multimedia generation capabilities. With a simple API call, visual content can be generated based on text or image cues for social media posts, marketing materials or automated video production processes.
QA
- Do I need to pay to use grok2api?
The grok2api project itself is open source and free. However, you need to have at least one Grok account (regular or Super) to use its features. According to Grok's official policy, there is a limit on the number of calls (e.g. 80 per 20 hours) for regular accounts. - Is this program an official API?
Not. This project is a third-party tool that encapsulates its functionality into an API interface by simulating a browser's interaction with the web version of Grok. As such, its stability may be affected by updates to the official Grok web version. - How to get the authentication information of Grok account (
cf_clearancecap (a poem)auth_token(___)?
You will need to log in to Grok's official website and then use your browser's developer tools (usually opened by pressing F12) to find the appropriate cookie and request header information in the Network or Application tab. This is a highly technical operation, so please handle your personal authentication information with care. - Why can't the generated images or videos be displayed?
This is usually due to not setting thebase_urlparameters. The links to media files returned by Grok have access restrictions and grok2api caches them locally. You must set thebase_urlSet to the publicly accessible address of the service (e.g.https://yourdomain.commaybehttp://<服务器IP>:8000) so that the media links returned by the API are accessible.






























