Internal HTTP plumbing for the Deepgram API: authenticated requests,
repeated-key query strings, and the HttpError returned for non-2xx
responses.
Per-call credentials. Every API-calling function in this package
takes an optional trailing opts Map, resolved by resolve: opts
first, then context, then the defaults. Nothing from opts is written
to ctx, so one run can act for many tenants and parallel flows don't
share credentials. Credentials in opts are masked in call records. A
credential key present but blank or null is an error, never a fallback
to context.
api-key: the API key for this callbase-url: another API root, such as a proxy. It needs anapi-keyin the sameopts:deepgram.api.keyis never sent to a per-call URL.callback-secret: the shared secret the async functions send with this call, instead ofdeepgram.callback.secret
Context Vars
deepgram.api.keydeepgram.callback.secret
Values
CREDENTIAL_KEYS
CREDENTIAL_KEYS: Vec [
"api-key",
"callback-secret"
]
Functions
build-request
fn (method: Str, path: Str, content-type: Str, body: Any, resolved: Map): ::hot::http/HttpRequest
The outgoing HttpRequest from resolved settings (see resolve). An
empty content-type sends no Content-Type. Pure; exposed for testing.
Example
settings ::deepgram::api/resolve({})
req ::deepgram::api/build-request("POST", "/v1/listen?model=nova-3", "application/json", to-json({url: "https://example.com/a.wav"}), settings)
req.url
bytes
fn (method: Str, path: Str, content-type: Str, body: Any): Bytes
fn (method: Str, path: Str, content-type: Str, body: Any, opts: Map): Bytes
Like request, but returns the verbatim response Bytes on 2xx (for
audio), or an Err carrying HttpError.
Example
audio ::deepgram::api/bytes("POST", "/v1/speak?model=aura-2-thalia-en", "application/json", to-json({text: "Hello."}))
::hot::file/write-file-bytes("hello.mp3", audio)
error-message
fn (status: Int, body: Any): Str
Build a readable message from a Deepgram error status and body. Exposed for testing.
Example
::deepgram::api/error-message(402, {err_code: "ASR_PAYMENT_REQUIRED", err_msg: "Out of credits."})
// "Deepgram API error (HTTP 402): ASR_PAYMENT_REQUIRED: Out of credits."
::deepgram::api/error-message(500, "") // "Deepgram API error (HTTP 500)"
json
fn (method: Str, path: Str): Any
fn (method: Str, path: Str, content-type: Str, body: Any): Any
fn (method: Str, path: Str, content-type: Str, body: Any, opts: Map): Any
Like request, but returns the decoded JSON body on 2xx, or an Err
carrying HttpError.
Example
models ::deepgram::api/json("GET", "/v1/models")
map(models.stt, %.canonical_name)
query-string
fn (params: Map): Str
Build a query-string suffix (?a=1&b=2, or "" when nothing is
set) from a Map of options. Null values are dropped, Bools render as
true/false, and Vec values repeat the key (keyterm=a&keyterm=b),
which is how Deepgram takes multi-value options such as keyterm,
keywords, search, replace, redact, tag, and extra.
Keys and values are percent-encoded.
Example
::deepgram::api/query-string({model: "nova-3", keyterm: ["Hot", "Deepgram"], diarize: null})
// "?model=nova-3&keyterm=Hot&keyterm=Deepgram"
request
fn (method: Str, path: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, content-type: Str, body: Any): ::hot::http/HttpResponse
fn (method: Str, path: Str, content-type: Str, body: Any, key: Str?): ::hot::http/HttpResponse
fn (method: Str, path: Str, content-type: Str, body: Any, opts: Map): ::hot::http/HttpResponse
Make an authenticated request to the Deepgram API (Authorization: Token <key>). path is relative to https://api.deepgram.com
(e.g. /v1/listen?model=nova-3). body is sent as-is: a JSON
string, raw audio Bytes, or "" for none. Returns the raw
HttpResponse; json and bytes unwrap it.
Example
response ::deepgram::api/request("GET", "/v1/models")
response.status
// A different key for this call
::deepgram::api/request("GET", "/v1/models", "", "", {api-key: ::hot::ctx/get("tenant-b.deepgram.api.key")})
resolve
fn (opts: Map): Map
Credentials and settings for one call (see resolve-from). Credentials
from opts are masked in this run's records and never written to ctx.
Example
::deepgram::api/resolve({api-key: "tenant-b-key"}).base-url // "https://api.deepgram.com"
deepgram.api.key, deepgram.callback.secret
resolve-from
fn (opts: Map, ctx-values: Map): Map
The one resolver, pure: {api-key, base-url, callback-secret} 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 a
credential key is in opts but blank or null (that never falls back
to context), or when opts has base-url without api-key. A
per-call base-url also keeps deepgram.callback.secret out of the
request. Exposed for testing; resolve feeds it the context.
Example
settings ::deepgram::api/resolve-from({}, {"deepgram.api.key": ::hot::ctx/get("deepgram.api.key")})
settings.base-url
Types
HttpError
HttpError type {
status: Int,
headers: Map?,
body: Any?,
message: Str
}
A non-2xx Deepgram response. message is a readable summary built
from Deepgram's error body (err_code/err_msg on older endpoints,
category/message/details on newer ones); body keeps the
decoded payload for anything else.