Skip to content

๐Ÿš€ Grok AI ็š„้ซ˜ๆ€ง่ƒฝ OpenAI ๅ…ผๅฎน็ฝ‘ๅ…ณ | ไผไธš็บงไปค็‰Œ็ฎก็† | ๅช’ไฝ“็ผ“ๅญ˜ | Go ๆž„ๅปบ

License

Notifications You must be signed in to change notification settings

xiaoxu123195/GrokToAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

30 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ GrokToAPI

High-Performance OpenAI-Compatible Gateway for Grok AI

License: MIT Go Version GitHub Stars GitHub Issues

English | ไธญๆ–‡


๐Ÿ“‹ Table of Contents


๐ŸŒŸ Overview

GrokToAPI is a high-performance API gateway that seamlessly bridges Grok (XAI's conversational AI) with an OpenAI-compatible interface. Built with Go for maximum reliability and speed, it provides:

  • โœ… Enterprise-grade token management
  • โœ… Intelligent media caching system
  • โœ… Comprehensive admin dashboard
  • โœ… Full OpenAI API compatibility
  • โœ… Advanced TLS fingerprinting to avoid rate limits

Perfect for developers who want to integrate Grok AI into their applications using familiar OpenAI SDK patterns!


โœจ Features

๐Ÿ”Œ Core Capabilities

  • OpenAI-Compatible API - Drop-in replacement for OpenAI clients
  • Multiple Model Support - 6 Grok models including fast, expert, and video generation
  • Streaming & Non-Streaming - Full support for Server-Sent Events (SSE)
  • Token Pool Management - Intelligent quota tracking with automatic tier fallback

๐ŸŽฌ Media Handling

  • ๐Ÿ“ธ Automatic caching of images and videos from Grok responses
  • ๐Ÿ—„๏ธ LRU-based cache management with configurable size limits
  • ๐Ÿ”— URL rewriting for local media serving
  • ๐Ÿ–ผ๏ธ Support for both URL and base64 image modes

๐ŸŽ›๏ธ Admin Features

  • ๐Ÿ“Š Web-based dashboard for token and system management
  • ๐Ÿ“ˆ Real-time statistics and usage tracking
  • ๐Ÿ”‘ Bulk token operations (add/delete/test)
  • โš™๏ธ Live configuration updates

๐Ÿ” Advanced Security

  • ๐Ÿ•ต๏ธ TLS Fingerprinting - Advanced browser mimicry
  • ๐Ÿ›ก๏ธ Bearer token authentication
  • ๐Ÿ”’ Rate limit handling and monitoring

๐Ÿง  Intelligence Features

  • ๐Ÿ’ญ Model reasoning display (show_thinking)
  • ๐ŸŒ Web search integration
  • ๐Ÿ“Š Per-model usage statistics
  • ๐Ÿท๏ธ Response tag filtering

๐ŸŽฏ Supported Models

Model Description Usage Multiplier Super Token Required
grok-3-fast โšก Fast lightweight model 1x โŒ
grok-4-fast ๐Ÿง  Fast Grok 4 with mini thinking 1x โŒ
grok-4-fast-expert ๐ŸŽ“ Expert mode with enhanced reasoning 4x โŒ
grok-4-expert ๐Ÿ† Full Grok 4 expert capabilities 4x โŒ
grok-4-heavy ๐Ÿ’Ž Most powerful model 1x โœ…
grok-imagine-0.9 ๐ŸŽฅ Video generation model - โŒ

๐Ÿš€ Quick Start

Prerequisites

  • Go 1.22.0 or higher
  • Valid Grok API tokens (Normal or Super tier)

Installation

# Clone the repository
git clone https://github.com/xiaoxu123195/GrokToAPI.git
cd GrokToAPI

# Build the binary
go build -o groktoapi ./cmd/groktoapi

Or download pre-built binaries from the Releases page.

Configuration

1๏ธโƒฃ Copy the configuration template:

cp data/setting.toml.example data/setting.toml

2๏ธโƒฃ Edit data/setting.toml with your settings:

[grok]
api_key = "your-api-key"              # Any string for API validation
proxy_url = ""                        # Optional HTTP proxy
show_thinking = true                  # Display model reasoning
stream_chunk_timeout = 120            # Per-chunk timeout (seconds)
stream_total_timeout = 600            # Total stream timeout (seconds)

[global]
base_url = "http://localhost:8000"    # Your service URL
log_level = "INFO"                    # DEBUG, INFO, WARN, ERROR
image_mode = "url"                    # "url" or "base64"
admin_username = "admin"              # Dashboard username
admin_password = "your-secure-password"  # Dashboard password
image_cache_max_size_mb = 512         # Image cache limit (MB)
video_cache_max_size_mb = 1024        # Video cache limit (MB)

3๏ธโƒฃ Add your Grok tokens:

  • Manually edit data/token.json, or
  • Use the Admin API after starting the service

Running the Service

# Default (listens on :8000)
./groktoapi

# Custom configuration
./groktoapi -config /path/to/setting.toml -addr :8080

The service will be available at http://localhost:8000 ๐ŸŽ‰


๐Ÿ“ก API Usage

Chat Completion (OpenAI Compatible)

Streaming Request:

curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-api-key" \
  -d '{
    "model": "grok-4-fast",
    "messages": [
      {
        "role": "user",
        "content": "What is the meaning of life?"
      }
    ],
    "stream": true
  }'

