Gemini CLI Setup With a Native-Protocol Base URL
Gemini CLI needs a Gemini-compatible native endpoint
Connecting Gemini CLI through a relay requires more than changing an address. The endpoint must understand Gemini’s native API requests. Gemini CLI provides GOOGLE_GEMINI_BASE_URL specifically to override the Gemini API destination when API-key authentication is selected. This walkthrough uses that mechanism rather than an OpenAI-compatible configuration.
BestRelay, which publishes this guide, is a quality-first API relay that forwards each request to the official model named in it, without substitution. Its coverage includes Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, Grok, and more. For this setup, the important detail is using its native Gemini route.
Step one installs the official Gemini CLI package
Start with Node.js 20 or newer and an available npm installation, following Gemini CLI’s documented runtime requirement. Run the following commands individually in your terminal.
- Check Node.js: node --version
- Check npm: npm --version
- Install Gemini CLI: npm install -g @google/gemini-cli
- Verify installation: gemini --version
The installation command comes from the official Gemini CLI documentation. Record the installed version so you can reproduce the setup or include it in a support request.
Step two prepares your relay key and model
Create a BestRelay account to follow along.
Obtain your relay API key and copy the exact API identifier for an available Gemini model, such as Gemini 2.5 Flash. Use the identifier provided for API requests, not a display label or an identifier guessed from the model’s name. The commands below deliberately use placeholders for these account-specific values.
BestRelay also works with Claude Code, Codex, and any OpenAI SDK, but those integrations do not determine Gemini CLI’s protocol settings. Keep this configuration focused on the native Gemini API.
Step three sets the native Gemini base URL
For Bash or Zsh, execute these assignments separately in the terminal where you will launch Gemini CLI. Replace both placeholders before continuing. Gemini CLI documents GEMINI_API_KEY, GEMINI_MODEL, and GOOGLE_GEMINI_BASE_URL as configuration variables.
For PowerShell, use the equivalent assignments:
- $env:GOOGLE_GEMINI_BASE_URL="https://api.bestapirelay.com"
- $env:GEMINI_API_KEY="REPLACE_WITH_YOUR_BESTRELAY_KEY"
- $env:GEMINI_MODEL="REPLACE_WITH_EXACT_MODEL_ID"
Use the base address as shown, rather than adding an OpenAI chat-completions path. Google’s SDK constructs Gemini request paths and handles API versioning separately; a base URL is not a complete generation endpoint.
Step four selects Gemini API key authentication
Launch Gemini CLI by running gemini. Select Gemini API-key authentication when prompted, rather than Google sign-in or Vertex AI. If an existing installation remembers another authentication method, enter /auth to open the authentication selector. The custom Gemini base URL applies to the API-key authentication path.
Keep the relay credential out of source files, screenshots, and shared terminal transcripts. Do not paste it into a conversational prompt.
Step five tests routing before larger coding tasks
After completing authentication, exit the interactive session and run a small headless request. Bash and Zsh users can execute gemini -m "$GEMINI_MODEL" -p "Reply with exactly READY." PowerShell users should replace "$GEMINI_MODEL" with "$env:GEMINI_MODEL". The prompt flag runs Gemini CLI without its interactive interface.
Confirm the response, then compare the request with any usage records available in your relay account. A successful answer alone does not establish which network destination handled it. Record the selected identifier and check for unexpected additional requests.
Step six checks configuration before repeating failed requests
If the test fails, first recheck the key, exact model identifier, base address, and selected authentication method. After changing environment variables, restart Gemini CLI from the configured terminal. Persist working values in a protected environment configuration only after the minimal test succeeds; Gemini CLI documents shell profiles and environment files for persistence.
Finally, review costs before expanding the workload. BestRelay bills pay-as-you-go using provider-reported token counts, with cache reads and writes priced separately. Members also earn referral commission when invited users top up. Keep those account features separate from technical validation: the essential checks remain the native endpoint, API-key mode, and explicitly selected model.
Want to try the models discussed here? Sign up