Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Gemini Server

A lightweight FastAPI server that exposes an Anthropic-style /v1/messages API backed by Google's Gemini web interface. This allows you to use Gemini models with any tool that supports the Anthropic API format (like Claude Code, Cursor, Continue.dev, etc.).

✨ Features

  • Anthropic-Compatible API: Use Gemini with tools expecting Anthropic's API format
  • Asynchronous & Fast: Built with FastAPI and Uvicorn for high performance
  • No Official API Key Required: Uses your browser cookies instead of paid API credits
  • Streaming Support: Full SSE (Server-Sent Events) streaming responses
  • Multiple Models: Supports gemini-3-flash and gemini-3-pro
  • Simple Configuration: Easy cookie-based authentication
  • Lightweight: Minimal dependencies

πŸ“‹ Prerequisites

  • Python 3.8+
  • A Google account with access to Gemini
  • Chrome/Firefox browser to extract cookies

πŸ”§ Installation

1. Clone or create the project directory

git clone https://github.com/IMApurbo/gemini_server
cd gemini-server

2. Install dependencies

pip install -r requirements.txt

Or install directly:

pip install fastapi uvicorn gemini-webapi python-multipart

πŸͺ Getting Your Gemini Cookies

The server requires two cookies to authenticate with Gemini:

  1. Open gemini.google.com in Chrome/Firefox

  2. Log in to your Google account

  3. Open Developer Tools:

    • Chrome: F12 or right-click β†’ Inspect
    • Firefox: F12 or right-click β†’ Inspect Element
  4. Find the cookies:

    Chrome:

    • Go to the Application tab
    • Click Cookies β†’ https://gemini.google.com
    • Look for __Secure-1PSID and __Secure-1PSIDTS

    Firefox:

    • Go to the Storage tab
    • Click Cookies β†’ https://gemini.google.com
    • Look for __Secure-1PSID and __Secure-1PSIDTS
  5. Copy the full value of each cookie (they're long strings)

βš™οΈ Configuration

Automatic Setup

On first run, the server will prompt you for the cookies:

python server.py

It will ask:

Gemini cookie '__Secure-1PSID' is not set in config.json.
Enter __Secure-1PSID: [paste your cookie here]
Gemini cookie '__Secure-1PSIDTS' is not set in config.json.
Enter __Secure-1PSIDTS: [paste your cookie here]

Manual Configuration

You can also create a config.json file manually:

{
    "secure_1psid": "YOUR_1PSID_VALUE",
    "secure_1psidts": "YOUR_1PSIDTS_VALUE"
}

Environment Variables

You can customize the server using environment variables:

export SERVER_HOST="0.0.0.0"  # Default: 0.0.0.0
export SERVER_PORT="8765"     # Default: 8765

πŸš€ Running the Server

Start the server

python server.py

You should see output like:

Starting Anthropic-style server...
INFO:     Started server process [12345]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8765

Verify it's working

curl http://localhost:8765/

Response:

{
  "status": "ok",
  "message": "Gemini-server Anthropic-style server is running."
}

🎯 Using with Claude Code

Option 1: Configure Claude Code settings

If Claude Code supports custom API endpoints, add this to your configuration:

{
  "api": {
    "base_url": "http://localhost:8765",
    "api_key": "dummy-key"
  }
}

Option 2: Environment variables

export ANTHROPIC_API_URL="http://localhost:8765/v1/messages"
export ANTHROPIC_API_KEY="dummy-key"

Option 3: Command-line

When running Claude Code, specify the endpoint:

claude --api-url http://localhost:8765 --api-key dummy-key

πŸ“‘ API Endpoints

GET /

Health check endpoint.

GET /v1/models

List available models.

Response:

{
  "data": [
    {"id": "gemini-3-flash", "object": "model"},
    {"id": "gemini-3-pro", "object": "model"}
  ]
}

POST /v1/messages

Send a message to Gemini.

Request:

{
  "model": "gemini-3-flash",
  "messages": [
    {"role": "user", "content": "Hello, how are you?"}
  ],
  "system": "You are a helpful assistant.",
  "stream": false
}

Response (non-streaming):

{
  "id": "msg_abc123def456",
  "type": "message",
  "role": "assistant",
  "model": "gemini-3-flash",
  "content": [
    {
      "type": "text",
      "text": "Hello! I'm doing well, thank you for asking..."
    }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 12,
    "output_tokens": 25
  }
}

πŸ§ͺ Testing the API

Test with curl (non-streaming)

curl -X POST http://localhost:8765/v1/messages \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3-flash",
    "messages": [
      {"role": "user", "content": "What is the capital of France?"}
    ]
  }'