Non-Streaming Request:

curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-api-key" \
  -d '{
    "model": "grok-4-fast",
    "messages": [
      {
        "role": "user",
        "content": "Hello, Grok!"
      }
    ],
    "stream": false
  }'

List Models

curl http://localhost:8000/v1/models \
  -H "Authorization: Bearer your-api-key"

Response:

{
  "object": "list",
  "data": [
    {
      "id": "grok-4-fast",
      "object": "model",
      "created": 1677649963,
      "owned_by": "xai"
    }
  ]
}

๐ŸŽ›๏ธ Admin Dashboard

Access the admin dashboard at: http://localhost:8000/v1/admin/

Authentication

Login to get a session token:

curl http://localhost:8000/v1/admin/api/login \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "your-password"
  }'

Response:

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Token Management

Add Tokens:

curl http://localhost:8000/v1/admin/api/tokens/add \
  -H "Authorization: Bearer your-admin-token" \
  -H "Content-Type: application/json" \
  -d '{
    "tokens": ["token1", "token2"],
    "token_type": "Normal"
  }'

Get Statistics:

curl http://localhost:8000/v1/admin/api/stats \
  -H "Authorization: Bearer your-admin-token"

Response Example:

{
  "pool_stats": {
    "normal": {
      "active": 10,
      "limited": 2,
      "expired": 1
    }
  },
  "usage_stats": {
    "grok-4-fast": 150,
    "grok-4-expert": 45
  },
  "total_quota": 750
}

๐Ÿ“– Full API Documentation: docs/admin_api.md


๐Ÿ“‚ Project Structure

GrokToAPI/
โ”œโ”€โ”€ ๐Ÿ“ cmd/
โ”‚   โ””โ”€โ”€ ๐Ÿ“ groktoapi/           # ๐Ÿšช Application entry point
โ”œโ”€โ”€ ๐Ÿ“ internal/
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ api/                 # ๐ŸŒ HTTP routing and handlers
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ cache/               # ๐Ÿ’พ Media caching system
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ chat/                # ๐Ÿ’ฌ Chat service layer
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ config/              # โš™๏ธ Configuration management
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ grok/                # ๐Ÿค– Grok API client
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ models/              # ๐Ÿ“‹ Model definitions and catalog
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ storage/             # ๐Ÿ—„๏ธ Token persistence
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ token/               # ๐ŸŽซ Token pool management
โ”‚   โ””โ”€โ”€ ๐Ÿ“ logger/              # ๐Ÿ“ Logging utilities
โ”œโ”€โ”€ ๐Ÿ“ data/
โ”‚   โ”œโ”€โ”€ โš™๏ธ setting.toml         # Configuration file
โ”‚   โ””โ”€โ”€ ๐Ÿ’พ token.json           # Token storage
โ”œโ”€โ”€ ๐Ÿ“ docs/                    # ๐Ÿ“š Documentation
โ””โ”€โ”€ ๐Ÿ“ web/                     # ๐ŸŽจ Frontend assets (future)

