Transport for the Anthropic API: x-api-key and anthropic-version headers, 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: overrides anthropic.api.key.
  • base-url: overrides anthropic.base.url, for a proxy or gateway. It needs api-key in opts too: the ctx key is never sent to a per-call URL. A base URL and key both set in ctx work as usual.
  • api-version: overrides anthropic.api.version (the anthropic-version header).

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.

Values

DEFAULT_API_VERSION

DEFAULT_API_VERSION: Str "2023-06-01"

Functions

base-url

fn (): Str
fn (opts: Map): Str

The base URL calls go to: opts.base-url, then the anthropic.base.url ctx variable, then https://api.anthropic.com. Set it to route calls through a proxy or gateway.

Example

::anthropic::api/base-url()                                     // => "https://api.anthropic.com"
::anthropic::api/base-url({base-url: "https://gw.example.com"}) // => "https://gw.example.com"
Context Vars: anthropic.base.url

build-headers

fn (resolved: Map, has-body: Bool): Map

Request headers from resolved settings (see resolve), pure: x-api-key, anthropic-version, and Content-Type when there is a body. Exposed for testing.

Example

resolved ::anthropic::api/resolve()
headers ::anthropic::api/build-headers(resolved, true)
headers.Content-Type
headers.anthropic-version

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 ::anthropic::api/resolve-from({api-key: "k"}, {})
req ::anthropic::api/build-request("GET", "https://api.anthropic.com/v1/models", {}, "", resolved)
req.headers  // includes x-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 ::anthropic::api/call("GET", "/v1/models", {}, "", {api-key: "tenant-key"})
Context Vars: anthropic.api.key, anthropic.base.url, anthropic.api.version

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 ::anthropic::api/call-stream("POST", "/v1/messages", {}, {
  model: "claude-sonnet-4-5",
  max_tokens: 256,
  stream: true,
  messages: [{role: "user", content: "Hello!"}]
}, "sse", {})

// Parsed SSE events, one at a time
for-each(response.body, (event) { print(event.event) })
Context Vars: anthropic.api.key, anthropic.base.url, anthropic.api.version

normalize-base-url

fn (url: Str?): Str

Normalize a base URL: trims whitespace, drops trailing slashes, and falls back to ::anthropic/BASE_URL when the value is null or empty.

Example

::anthropic::api/normalize-base-url("https://gateway.example.com/")  // => "https://gateway.example.com"
::anthropic::api/normalize-base-url(null)                            // => "https://api.anthropic.com"

refused-stream

fn (response: ::hot::http/StreamingHttpResponse): HttpError

The HttpError for a streaming call the API refused, with the reason read from the response. Its body is what a plain call's would be: the decoded JSON, or the text when it isn't JSON.

Example

body {model: "claude-sonnet-4-5", max_tokens: 256, stream: true, messages: [{role: "user", content: "Hello!"}]}
response ::anthropic::api/call-stream("POST", "/v1/messages", {}, body, "sse", {})
if(gte(response.status, 400), err(::anthropic::api/refused-stream(response)), response)

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 Anthropic service headers.

Automatically includes the x-api-key and anthropic-version headers. The API key is read from context (anthropic.api.key) by default. The 5th argument can be an explicit API key, or an opts Map (see resolve). Additional headers can be provided to merge/override defaults (e.g. beta feature headers).

Example

// Simple GET request
response ::anthropic::api/request("GET", "https://api.anthropic.com/v1/models")

// POST with body
response ::anthropic::api/request("POST", "https://api.anthropic.com/v1/messages", {}, {
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    messages: [{role: "user", content: "Hello!"}]
})

// With additional headers (e.g. beta features)
response ::anthropic::api/request("GET", "https://api.anthropic.com/v1/files", {
    anthropic-beta: "files-api-2025-04-14"
})
Context Vars: anthropic.api.key, anthropic.api.version

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 Anthropic service headers.

Returns a StreamingHttpResponse whose body is an iterator that yields parsed SSE or NDJSON events. The default format is "sse". API key is read from context by default. The 6th argument can be an explicit API key, or an opts Map (see resolve).

Example

response ::anthropic::api/request-stream("POST", "https://api.anthropic.com/v1/messages", {}, {
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    stream: true,
    messages: [{role: "user", content: "Hello!"}]
})
// response.body is an iterator of parsed SSE events
events collect(response.body)
Context Vars: anthropic.api.key, anthropic.api.version

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

::anthropic::api/resolve({api-key: "tenant-key"}).base-url  // => "https://api.anthropic.com"
Context Vars: anthropic.api.key, anthropic.base.url, anthropic.api.version

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, api-version}, 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

::anthropic::api/resolve-from({api-key: "k"}, {}).api-version  // => "2023-06-01"

Types

HttpError

HttpError type {
    status: Int,
    headers: Map,
    body: Any
}