Test with curl (streaming)

curl -X POST http://localhost:8765/v1/messages \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3-flash",
    "messages": [
      {"role": "user", "content": "Tell me a short story"}
    ],
    "stream": true
  }'

Test with Python

import requests

response = requests.post(
    "http://localhost:8765/v1/messages",
    json={
        "model": "gemini-3-flash",
        "messages": [
            {"role": "user", "content": "Explain quantum computing in simple terms"}
        ]
    }
)

print(response.json()["content"][0]["text"])

πŸ› Troubleshooting

Cookie Expiration

If you see errors like:

RuntimeError: Could not find SNlM0e token - your cookies are likely invalid or expired

Your cookies have expired. Simply get fresh cookies from Gemini and update config.json.

Tips for Keeping Cookies Active

  • Stay logged into Google in your browser
  • Visit gemini.google.com occasionally
  • The free tier has usage limits, so use judiciously

Connection Refused

If the server isn't accessible:

  • Check if it's running: ps aux | grep server.py
  • Check the port: netstat -tulpn | grep 8765
  • Try changing the port: export SERVER_PORT=8766

Rate Limiting

Gemini has usage limits. If you hit them, you'll receive errors. Wait a few minutes and try again.

πŸ”’ Security Considerations

  • Local Network: The server binds to 0.0.0.0 by default, making it accessible on your network
  • No Authentication: The server doesn't implement API key validation
  • Cookie Storage: Your cookies are stored in plaintext in config.json
  • Recommendations:
    • Use a firewall to restrict access
    • Change HOST to 127.0.0.1 for local-only access
    • Don't expose the server to the public internet

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     HTTP Request     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client    β”‚  ──────────────────> β”‚   FastAPI App    β”‚
β”‚ (Claude Codeβ”‚   /v1/messages       β”‚  (server.py)     β”‚
β”‚  Cursor,    β”‚                      β”‚                  β”‚
β”‚  etc.)      β”‚                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                               β”‚
                                               β–Ό
                                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                      β”‚  GeminiClient    β”‚
                                      β”‚ (gemini_webapi)  β”‚
                                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                               β”‚
                                               β–Ό
                                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                      β”‚  Gemini Web      β”‚
                                      β”‚  (gemini.google) β”‚
                                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Dependencies

  • fastapi: Web framework
  • uvicorn: ASGI server
  • gemini-webapi: Gemini web client
  • python-multipart: Form data parsing

🀝 Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

πŸ“„ License

MIT License - feel free to use this for any purpose.

πŸ’‘ Use Cases

  • Claude Code: Use Gemini as a replacement for Claude
  • Cursor IDE: Use Gemini with Cursor's AI features
  • Continue.dev: Use Gemini with Continue's coding assistant
  • Custom Tools: Any tool that supports Anthropic's API format
  • Testing: Test Gemini without paying for API credits
  • Development: Develop applications with Gemini's capabilities

πŸ†˜ Support

If you encounter issues:

  1. Check the troubleshooting section
  2. Verify your cookies are correct
  3. Check the logs for error messages
  4. Open an issue on GitHub

πŸ—‘οΈ Uninstalling

To completely remove the server:

# Remove the directory
rm -rf gemini-server

# Or if using pip
pip uninstall fastapi uvicorn gemini-webapi

πŸ“š Additional Resources


Happy Coding! πŸš€

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages