A local API server that provides OpenAI-compatible chat completions API for your preferred Grok interface, enabling seamless integration with your applications.
- 🔄 OpenAI-compatible API endpoints
- 🔄 Message processing with automatic queueing
- 📋 Clean API versioning
- 🔌 Easy integration with browser extension
- 🚀 Fast and reliable message processing
- Python 3.13 or higher
- pip (Python package installer)
- A modern web browser
- Tampermonkey or similar userscript manager
-
Clone the repository:
git clone https://github.com/excap3r/grok-free-api.git cd grok-free-api
-
Install dependencies:
pip3 install -r requirements.txt
You may also need to install additional packages for the test client:
pip3 install requests python-dotenv colorama
-
Start the local server:
python3 app.py
The server will start on
http://localhost:5001
-
Install the Tampermonkey browser extension:
-
Create a new userscript in Tampermonkey:
- Click on the Tampermonkey icon
- Select "Create a new script"
- Copy the contents of
chat.user.js
into the editor - Save the script (Ctrl+S or ⌘+S)
-
Enable the script and navigate to your Grok URL
IMPORTANT: You need to replace
grok.example.com
with your actual Grok URL in the following files:
- In
chat.user.js
: Update the@match
pattern (line 7) and theOrigin
header (line 33)- In
app.py
: Update references to the domain in server messages (lines 404-405)- The model ownership field uses a hyphenated format (
grok-example
)- In any other files that reference this domain
It's crucial that your Grok URL is correctly set before using the script, otherwise the userscript won't activate on the correct page.
The server comes with sensible defaults, but you can customize:
RESPONSE_EXPIRATION_TIME
: How long to keep responses (default: 300 seconds)API_BASE
: API endpoint base URL (default: http://localhost:5001)
POST /api/v1/chat
Content-Type: application/json
{
"message": "Your message here"
}
POST /api/v1/chat/completions
Content-Type: application/json
{
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
}
GET /api/v1/chat/completions/latest
Returns the latest chat completion response in OpenAI format.
GET /api/v1/messages/pending
POST /api/v1/messages/mark-processed
Content-Type: application/json
{
"message": "Message content to mark as processed"
}
The API implements a message queuing system that ensures all requests are processed in an orderly manner. Messages are stored until they are processed, preventing duplicates and ensuring a smooth experience.
All responses follow the OpenAI Chat Completions API format. Note that the model IDs have been changed to numeric values (2 and 3) instead of the OpenAI model names, and ownership is set to 'grok-example':
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "2", // Model ID 2 or 3
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Response content here"
},
"finish_reason": "stop"
}
]
}
A test client is included to help you verify that your setup is working properly:
python3 test_chat.py
This will start an interactive chat session where you can test the functionality of the API.
The API uses simplified model IDs:
2
- Equivalent to what was previously labeled as gpt-3.5-turbo3
- Equivalent to what was previously labeled as gpt-4o
Model ownership is set to grok-example
. You can modify these values in the list_models
function in app.py
if needed.
-
Server Connection Error
- Ensure the local server is running
- Check if the port 5001 is available
- Verify your firewall settings
-
Userscript Not Working
- Ensure Tampermonkey is properly installed
- Check if the script is enabled
- Clear browser cache and reload the page
- Verify that you've updated all instances of
grok.example.com
to your actual Grok URL
-
Processing Issues
- Ensure your messages are properly formatted
- Check for server connectivity
- Verify your browser's console for error messages
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
To quickly replace all instances of grok.example.com
with your actual Grok URL, you can use this command (replacing your-grok-url.com
with your actual URL). Note that this won't update the hyphenated format in model ownership:
find . -type f -name "*.py" -o -name "*.js" -o -name "*.md" | xargs sed -i '' 's/grok\.example\.com/your-grok-url.com/g'
# For manually updating the model ownership format if needed:
sed -i '' 's/grok-example/your-custom-name/g' app.py
Note: This command works for macOS. For Linux, remove the
''
after-i
.
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by OpenAI's API design
- Built with Flask and Python