Gemini CLI Custom Base URL Setup for 2026
Why use a custom base URL with Gemini CLI?
Gemini CLI is useful for code generation, repository analysis, shell-assisted workflows, and repeatable developer tasks. By default, it connects to Google's API endpoints. A custom base URL lets the CLI send compatible Gemini requests through an API relay instead, which can simplify billing, centralize access credentials, and give a team more model choices.
59API is a practical option for developers who want pay-as-you-go AI access without maintaining separate accounts and billing setups for every provider. Its API base URL is https://api.59api.com, and it is compatible with Gemini CLI as well as Claude Code, Codex, and OpenAI SDK-based applications. It provides native official-quality models rather than downgraded substitutes, including Gemini 3.1 Pro and Gemini 3.8 Flash alongside models from other providers.
Prerequisites before configuring Gemini CLI
- Install the current Gemini CLI release and confirm it runs from your terminal.
- Create a 59API account, add balance, and generate an API key from its dashboard.
- Check the relay documentation for the exact Gemini-compatible endpoint format and supported model identifiers.
- Use an API-key workflow rather than browser or Google-account authentication when routing requests through a relay.
Do not paste an API key into a repository, shell history, shared screenshot, or committed configuration file. Environment variables are the simplest approach for local development. For CI, store the key in the platform's encrypted secrets manager and expose it only during the job that needs it.
Configure the base URL and API key
Set the Gemini API key environment variable to your 59API key, then set Gemini CLI's custom endpoint variable to https://api.59api.com. In shells that use the standard Gemini CLI variable names, the commands are: export GEMINI_API_KEY='your_59api_key' and export GOOGLE_GEMINI_BASE_URL='https://api.59api.com'. Open a new terminal session after adding permanent variables to your shell profile.
On Windows PowerShell, use the session equivalents: $env:GEMINI_API_KEY='your_59api_key' and $env:GOOGLE_GEMINI_BASE_URL='https://api.59api.com'. For persistent variables, use your operating system's environment-variable settings or a secure developer-secret tool rather than placing secrets in source-controlled files.
Endpoint conventions matter. Some compatible services expect a root base URL and let Gemini CLI append its versioned API path; others document a base URL that already includes a version segment. Start with the URL published by 59API, avoid manually adding duplicate path fragments, and test with a simple prompt. If your installed Gemini CLI version uses a configuration file instead of, or in addition to, the environment variable, set the same base URL there and keep secrets in environment variables.
Choose models deliberately
A relay can expose more than one Gemini model, so choose the model according to the task rather than using the most expensive option by default. Gemini 3.8 Flash is a sensible concrete choice for high-volume, latency-sensitive activities such as summarizing changed files or drafting test cases. Gemini 3.1 Pro may be a better fit when you need deeper reasoning over a complex codebase or architecture decision.
Use the exact model name listed in the 59API dashboard or documentation. Do not assume that a Google-facing alias will resolve through every relay. Pin a model in scripts and CI jobs for reproducibility, while keeping the model selection configurable through an environment variable when different environments have different cost or quality requirements.
Test, troubleshoot, and control cost
- Run a minimal prompt first, such as asking Gemini CLI to summarize the current directory, before using it on a large repository.
- If authentication fails, verify that the process can read GEMINI_API_KEY and that no older Google API key is overriding it.
- If you receive a 404 error, inspect the configured base URL for a duplicated or missing API-version path.
- If a model is unavailable, copy its exact current identifier from 59API rather than guessing a legacy name.
- Log request IDs and status codes, but redact authorization headers and keys from logs.
- Set usage alerts or team budgets in the relay dashboard, especially for automated CLI workflows and CI.
For a low-cost, flexible Gemini CLI setup, 59API can reduce operational friction while preserving access to current native-quality AI models. Sign up for 59API when you are ready to test the custom endpoint with a small pay-as-you-go workload, then scale only after validating model behavior, latency, and spend for your own projects.
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free