Skip to content

Latest commit

 

History

History
319 lines (222 loc) · 22 KB

File metadata and controls

319 lines (222 loc) · 22 KB

Google AI Studio to API Adapter

中文文档 | English

A tool that wraps Google AI Studio web interface to provide OpenAI API, Gemini API, and Anthropic API compatible endpoints. The service acts as a proxy, converting API requests to browser interactions with the AI Studio web interface.

✨ Features

  • 🔄 API Compatibility: Compatible with OpenAI API, Gemini API, and Anthropic API formats
  • 🌐 Web Automation: Uses browser automation to interact with AI Studio web interface
  • 👥 Multi-Account Support: Support multiple Google accounts logged in simultaneously for fast switching without re-login
  • 🔧 Tool Calls Support: OpenAI, Gemini, and Anthropic APIs all support Tool Calls (Function Calling)
  • 📝 Model Support: Access to various Gemini models through AI Studio, including image generation and TTS (text-to-speech) models
  • 🎨 Homepage Display Control: Provides a visual web console with account management, VNC login, and more

🚀 Quick Start

💻 Run Directly (Windows / macOS / Linux)

  1. Clone the repository:

    git clone https://github.com/iBUHub/AIStudioToAPI.git
    cd AIStudioToAPI
  2. Run the setup script:

    npm run setup-auth

    For auto-fill and non-interactive examples, see the Account Auto-fill section.

    This script will:

    • Automatically download the Camoufox browser (a privacy-focused Firefox fork)
    • Launch the browser, open AI Studio, and guide you through manual login
    • Save your authentication credentials locally (auth files are stored in /configs/auth)

    💡 Tip: If downloading the Camoufox browser fails or takes too long, you can manually download it from here, and set the environment variable CAMOUFOX_EXECUTABLE_PATH to the path of the browser executable (both absolute and relative paths are supported).

  3. Configure Environment Variables (Optional):

    Copy .env.example in the root directory to .env, and modify settings in .env as needed (e.g., port, API Key).

  4. Start the service:

    npm start

    The API server will be available at http://localhost:7860

    After the service starts, you can access http://localhost:7860 in your browser to open the web console homepage, where you can view account status and service status. Request usage statistics are persisted locally at /data/usage-stats.jsonl.

  5. Update to the latest version (for existing local deployments):

    git pull
    npm install

Note: Running directly does not support adding accounts via VNC online. You need to use the npm run setup-auth script to add accounts. VNC login is only available in Docker deployments.

🐋 Docker Deployment

Deploy using Docker without pre-extracting authentication credentials.

🚢 Step 1: Deploy Container

🎮️ Option 1: Docker Command
docker run -d \
  --name aistudio-to-api \
  -p 7860:7860 \
  -v /path/to/auth:/app/configs/auth \
  -v /path/to/data:/app/data \
  -e API_KEYS=your-api-key-1,your-api-key-2 \
  -e TZ=America/New_York \
  --restart unless-stopped \
  ghcr.io/ibuhub/aistudio-to-api:latest

💡 Tip: If ghcr.io is slow or unavailable, you can use the Docker Hub image: ibuhub/aistudio-to-api:latest.

Parameters:

  • -p 7860:7860: API server port (if using a reverse proxy, strongly consider 127.0.0.1:7860)
  • -v /path/to/auth:/app/configs/auth: Mount directory containing auth files
  • -v /path/to/data:/app/data: Mount persistent data directory for usage statistics (/app/data/usage-stats.jsonl)
  • -e API_KEYS: Comma-separated list of API keys for authentication
  • -e TZ=America/New_York: Timezone for logs (optional, defaults to system timezone)
📦 Option 2: Docker Compose

Create a docker-compose.yml file:

name: aistudio-to-api

services:
  app:
    image: ghcr.io/ibuhub/aistudio-to-api:latest
    container_name: aistudio-to-api
    ports:
      # API server port (if using a reverse proxy, strongly consider `127.0.0.1:7860`)
      - 7860:7860
    restart: unless-stopped
    volumes:
      # Mount directory containing auth files
      - ./auth:/app/configs/auth
      # Mount persistent data directory for usage statistics
      - ./data:/app/data
    environment:
      # Comma-separated list of API keys for authentication
      API_KEYS: your-api-key-1,your-api-key-2
      # Timezone setting (optional, defaults to system timezone)
      TZ: America/New_York
🛠️ Option 3: Build from Source

If you prefer to build the Docker image yourself, you can use the following commands:

  1. Build the image:

    docker build -t aistudio-to-api .
  2. Run the container:

    docker run -d \
      --name aistudio-to-api \
      -p 7860:7860 \
      -v /path/to/auth:/app/configs/auth \
      -v /path/to/data:/app/data \
      -e API_KEYS=your-api-key-1,your-api-key-2 \
      -e TZ=America/New_York \
      --restart unless-stopped \
      aistudio-to-api

🔑 Step 2: Account Management

After deployment, you need to add Google accounts using one of these methods:

Method 1: VNC-Based Login (Recommended)

  • Access the deployed service address in your browser (e.g., http://your-server:7860) and click the "Add User" button
  • You'll be redirected to a VNC page with a browser instance
  • Log in to your Google account, then click the "Save" button after login is complete
  • The account will be automatically saved as auth-N.json (N starts from 0)

Method 2: Upload Auth Files

  • Run npm run setup-auth on your local machine to generate auth files (refer to steps 1 and 2 of Run Directly), the auth files are in /configs/auth
  • In the web console, click "Upload Auth" to upload the auth JSON file, or manually upload to the mounted /path/to/auth directory

💡 Tip: You can also download auth files from an existing container and upload them to a new container. Click the "Download Auth" button for the corresponding account in the web console to download the auth file.

⚠ Environment variable-based auth injection is no longer supported.

🌐 Step 3 (Optional): Nginx Reverse Proxy

If you need to access via a domain name or want unified management at the reverse proxy layer (e.g., configure HTTPS, load balancing, etc.), you can use Nginx.

📖 For detailed Nginx configuration instructions, see: Nginx Reverse Proxy Configuration

🐾 Claw Cloud Run Deployment

Claw Cloud Run announcement: Claw Cloud Run has announced that it will discontinue its product and related services. All services will be discontinued on May 11, 2026, 00:00 UTC; export or back up your data before then. See the official announcement for details: Announcement

Deploy directly on Claw Cloud Run, a fully managed container platform.

📖 For detailed deployment instructions, see: Deploy on Claw Cloud Run

🦓 Zeabur Deployment

Zeabur announcement: Since March 15, 2026, Zeabur has stopped allowing new projects to be created on the Shared Cluster. Services already running on the Shared Cluster are not affected. See the official changelog for details: Announcement

📖 For the legacy deployment guide, see: Deploy on Zeabur

📡 API Usage

🤖 OpenAI-Compatible API

This endpoint is processed and then forwarded to the official Gemini API format endpoint.

  • GET /v1/models: List models.
  • POST /v1/chat/completions: Chat completion and image generation, supports non-streaming, real streaming, and fake streaming.
  • POST /v1/responses: OpenAI Responses API compatible endpoint for conversation generation, does not support image generation, and supports non-streaming, real streaming, and fake streaming.
  • POST /v1/responses/input_tokens: Count input tokens for an OpenAI Responses API request.

♊ Gemini Native API Format

This endpoint is forwarded to the official Gemini API format endpoint.

  • GET /v1beta/models: List available Gemini models.
  • POST /v1beta/models/{model_name}:generateContent: Generate content, images, and speech.
  • POST /v1beta/models/{model_name}:streamGenerateContent: Stream content, image, and speech generation, supports real and fake streaming.
  • POST /v1beta/models/{model_name}:batchEmbedContents: Batch generate text embedding vectors.
  • POST /v1beta/models/{model_name}:predict: Imagen series models image generation.

👤 Anthropic Compatible API

This endpoint forwards requests to the official Gemini API format endpoint.

  • GET /v1/models: List models.
  • POST /v1/messages: Chat message completions, supports non-streaming, real streaming, and fake streaming.
  • POST /v1/messages/count_tokens: Count tokens in the messages.

📖 For detailed API usage examples, see: API Usage Examples

🖥️ Recommended Frontend: AMC WebUI

AMC WebUI is a Local-First Gemini workflow WebUI with multimodal chat, Canvas, file processing, realtime search, code execution, and advanced reasoning. It already supports AIStudioToAPI as a third-party Gemini-compatible backend and can be used as a graphical frontend for this project.

Online demo: https://all-model-chat.pages.dev

Usage:

  • Deploy and start AIStudioToAPI first, and make sure the Gemini native API endpoint is reachable, for example http://localhost:7860/v1beta.
  • In AMC WebUI, go to Settings -> API Configuration, enable "Custom API Configuration", and set the Gemini-compatible Base URL to AIStudioToAPI's /v1beta endpoint.
  • The API Key configured in AMC WebUI should match one of the API_KEYS configured for AIStudioToAPI.

🧰 Configuration

🔧 Environment Variables

📱 Application Configuration

Variable Description Default
API_KEYS Comma-separated list of valid API keys for authentication. 123456
WEB_CONSOLE_USERNAME Username for web console login (optional). If both username and password are set, both are required to login. None
WEB_CONSOLE_PASSWORD Password for web console login (optional). If only password is set, login requires password only. If neither is set, the system falls back to API_KEYS for console login. None
PORT API server port. 7860
HOST Server listening host address. 0.0.0.0
ICON_URL Custom favicon URL for the console. Supports ICO, PNG, SVG, etc. /AIStudio_logo.svg
SECURE_COOKIES Enable secure cookies. true for HTTPS only, false for both HTTP and HTTPS. false
RATE_LIMIT_MAX_ATTEMPTS Maximum failed login attempts allowed within the time window (0 to disable). 5
RATE_LIMIT_WINDOW_MINUTES Time window for rate limiting in minutes. 15
CHECK_UPDATE Enable version update check on page load (false to disable). true
LOG_LEVEL Logging output level. Set to DEBUG for detailed debug logs. INFO
TZ Timezone used for logs and displayed times, for example America/New_York. Defaults to the system timezone when empty. System timezone

🌐 Proxy Configuration

Variable Description Default
INITIAL_AUTH_INDEX Initial authentication index to use on startup. 0
ENABLE_AUTH_UPDATE Whether to enable automatic auth credential updates. Defaults to enabled. The auth file will be automatically updated upon successful login/account switch and every 24 hours. Set to false to disable. true
MAX_RETRIES Maximum number of retries for failed requests (only effective for fake streaming and non-streaming). 3
RETRY_DELAY Delay between retries in milliseconds. 2000
SWITCH_ON_USES Number of requests before automatically switching accounts (0 to disable). 40
FAILURE_THRESHOLD Number of consecutive failures before switching accounts (0 to disable). 3
IMMEDIATE_SWITCH_STATUS_CODES HTTP status codes that trigger immediate account switching (comma-separated, set to empty to disable). 429,503
MAX_CONTEXTS Maximum number of accounts that can be logged in simultaneously. Accounts logged in simultaneously can switch faster without re-login. Higher values consume more memory (approx: 1 account ~700MB, 2 accounts ~950MB, 3 accounts ~1100MB). Set to 0 for unlimited. 1
HTTP_PROXY HTTP proxy address for accessing Google services. None
HTTPS_PROXY HTTPS proxy address for accessing Google services. None
NO_PROXY Comma-separated list of addresses to bypass the proxy. The project automatically bypasses local addresses (localhost, 127.0.0.1 and 0.0.0.0), so manual local bypass configuration is usually not required. None

🗒️ Other Configuration

Variable Description Default
STREAMING_MODE Streaming mode. real for real streaming, fake for fake streaming. real
ENABLE_USAGE_STATS Whether to enable request usage statistics. Defaults to enabled. Set to false to skip loading local stats, skip writing stats, and make /api/usage-stats return an empty payload. true
SAFETY_SETTINGS_THRESHOLD Safety settings level. Official docs: Safety settings OFF
FORCE_THINKING Force enable thinking mode for all requests. false
FORCE_WEB_SEARCH Force enable web search for all requests. false
FORCE_URL_CONTEXT Force enable URL context for all requests. false
CAMOUFOX_EXECUTABLE_PATH Path to the Camoufox browser executable (supports both absolute and relative paths). Only required if manually downloaded. Auto-detected

⚡ Account Auto-fill

To simplify the login process for multiple accounts, you can configure the users.csv file for auto-fill:

  1. Create users.csv in the project root.
  2. Format: email,password,recovery_email,totp_secret (one per line, recovery_email and totp_secret are optional).
  3. Run npm run setup-auth and select the account when prompted.

📖 For detailed configuration instructions, see: Account Auto-fill Guide

💡 Tip: For promptless runs, use npm run setup-auth -- --non-interactive --account 1, or pass --email / --password directly.

💡 Batch add: Use npm run setup-auth-batch -- --headless to add every account in users.csv sequentially.

🧠 Model List Configuration

Edit configs/models.json to customize available models and their settings.

💡 Tip: The thinking parameter reserves the function to be set via the model suffix. It supports setting the thinking level by appending -THINKING_LEVEL or (THINKING_LEVEL) to the model name (THINKING_LEVEL supports high, low, medium, minimal, case-insensitive). For example: gemini-3-flash-preview(minimal) or gemini-3-flash-preview-minimal.

Streaming mode can also be overridden with -real or -fake. This override has higher priority than the system streaming mode, but it only takes effect for streaming requests. For example: gemini-3-flash-preview-fake. When used together with a thinking suffix, the streaming suffix should come after the thinking suffix, for example: gemini-3-flash-preview-minimal-fake or gemini-3-flash-preview(minimal)-real.

Web search can also be forced on by appending -search to the end of the model name. For example: gemini-3-flash-preview-search. When combined with other suffixes, -search must be the final suffix; the full combined order remains thinking -> streaming -> search, for example: gemini-3-flash-preview-minimal-search, gemini-3-flash-preview-real-search, or gemini-3-flash-preview(minimal)-fake-search.

📄 License

This project is a fork of ais2api by Ellinav, and fully adopts the CC BY-NC 4.0 license used by the upstream project. All usage, distribution, and modification activities must comply with all terms of the original license. See the full license text in LICENSE.

🤝 Contributors

Contributors

We would like to thank all developers who have contributed their time, effort, and wisdom to this project.


If you find AIStudioToAPI useful, consider giving it a ⭐️!

Star History Chart