Gemini CLI API proxy setup with 59API: exact settings
Why Gemini CLI connections usually stall
Most Gemini CLI connection problems are not really credential problems. They are protocol problems. Gemini CLI speaks to Gemini models through Google's native protocol, so a configuration copied from an OpenAI-style client tends to fail even when the API key is perfectly valid. Pointing the client at an OpenAI-compatible chat endpoint is the most common mistake, and it usually returns a confusing error instead of a clear explanation.
That is why a good API proxy setup starts with one simple rule: match the client to the model family. Gemini models use Google's native protocol only, and they run in Gemini CLI. They are not OpenAI-compatible, so they do not belong on the OpenAI-style base URL and chat endpoint that 59API provides for other families. If you keep that distinction in mind, the rest of the configuration becomes short and predictable.
This guide walks through connecting Gemini CLI to 59API, an independent third-party AI API relay, using the exact settings the client expects. You will also see current per-token rates for several Gemini models, so you can decide what to run before spending anything. The goal is a working connection in a few minutes, with no guesswork about URLs, headers, or endpoints.
- One API host:
https://api.59api.com - One account and one API key
- Prepaid balance, charged per request by real token usage
What 59API gives you before you connect anything
59API is an independent third-party AI API relay. One account and one API key give you one API host, https://api.59api.com, for Claude, GPT, Gemini, DeepSeek and other models. You are not juggling separate providers, separate dashboards, or separate billing systems for every family you want to experiment with.
The billing model is straightforward. You top up a prepaid balance, and each request is charged by real token usage. There is no monthly fee, and your balance never expires. Top-ups are available at ¥10, ¥50, ¥100, ¥200, ¥500 and ¥1000, paid by WeChat Pay or bank card, and invoices are available. Billing is in CNY, with USD figures shown at ¥7.2 = $1, which makes the rates below easy to compare with your own budget.
One important detail: client rules differ by model family, and 59API does not blur them. Gemini models run through Gemini CLI using Google's native protocol. Claude models work only through the genuine Claude Code or Claude Desktop client (Claude Code 2.1.285 or newer), never through the OpenAI SDK, cURL or other clients. Codex, OpenAI SDKs, cURL and OpenAI-compatible clients such as Cursor, Cherry Studio, Cline and OpenCode work with GPT, DeepSeek and other non-Claude, non-Gemini models only.
For a Gemini CLI user, your job is narrow and clean: one base URL, one key, and the correct model id.
Connect Gemini CLI to 59API step by step
Setting up Gemini CLI through 59API takes two environment variables and a working key. Here is the exact configuration, in the order I would do it myself.
- Create your 59API account and add a top-up. Pick an amount from ¥10, ¥50, ¥100, ¥200, ¥500 or ¥1000 and pay by WeChat Pay or bank card. Because requests are charged by real token usage, a small starting balance is enough to test the connection.
- Copy your API key. Your key lives in your 59API account. Keep it somewhere you can paste it without retyping it.
- Set the Gemini base URL. Use
GOOGLE_GEMINI_BASE_URL=https://api.59api.com. Write the host exactly as shown. - Set the Gemini key. Use
GEMINI_API_KEY=YOUR_59API_KEY, replacingYOUR_59API_KEYwith the real key from your account. - Start Gemini CLI. Launch the client with those two variables present in its environment, then pick a Gemini model id from the list below.
Two details cause most of the friction here. First, the value for GOOGLE_GEMINI_BASE_URL is the 59API host itself, exactly https://api.59api.com. Second, the key goes in GEMINI_API_KEY, not in a variable named for an OpenAI client. If your shell profile still contains an older configuration pointing somewhere else, remove it so it cannot override what you just set.
One more point worth repeating: the OpenAI-style base URL and chat endpoint on 59API are for GPT, DeepSeek and other non-Claude, non-Gemini models. Gemini CLI does not use that route. It uses the native Gemini path, which is why the two environment variables above are all you need.
Choosing a Gemini model and what it costs
Once the client is connected, pick the model that matches the task. 59API lists Gemini pricing per 1 million tokens, with input, output and cache read shown separately. Here are four current Gemini rates to start from.
- Gemini 2.5 Flash (
gemini-2.5-flash): input $0.2083 (¥1.5), output $1.67 (¥12), cache read $0.0208 (¥0.15). - Gemini 3.1 Pro (
gemini-3.1-pro): input $1.39 (¥10), output $8.06 (¥58), cache read $0.1389 (¥1). - Gemini 3.8 Flash (
gemini-3.8-flash): input $1.11 (¥8), output $5 (¥36), cache read $0.1111 (¥0.8). - Gemini 4 Argon (
gemini-4-argon): input $1.39 (¥10), output $6.67 (¥48), cache read $0.0694 (¥0.5).
The pattern is easy to read. Flash-class models keep both input and output costs low, which suits everyday prompts, quick iterations and exploratory work. Pro-class models cost more per million tokens, and output is priced well above input across the range, so long generations are where the difference shows up.
Cache read pricing matters too if you reuse a large fixed context. On 59API, cache reads are billed at their own rate, which is lower than the input rate for every Gemini model listed above. If your workflow sends the same material repeatedly, that is worth keeping in mind.
Because billing is based on actual token usage rather than a subscription, the practical way to control spend is simple: pick the model that fits the job, keep the context lean, and watch the balance in your 59API account.
Verify the connection and fix what usually breaks
Send one short prompt first. A small test tells you far more than a long task, and it keeps the cost of debugging low. If the request succeeds, you can move straight to real work. If it fails, work through this short list in order.
- Check the base URL spelling. It should read
https://api.59api.com, with no extra path segments appended. - Check the variable names. They are
GOOGLE_GEMINI_BASE_URLandGEMINI_API_KEY, and they must be present in the environment that launches Gemini CLI. - Check the key value. Make sure the placeholder was actually replaced, with no quotes or spaces carried over from the copy.
- Check the protocol assumption. Gemini models use Google's native protocol only. If you are pointing the client at the OpenAI-compatible chat endpoint, switch back to the Gemini CLI configuration above.
- Check the model id. Copy it exactly, including the dot and dash pattern in ids such as
gemini-3.1-pro.
Once a test request returns, check your 59API balance. The deduction should reflect the real token usage of that request. Since there is no monthly fee and the balance never expires, you can top up when it suits you instead of keeping a subscription alive.
That is the whole setup: one account, one key, one host, two environment variables, and a Gemini model id. Once it works, you can focus on the prompt instead of the plumbing.