Low-level authenticated requests to Model Studio's OpenAI-compatible endpoint, through ::openai::api/Endpoint. Paths are relative to ::qwen/base-url() (e.g. /chat/completions).

Per-call credentials. Every API-calling function in this package takes a trailing opts Map, resolved by resolve: opts first, then ctx, then defaults. Nothing from opts is written to ctx, so one run can serve many tenants.

  • api-key: overrides qwen.api.key. Present but null or blank is an Err, never a fallback to ctx.
  • base-url, region, workspace-id: where this call goes. Each of them needs api-key in opts too: qwen.api.key is never sent to a per-call host. region and workspace-id build the hostname, so region must be a known one and workspace-id a plain DNS label. A per-call workspace-id without region uses qwen.region; a per-call region doesn't use qwen.workspace.id, because a workspace lives in one region.
  • endpoint: an ::openai::api/Endpoint to use instead, on its own. Reads no ctx.

A key from 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.

Values

SITE_KEYS

SITE_KEYS: Vec [
  "base-url",
  "region",
  "workspace-id"
]

Functions

configured-endpoint

fn (): ::openai::api/Endpoint

The ctx-configured base URL with the qwen.api.key key if there is one, for a provider whose calls may bring their own key through provider-opts. See ::qwen::ai/provider.

Example

ep ::qwen::api/configured-endpoint()
ep.base-url
Context Vars: qwen.api.key

endpoint

fn (): ::openai::api/Endpoint

The ctx-configured Qwen endpoint: ::qwen/base-url() with the qwen.api.key key (your DashScope API key, DASHSCOPE_API_KEY in Alibaba's docs). An Err when no key is set. The same as resolve({}).

Example

ep ::qwen::api/endpoint()
ep.base-url  // => "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
Context Vars: qwen.api.key

endpoint-for

fn (region: Str?, workspace-id: Str?, api-key: Str): ::openai::api/Endpoint

An explicit Qwen endpoint for a region (and optional workspace id), with an explicit key. Reads no ctx. The key is masked in the run's call records.

Example

us ::qwen::api/endpoint-for("us", null, "sk-...")
us.base-url  // => "https://dashscope-us.aliyuncs.com/compatible-mode/v1"

request

fn (method: Str, path: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any, opts: Map): ::hot::http/HttpResponse

Authenticated request to the ctx-configured endpoint, or per the call's opts (see resolve). Returns the raw HttpResponse. A full URL off the endpoint's base URL is an Err.

Example

response ::qwen::api/request("GET", "/models")
response ::qwen::api/request("POST", "/chat/completions", {}, {
    model: "qwen3.7-plus",
    messages: [{role: "user", content: "Hello"}]
})
response ::qwen::api/request("GET", "/models", {}, "", {api-key: ::hot::ctx/get("tenant.qwen.key"), region: "us"})

request-stream

fn (method: Str, path: Str, additional-headers: Map, body: Any): ::hot::http/StreamingHttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any, opts: Map): ::hot::http/StreamingHttpResponse

Streaming (SSE) request to the ctx-configured endpoint, or per the call's opts (see resolve). Returns a StreamingHttpResponse.

Example

response ::qwen::api/request-stream("POST", "/chat/completions", {}, {
    model: "qwen3.8-flash",
    stream: true,
    messages: [{role: "user", content: "Hello"}]
})

for-each(response.body, (event) {
    print(::qwen::chat/extract-chunk-text(event))
})

resolve

fn (): ::openai::api/Endpoint
fn (opts: Map): ::openai::api/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. Nothing is written to ctx, and a key from opts is masked in the run's call records.

Example

::qwen::api/resolve({api-key: ::hot::ctx/get("tenant.qwen.key"), region: "us", workspace-id: "llm-abc"}).base-url
// => "https://llm-abc.us-east-1.maas.aliyuncs.com/compatible-mode/v1"
Context Vars: qwen.api.key, qwen.base.url, qwen.region, qwen.workspace.id

resolve-from

fn (opts: Map, ctx-values: Map): ::openai::api/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, or when site-opts-from refuses opts. {endpoint: ep} is the explicit form: ep as it is, with no ctx involved. Exposed for testing; resolve feeds it the context.

Example

::qwen::api/resolve-from({api-key: "k", region: "us"}, {}).base-url
// => "https://dashscope-us.aliyuncs.com/compatible-mode/v1"

site-opts

fn (opts: Map): Map

site-opts-from with qwen.region from ctx. Also masks a key from opts in the run's call records.

Example

site ::qwen::api/site-opts({api-key: ::hot::ctx/get("tenant.qwen.key"), region: "us"})
site.base-url

site-opts-from

fn (opts: Map, ctx-region: Str?): Map

A call's opts with region and workspace-id turned into the base-url they mean (pure), so ::openai::api/with-opts can apply them. ctx-region is the region for a workspace-id with no region of its own. An Err when api-key is blank, when any of base-url, region or workspace-id comes without api-key (the ctx key is never sent to a per-call host), or when a region or workspace id isn't valid.

Example

::qwen::api/site-opts-from({api-key: "k", region: "us"}, null).base-url
// => "https://dashscope-us.aliyuncs.com/compatible-mode/v1"