Transport for the OpenAI API and OpenAI-compatible servers: bearer auth, the base URL, the one credential resolver, and explicit endpoints.
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: overridesopenai.api.key. Present but null or blank is an Err, never a fallback to ctx.base-url: overridesopenai.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.endpoint: anEndpointto use instead, on its own. This is the explicit form: it reads no ctx, and a keylessEndpointsends noAuthorizationheader, which is how local servers work.
resolve always returns an Endpoint, and every request goes out through
one, so the two forms can't drift apart.
Full URLs. A path or url that is a full URL gets the key only when
it is on the call's base URL, or when opts carries its own api-key. A
URL whose host can't be read safely (userinfo, backslashes, control
characters) is always refused.
Call records. A key passed in opts or an Endpoint 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. Mark a credential
you put in Endpoint.headers yourself with ::hot::ctx/mark-secret.
Functions
base-url
fn (): Str
fn (opts: Map): Str
The base URL calls go to: opts.base-url, then the openai.base.url ctx
variable, then https://api.openai.com/v1 (::openai/BASE_URL). A
trailing slash is dropped, so https://gateway.example.com/v1/ works too.
Set it to route the whole package through a gateway or proxy that
speaks the OpenAI API. To call several OpenAI-compatible servers from
one program, build an Endpoint per server instead.
Example
::openai::api/base-url() // => "https://api.openai.com/v1"
::openai::api/base-url({base-url: "https://gw.example.com/v1"}) // => "https://gw.example.com/v1"
openai.base.url
build-endpoint-request
fn (ep: Endpoint, method: Str, path: Str, additional-headers: Map?, body: Any): ::hot::http/HttpRequest
Build (without sending) the HttpRequest for an Endpoint call, or an Err
when path is a full URL target-url refuses. path is joined onto the
endpoint's base URL. Exposed for testing; endpoint-request sends it.
Example
req ::openai::api/build-endpoint-request(
::openai::api/endpoint("http://localhost:11434/v1"),
"POST", "/chat/completions", {}, {model: "llama3.2", messages: []}
)
req.url // => "http://localhost:11434/v1/chat/completions"
build-request
fn (ep: Endpoint, method: Str, url: Str, additional-headers: Map?, body: Any): ::hot::http/HttpRequest
The outgoing HttpRequest to the final url with ep's credentials and
headers, pure. A Map or typed record body is sent as JSON; Bytes as-is;
"" or null sends no body. Exposed for testing.
Example
ep ::openai::api/endpoint("http://localhost:11434/v1")
req ::openai::api/build-request(ep, "POST", "http://localhost:11434/v1/embeddings", {}, {model: "m"})
req.method // "POST"
req.body // "{\"model\":\"m\"}"
default-endpoint
fn (): Endpoint
The ctx-configured OpenAI endpoint: openai.base.url (default
https://api.openai.com/v1) with the openai.api.key key. An Err when no
key is set. The same as resolve({}).
Example
ep ::openai::api/default-endpoint()
ep.base-url // => "https://api.openai.com/v1"
openai.api.key, openai.base.url
endpoint
fn (base-url: Str?): Endpoint
fn (base-url: Str?, api-key: Str?): Endpoint
fn (base-url: Str?, api-key: Str?, headers: Map?): Endpoint
Build an explicit Endpoint from a base URL, an optional API key and
optional extra headers. Reads no ctx. The key is masked in the run's call
records. A blank base URL is an Err, so a key can't end up at a default.
Example
local ::openai::api/endpoint("http://localhost:11434/v1")
groq ::openai::api/endpoint("https://api.groq.com/openai/v1", ::hot::ctx/get("groq.api.key"))
router ::openai::api/endpoint("https://openrouter.ai/api/v1", ::hot::ctx/get("openrouter.api.key"), {
"HTTP-Referer": "https://example.com",
"X-OpenRouter-Title": "Example App"
})
endpoint-headers
fn (ep: Endpoint, additional-headers: Map?, json-body: Bool): Map
The headers an Endpoint request sends: Authorization: Bearer <key>
when the endpoint has a non-blank key, Content-Type: application/json
when json-body is true, then the endpoint's own headers, then
additional-headers (later entries win).
Example
ep ::openai::api/endpoint("http://localhost:11434/v1")
::openai::api/endpoint-headers(ep, {X-Request-Id: "r1"}, true) // {Content-Type: "application/json", X-Request-Id: "r1"}
endpoint-request
fn (ep: Endpoint, method: Str, path: Str): ::hot::http/HttpResponse
fn (ep: Endpoint, method: Str, path: Str, additional-headers: Map): ::hot::http/HttpResponse
fn (ep: Endpoint, method: Str, path: Str, additional-headers: Map, body: Any): ::hot::http/HttpResponse
Send a request to an explicit Endpoint. path is relative to the
endpoint's base URL (e.g. /chat/completions). Reads no ctx, so it
works against keyless local servers and any number of providers.
Example
ep ::openai::api/endpoint("http://localhost:11434/v1")
response ::openai::api/endpoint-request(ep, "GET", "/models")
response.status // => 200
endpoint-request-stream
fn (ep: Endpoint, method: Str, path: Str, additional-headers: Map, body: Any): ::hot::http/StreamingHttpResponse
fn (ep: Endpoint, method: Str, path: Str, additional-headers: Map, body: Any, format: Str): ::hot::http/StreamingHttpResponse
Send a streaming request (SSE by default) to an explicit Endpoint.
Returns a StreamingHttpResponse whose body iterates events.
Example
ep ::openai::api/endpoint("http://localhost:11434/v1")
response ::openai::api/endpoint-request-stream(ep, "POST", "/chat/completions", {}, {
model: "llama3.2",
stream: true,
messages: [{role: "user", content: "Hi"}]
})
endpoint-response
fn (response: Result): Any
Unwrap an Endpoint response: the decoded body on 2xx, otherwise an
Err carrying HttpError. An Err from the request passes through. The
body is returned as a plain Map so servers that omit OpenAI-only fields
still parse.
Example
success ::hot::http/HttpResponse({status: 200, headers: {}, body: {id: "x"}})
::openai::api/endpoint-response(success) // {id: "x"}
missing ::hot::http/HttpResponse({status: 404, headers: {}, body: "nope"})
is-err(::openai::api/endpoint-response(missing)) // true
is-on-root
fn (url: Str, root: Str): Bool
True when the full URL url is under the base URL root (pure): same
scheme, host and port, a path inside the root's, and no .. segments.
Hosts match exactly, so api.openai.com.evil.example is off the root.
Example
::openai::api/is-on-root("https://api.openai.com/v1/models", "https://api.openai.com/v1") // => true
::openai::api/is-on-root("https://evil.example/v1/models", "https://api.openai.com/v1") // => false
join-url
fn (base: Str, path: Str): Str
Join an API path onto a base URL without doubling or dropping the
slash. A path that is already an absolute http:// or https://
URL is returned unchanged; target-url decides whether it may be sent.
Example
::openai::api/join-url("https://api.groq.com/openai/v1/", "/chat/completions")
// => "https://api.groq.com/openai/v1/chat/completions"
::openai::api/join-url("http://localhost:11434/v1", "models")
// => "http://localhost:11434/v1/models"
non-blank
fn (value: Any): Any
value, or null when it is null or a blank Str.
Example
::openai::api/non-blank(" ") // null
::openai::api/non-blank("sk-1") // "sk-1"
normalize-base-url
fn (url: Str?): Str
Normalize a base URL: trims whitespace, drops trailing slashes, and
falls back to ::openai/BASE_URL when the value is null or empty.
Example
::openai::api/normalize-base-url("http://localhost:11434/v1/") // => "http://localhost:11434/v1"
::openai::api/normalize-base-url(null) // => "https://api.openai.com/v1"
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 HTTP request with pre-filled service headers. url is a path
under the call's base URL (/models), or a full URL. Additional headers
can be provided to merge/override defaults.
The key comes from ctx (openai.api.key) by default. The 5th argument can
be an explicit key, or an opts Map (see resolve). A full URL off the
base URL is an Err unless the call passes its own key. The typed functions
in this package go through here.
Example
response ::openai::api/request("GET", "/models")
response ::openai::api/request("GET", "/models", {}, "", {api-key: "tenant-key"})
openai.api.key, openai.base.url
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 with pre-filled service headers. Returns a
response with an iterator body for SSE streams. url and the key work as
in request; the 6th argument can be an explicit key, or an opts Map.
Example
response ::openai::api/request-stream("POST", "/chat/completions", {}, {
model: "gpt-4o-mini",
stream: true,
messages: [{role: "user", content: "Hi"}]
})
for-each(response.body, (event) { print(event.data) })
openai.api.key, openai.base.url
resolve
fn (): Endpoint
fn (opts: Map): Endpoint
The Endpoint 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. With an endpoint in
opts, ctx is not read at all. Nothing is written to ctx, and a key from
opts is masked in the run's call records.
Example
::openai::api/resolve({api-key: "tenant-key"}).base-url // => "https://api.openai.com/v1"
local ::openai::api/endpoint("http://localhost:11434/v1")
::openai::api/resolve({endpoint: local}).api-key // => null
openai.api.key, openai.base.url
resolve-from
fn (opts: Map, ctx-values: Map): Endpoint
The one resolver, pure: the Endpoint for a call from opts first, then
ctx-values (a Map of context variables), then defaults. 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). {endpoint: ep} is the explicit form: ep as it is,
keyless or not, with no ctx involved.
Exposed for testing; resolve feeds it the context.
Example
::openai::api/resolve-from({api-key: "k"}, {}).base-url // => "https://api.openai.com/v1"
target-url
fn (ep: Endpoint, path: Str, own-key: Bool): Str
The URL to send a request to, for path on ep (pure). A relative path
is joined onto the base URL. A full URL passes when it is on the base URL
or when ep holds no credentials, and otherwise only when own-key says
the key came from the call's own opts. An Err for anything else, and
always for a URL whose host can't be read safely.
Example
ep ::openai::api/endpoint("https://api.openai.com/v1", "k")
::openai::api/target-url(ep, "/models", false) // => "https://api.openai.com/v1/models"
is-err(::openai::api/target-url(ep, "https://evil.example/", false)) // => true
url-parts
fn (url: Str): Map?
The {scheme, host, port, path} of an http(s) URL (pure), with scheme
and host lower-cased, or null unless the authority is plainly a host and
optional port. Userinfo, backslashes, extra slashes, whitespace and control
characters all give null: URL parsers disagree on where the host is in
those, and a backslash ends the authority in the one that opens the
connection, so https://evil.example\@api.openai.com/ connects to
evil.example.
Example
::openai::api/url-parts("https://API.openai.com/v1/models").host // => "api.openai.com"
::openai::api/url-parts("https://user@api.openai.com/") // => null
with-opts
fn (ep: Endpoint, opts: Map): Endpoint
ep with a call's opts applied (pure), for a provider whose endpoint is
already built:
endpointinoptsreplacesep.api-keyreplaces the key. Present but null or blank is an Err.base-urlreplaces the URL. Withoutapi-keyit is an Err whenepholds a key, which never goes to a per-call URL. A keylessepstays keyless.ep's headers don't follow to a new URL either.
Empty opts give ep unchanged. resolve-from builds on this, so the
rules live in one place.
Example
groq ::openai::api/endpoint("https://api.groq.com/openai/v1", "ctx-key")
::openai::api/with-opts(groq, {api-key: "tenant-key"}).api-key // => "tenant-key"
is-err(::openai::api/with-opts(groq, {base-url: "https://x"})) // => true
Types
Endpoint
Endpoint type {
base-url: Str,
api-key: Str?,
headers: Map?
}
Where and how to reach an OpenAI-compatible API: a base URL, an optional bearer key, and optional extra headers sent with every request.
base-url: e.g.https://api.openai.com/v1,http://localhost:11434/v1(Ollama),https://openrouter.ai/api/v1.api-key: sent asAuthorization: Bearer <key>. Leave it null or empty for keyless local servers; no Authorization header is sent.headers: merged into every request, e.g. OpenRouter'sHTTP-RefererandX-OpenRouter-Title, or a gateway's routing header.
An Endpoint is explicit: functions that take one never read the
openai.api.key or openai.base.url ctx variables. Build one with
endpoint(...), or resolve(opts) for opts over ctx over defaults.
Any typed function takes one as {endpoint: ep} in its opts.
HttpError
HttpError type {
status: Int,
headers: Map,
body: Any
}