โš™๏ธ Configuration Reference

[grok] Section

Parameter Type Default Description
api_key string - API authentication key (any string)
base_endpoint string - Custom Grok API endpoint (optional)
proxy_url string - HTTP proxy for API requests
cache_proxy_url string - Separate proxy for media downloads
cf_clearance string - Cloudflare clearance cookie
x_statsig_id string - Browser fingerprinting ID (base64)
dynamic_statsig bool false Auto-generate Statsig ID
filtered_tags string - Comma-separated tags to filter
stream_chunk_timeout int 120 Per-chunk timeout (seconds)
stream_total_timeout int 600 Total stream timeout (seconds)
stream_first_response_timeout int 30 Initial response timeout (seconds)
show_thinking bool true Display model reasoning process

[global] Section

Parameter Type Default Description
base_url string - Base URL for media file serving
log_level string INFO Logging level (DEBUG/INFO/WARN/ERROR)
image_mode string url Media format ("url" or "base64")
admin_username string admin Dashboard username
admin_password string - Dashboard password
image_cache_max_size_mb int 512 Image cache size limit (MB)
video_cache_max_size_mb int 1024 Video cache size limit (MB)

๐Ÿ› ๏ธ Development

Building

# Build for current platform
go build -o groktoapi ./cmd/groktoapi

# Build for Linux
GOOS=linux GOARCH=amd64 go build -o groktoapi-linux ./cmd/groktoapi

# Build for Windows
GOOS=windows GOARCH=amd64 go build -o groktoapi.exe ./cmd/groktoapi

Testing

# Run all tests
go test ./...

# Run with coverage
go test -cover ./...

# Generate coverage report
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

Dependencies

Package Purpose
gin-gonic/gin ๐ŸŒ HTTP web framework
bogdanfinn/tls-client ๐Ÿ” Browser fingerprinting
rs/zerolog ๐Ÿ“ Structured logging
spf13/viper โš™๏ธ Configuration management
google/uuid ๐Ÿ†” UUID generation

๐Ÿค Contributing

Contributions are welcome! Here's how you can help:

  1. ๐Ÿด Fork the repository
  2. ๐Ÿ”จ Create a feature branch (git checkout -b feature/amazing-feature)
  3. ๐Ÿ’พ Commit your changes (git commit -m 'Add amazing feature')
  4. ๐Ÿ“ค Push to the branch (git push origin feature/amazing-feature)
  5. ๐ŸŽ‰ Open a Pull Request

Please read CONTRIBUTING.md for details on our code of conduct.


๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

MIT License

Copyright (c) 2025 GrokToAPI Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files...

๐Ÿ™ Acknowledgments

  • ๐Ÿ Originally ported from a Python implementation
  • โšก Built with Go for enhanced performance and reliability
  • ๐Ÿค– Powered by XAI's Grok conversational AI
  • ๐Ÿ’™ Inspired by the open-source community

๐Ÿ“ž Support


Made with โค๏ธ by the GrokToAPI Team

โญ Star us on GitHub โ€” it helps!

โฌ† Back to Top

About

๐Ÿš€ Grok AI ็š„้ซ˜ๆ€ง่ƒฝ OpenAI ๅ…ผๅฎน็ฝ‘ๅ…ณ | ไผไธš็บงไปค็‰Œ็ฎก็† | ๅช’ไฝ“็ผ“ๅญ˜ | Go ๆž„ๅปบ

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published