aistudio-to-api

repository·main·Indexed 23 days ago

https://github.com/ibuhub/aistudiotoapi

A proxy tool that converts OpenAI, Gemini, and Anthropic API requests into browser-based interactions with Google AI Studio's 'Build App' interface. It enables the use of Gemini models via standard API protocols using browser automation, featuring multi-account support, tool calling, and a web console for account management.

Tokens
19.2K
Snippets
22
Records
92
Agent score
77%

What's inside aistudio-to-api

  1. Overview of Google AI Studio Build App to API Adapter

    main

    This tool wraps the Google AI Studio Build App web interface into an API service that is compatible with OpenAI, Gemini, and Anthropic API formats. It acts as a proxy, converting API requests into browser interactions with the AI Studio Build App interface using browser automation technology.

    Key Features:

    • API Compatibility: Supports OpenAI, Gemini, and Anthropic API formats.
    • Web Automation: Uses browser automation to interact with AI Studio Build.
    • Multi-account Support: Allows multiple Google accounts to be logged in simultaneously with fast switching.
    • Tool Calling Support: Supports Function Calling across OpenAI, Gemini, and Anthropic interfaces.
    • Model Support: Access various Gemini models, including image generation and TTS (Text-to-Speech) models.
    • Web Console: Provides a visual dashboard for account management and VNC-based login.
  2. License information for AIStudioToAPI

    main
    AIStudioToAPI is developed based on a branch of ais2api (Author: Ellinav). It follows the CC BY-NC 4.0 license used by the upstream project. Any use, distribution, or modification must comply with the terms of the original license. For full details, refer to the LICENSE file in the repository.
  3. Understand 2FA and recovery email limitations

    main

    The auto-fill script has specific capabilities and limitations regarding Google's security challenges:

    • Standard TOTP: Supported via the --totp-secret flag or the totp_secret column in users.csv. This works for apps like Google Authenticator or Aegis.
    • Unsupported 2FA: The script cannot automatically handle SMS codes, Google Prompts, passkeys, or CAPTCHAs. These must be completed manually in the browser.
    • Recovery Email: If Google presents a recovery email confirmation challenge, the script can auto-fill it using the recovery_email field from the CSV or the --recovery-email flag.
    • Terms of Service: For new accounts, the script attempts to automatically click buttons like I agree, Agree, Continue, or their Chinese equivalents to handle the first-run terms dialog.
  4. Customize Models in configs/models.json

    main

    Edit configs/models.json to define available models. You can override model behavior using specific suffixes in the model name:

    Suffix Order

    The combined order for suffixes must be: thinking -> streaming -> built-in tools.

    Supported Suffixes

    • Thinking: -THINKING_LEVEL or (THINKING_LEVEL) where level is high, low, medium, or minimal (e.g., gemini-3-flash-preview(minimal)).
    • Streaming: -real or -fake (e.g., gemini-3-flash-preview-minimal-fake).
    • Built-in Tools: -search (web search) and -code (code execution).

    Example Combined Suffixes:

    • gemini-3-flash-preview-minimal-search (Thinking + Search)
    • gemini-3-flash-preview(minimal)-fake-search-code (Thinking + Fake Streaming + Search + Code)
  5. Configure Models and Feature Suffixes

    main

    Custom models and their behaviors can be configured in configs/models.json. You can also override settings by appending suffixes to the model name.

    Suffix Order

    When using multiple suffixes, they must follow this specific order: Thinking $\rightarrow$ Streaming $\rightarrow$ Built-in Tools.

    Supported Suffixes

    1. Thinking Level: -THINKING_LEVEL or (THINKING_LEVEL). Supported values: high, low, medium, minimal.
    2. Streaming Mode: -real or -fake. (Note: This suffix has higher priority than the system-wide STREAMING_MODE but only affects streaming requests).
    3. Built-in Tools:
      • -search: Forces web search.
      • -code: Forces code execution.

    Examples

    • gemini-3-flash-preview(minimal)-fake-search-code (Thinking + Fake Streaming + Search + Code)
    • gemini-3-flash-preview-minimal-search (Thinking + Search)
    • gemini-3-flash-preview-real-code (Real Streaming + Code)
  6. Enable Nginx Configuration

    main

    After creating your Nginx configuration file in /etc/nginx/sites-available/aistudio-api, follow these steps to enable and apply it:

    1. Create a symbolic link to the sites-enabled directory.
    2. Test the configuration for syntax errors.
    3. Restart the Nginx service.
    # Create symbolic link to enable site
    sudo ln -s /etc/nginx/sites-available/aistudio-api /etc/nginx/sites-enabled/
    
    # Test if configuration is correct
    sudo nginx -t
    
    # Restart Nginx
    sudo systemctl restart nginx
  7. Configure Nginx as a Reverse Proxy

    main

    To access the aistudio-to-api service via a domain name or to enable HTTPS and load balancing, use Nginx as a reverse proxy.

    When configuring the location / block, ensure you include the following critical settings:

    • client_max_body_size 0; to allow large request bodies.
    • proxy_pass http://127.0.0.1:7860; to point to the service container.
    • proxy_buffering off; to support streaming AI responses.
    • WebSocket support (using Upgrade and Connection headers) to enable VNC access.
    • Extended timeout settings (proxy_connect_timeout, proxy_send_timeout, proxy_read_timeout) set to 600s to accommodate long-running AI requests.
    server {
        listen 80;
        listen [::]:80;  # IPv6 support
        server_name your-domain.com;  # Replace with your domain
    
        # For HTTPS, uncomment the following lines and configure SSL certificates
        # listen 443 ssl http2;
        # listen [::]:443 ssl http2;  # IPv6 HTTPS
        # ssl_certificate /path/to/your/certificate.crt;
        # ssl_certificate_key /path/to/your/private.key;
    
        # Client request body size limit (0 = unlimited)
        client_max_body_size 0;
    
        location / {
            # Reverse proxy to Docker container
            proxy_pass http://127.0.0.1:7860;
    
            # Critical: Pass real client IP
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
            # Other necessary proxy headers
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-Proto $scheme;
    
            # Timeout settings (adapted for long-running AI requests)
            proxy_connect_timeout 600s;
            proxy_send_timeout 600s;
            proxy_read_timeout 600s;
    
            # Disable buffering to support streaming responses
            proxy_buffering off;
    
            # WebSocket support (required to access VNC)
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }
    }
  8. Enable Fixed Proxy per Account

    main

    You can assign specific HTTP proxies to specific accounts by creating a proxylist.txt file in the project root.

    How it works

    1. Add one proxy per line to proxylist.txt. Supported formats:
      • user:pass@ip:port
      • ip:port:user:pass
      • ip:port
      • http://user:pass@ip:port
    2. When the service starts, it maps each account to an available proxy from the list and saves this in proxy_mapping.json.
    3. Accounts will reuse their assigned proxy as long as they and the proxy remain in the configuration.
    4. If you have more accounts than proxies, unassigned accounts will fail to start.

    Proxy Bypass

    Fixed proxies follow the same bypass rules as HTTP_PROXY. You can use NO_PROXY to define addresses that should not use the proxy.

    NO_PROXY=internal.example.com,10.0.0.0/8
  9. Configure Nginx for Multi-layer Proxying

    main

    If your architecture involves multiple Nginx proxies (e.g., Client $\rightarrow$ Public Gateway $\rightarrow$ Internal Gateway $\rightarrow$ App), you must handle the X-Real-IP header carefully to avoid losing the original client IP.

    • Outermost Public Gateway: Use proxy_set_header X-Real-IP $remote_addr; to capture the actual client IP.
    • Inner Proxies: Do NOT use $remote_addr. Instead, pass through the existing header using proxy_set_header X-Real-IP $http_x_real_ip;.
    # Inner Nginx (internal gateway) configuration example
    location / {
        proxy_pass http://127.0.0.1:7860;
    
        # Critical: Pass through upstream X-Real-IP, do NOT override with $remote_addr
        proxy_set_header X-Real-IP $http_x_real_ip;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
        # Other necessary proxy headers
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
    
        # Timeout settings
        proxy_connect_timeout 600s;
        proxy_send_timeout 600s;
        proxy_read_timeout 600s;
    
        # Disable buffering
        proxy_buffering off;
    
        # WebSocket support (required to access VNC)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }
  10. Prepare a users.csv file for auto-fill

    main

    To simplify the Google login process, you can use a users.csv file located in the project root. This file allows the script to automatically fill in credentials for multiple accounts.

    CSV Format:

    email,password,recovery_email,totp_secret
    your-email-1@gmail.com,your-password-1,recovery-1@example.com,BASE32TOTPSECRET1
    your-email-2@gmail.com,your-password-2,,

    Field Details:

    • Account Identification: The script automatically identifies the account column by looking for the @ symbol. The header is optional.
    • recovery_email: Used to handle Google's recovery email challenges.
    • totp_secret: Used for standard TOTP-based Two-Factor Authentication (2FA).
    WARNING

    The users.csv file contains plain-text passwords. Ensure your environment is secure and never share this file.

  11. Deploy with Docker

    main

    Docker deployment is recommended as it allows for VNC-based account management via the Web Console.

    Using Docker CLI

    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=Asia/Shanghai \
      --restart unless-stopped \
      ghcr.io/ibuhub/aistudio-to-api:latest

    Using 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:
          - 7860:7860
        restart: unless-stopped
        volumes:
          - ./auth:/app/configs/auth
          - ./data:/app/data
        environment:
          API_KEYS: your-api-key-1,your-api-key-2
          TZ: Asia/Shanghai

    Parameter Reference

    • -p 7860:7860: API server port. If using a reverse proxy, it is highly recommended to use 127.0.0.1:7860.
    • -v /path/to/auth:/app/configs/auth: Mount directory containing authentication files.
    • -v /path/to/data:/app/data: Mount directory for persisting usage statistics (/app/data/usage-stats.jsonl).
    • -e API_KEYS: List of API keys for authentication, separated by commas.
    • -e TZ: Timezone setting (e.g., Asia/Shanghai).
  12. Manage Google accounts in Docker deployment

    main

    After deploying via Docker, you must add Google accounts using one of these two methods:

    1. Access the service URL in your browser (e.g., http://your-server:7860).
    2. Click the "Add User" button.
    3. You will be redirected to a VNC page with a browser instance. Log in to your Google account.
    4. Once logged in, click the "Save" button. The account will be saved as auth-N.json (where N starts from 0).

    Method 2: Upload Auth Files

    1. Generate auth files on your local machine using npm run setup-auth (as described in the local run guide).
    2. In the web console, click "Upload Auth" to upload the JSON file, or manually place the file in the mounted /app/configs/auth directory.