Transport for the Gemini API: the x-goog-api-key header, the base URL,
and the one credential resolver.
Per-call credentials. Every typed function takes a trailing
opts: Map, which wins over ctx for that call. Nothing is kept in ctx,
so one run can serve many tenants and parallel flows don't share keys.
api-key: overridesgemini.api.key.base-url: overridesgemini.base.url, for a proxy or gateway. It needsapi-keyinoptstoo: the ctx key is never sent to a per-call URL. A base URL and key both set in ctx work as usual.
The key goes in a header, never the URL, so it can't end up in a logged URL or an error message.
Call records. A key passed in opts is masked in the run's call
records, including inside strings like Bearer ${key}. A key shorter than
8 bytes is masked only as a whole value.
Functions
base-url
fn (): Str
fn (opts: Map): Str
The base URL calls go to: opts.base-url, then the gemini.base.url ctx
variable, then https://generativelanguage.googleapis.com. Set it to
route calls through a proxy or gateway.
Example
::gemini::api/base-url() // => "https://generativelanguage.googleapis.com"
::gemini::api/base-url({base-url: "https://gw.example.com"}) // => "https://gw.example.com"
gemini.base.url
build-headers
fn (resolved: Map, has-body: Bool): Map
Request headers from resolved settings (see resolve), pure:
x-goog-api-key, and Content-Type when there is a body. Exposed for
testing.
Example
::gemini::api/build-headers({api-key: "k"}, false) // {x-goog-api-key: "k"}
::gemini::api/build-headers({api-key: "k"}, true) // {x-goog-api-key: "k", Content-Type: "application/json"}
build-request
fn (method: Str, url: Str, additional-headers: Map, body: Any, resolved: Map): ::hot::http/HttpRequest
The outgoing HttpRequest for resolved settings, pure. additional-headers
override the defaults. An empty or null body sends no body at all.
Exposed for testing.
Example
resolved ::gemini::api/resolve-from({api-key: "k"}, {})
req ::gemini::api/build-request("GET", "https://example.com/v1/models", {}, "", resolved)
req.headers // {x-goog-api-key: "k"}
call
fn (method: Str, path: Str, additional-headers: Map, body: Any, opts: Map): ::hot::http/HttpResponse
Resolve opts and send a request to path under the resolved base URL.
The typed functions in this package go through here.
Example
response ::gemini::api/call("GET", "/v1/models", {}, "", {api-key: ::hot::ctx/get("tenant.gemini.key")})
gemini.api.key, gemini.base.url
call-stream
fn (method: Str, path: Str, additional-headers: Map, body: Any, format: Str, opts: Map): ::hot::http/StreamingHttpResponse
Streaming counterpart to call. Returns a StreamingHttpResponse whose
body is an iterator of parsed SSE or NDJSON events.
Example
response ::gemini::api/call-stream("POST", "/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse", {}, {
contents: [{role: "user", parts: [{text: "Count to 3."}]}]
}, "sse", {})
for-each(response.body, (event) {
println(::gemini::chat/extract-chunk-text(event))
})
gemini.api.key, gemini.base.url
non-blank
fn (value: Any): Any
value, or null when it is null or a blank Str.
Example
::gemini::api/non-blank("key") // "key"
::gemini::api/non-blank(" ") // null
normalize-base-url
fn (url: Str?): Str
Normalize a base URL: trims whitespace, drops trailing slashes, and falls
back to ::gemini/BASE_URL when the value is null or empty.
Example
::gemini::api/normalize-base-url("https://gateway.example.com/") // => "https://gateway.example.com"
::gemini::api/normalize-base-url(null) // => "https://generativelanguage.googleapis.com"
request
fn (method: Str, url: Str): ::hot::http/HttpResponse
fn (method: Str, url: Str, additional-headers: Map): ::hot::http/HttpResponse
fn (method: Str, url: Str, additional-headers: Map, body: Any): ::hot::http/HttpResponse
fn (method: Str, url: Str, additional-headers: Map, body: Any, api-key: Str): ::hot::http/HttpResponse
fn (method: Str, url: Str, additional-headers: Map, body: Any, opts: Map): ::hot::http/HttpResponse
Make an authenticated HTTP request to the Gemini API. Automatically sends the API key from context in the x-goog-api-key header. Additional headers can be provided to merge/override defaults.
Supports 2 to 5 arguments: method and URL (minimum), plus optional headers, body, and an explicit API key or opts Map (see resolve).
Example
response ::gemini::api/request("GET", `${::gemini/BASE_URL}/v1/models`)
response.status // => 200
response.body // => {models: [...]}
With body
response ::gemini::api/request("POST", `${::gemini/BASE_URL}/v1/models/gemini-2.0-flash:generateContent`, {}, {
contents: [{role: "user", parts: [{text: "Say hello"}]}]
})
response.body.candidates
gemini.api.key
request-stream
fn (method: Str, url: Str, additional-headers: Map, body: Any): ::hot::http/StreamingHttpResponse
fn (method: Str, url: Str, additional-headers: Map, body: Any, format: Str): ::hot::http/StreamingHttpResponse
fn (method: Str, url: Str, additional-headers: Map, body: Any, format: Str, api-key: Str): ::hot::http/StreamingHttpResponse
fn (method: Str, url: Str, additional-headers: Map, body: Any, format: Str, opts: Map): ::hot::http/StreamingHttpResponse
Make a streaming HTTP request to the Gemini API. Returns a response with an iterator body for consuming SSE or NDJSON streams.
Supports 4 to 6 arguments: method, URL, headers, body (minimum), plus optional format and an explicit API key or opts Map (see resolve).
Example
url `${::gemini/BASE_URL}/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse`
body {contents: [{role: "user", parts: [{text: "Count to 3."}]}]}
response ::gemini::api/request-stream("POST", url, {}, body, "sse")
for-each(response.body, (event) {
println(::gemini::chat/extract-chunk-text(event))
})
gemini.api.key
resolve
fn (): Map
fn (opts: Map): Map
Settings for one call: opts over ctx over defaults (see resolve-from).
With an api-key in opts, the ctx key is not read, and a blank one is
an Err rather than a fallback to ctx. Nothing is written to ctx, and the
key from opts is masked in the run's call records.
Example
::gemini::api/resolve({api-key: ::hot::ctx/get("tenant.gemini.key")}).base-url // => "https://generativelanguage.googleapis.com"
gemini.api.key, gemini.base.url
resolve-from
fn (opts: Map, ctx-values: Map): Map
The one resolver, pure: settings for a call from opts first, then
ctx-values (a Map of context variables), then defaults. Returns
{api-key, base-url}, or an Err when no API key is set, when api-key in opts is
blank (never a fallback to ctx, so a tenant with a missing key can't run
on the project's key), or when opts has a base-url but no api-key
(the ctx key is never sent to a per-call URL). Exposed for
testing; resolve feeds it the context.
Example
::gemini::api/resolve-from({api-key: "k"}, {}).base-url // => "https://generativelanguage.googleapis.com"
Types
HttpError
HttpError type {
status: Int,
headers: Map,
body: Any
}