Client configuration
You configure the SDK in two places: options you pass to new Glexir(), which apply to every request, and request options you pass as the last argument of any method, which apply to one call.
Constructor options
All options are optional.
apiKeystringoptionaldefaultprocess.env.GLEXIR_API_KEY- Your Glexir key (
glx_live_...). Sent asAuthorization: Bearer <apiKey>. If neither this option nor the environment variable is set, the constructor throws aGlexirError. baseURLstringoptionaldefaultprocess.env.GLEXIR_BASE_URL, then "https://api.glexir.com/v1"- The gateway URL, including
/v1. Trailing slashes are removed. Set it only to reach a local gateway or another deployment. See Base URL. timeoutnumberoptionaldefault600000 (10 minutes)- Milliseconds allowed for each attempt, including reading the response body. Each retry gets a fresh timeout. Once a stream or file download has returned its headers, it has no timeout.
maxRetriesnumberoptionaldefault2- How many times to retry connection errors, timeouts and
408,409,429and5xxresponses.0disables retries. See Retries. fetchtypeof fetchoptionaldefaultglobalThis.fetch- A custom
fetch, for example to add a proxy, logging or a test double. defaultHeadersRecord<string, string>optionaldefault{}- Headers sent with every request. They are applied after the SDK's own headers, so they can override them. See Headers.
Environment variables
The constructor reads two variables when the matching option is not passed:
| Variable | Used for | Fallback |
|---|---|---|
GLEXIR_API_KEY | apiKey | none; the constructor throws GlexirError |
GLEXIR_BASE_URL | baseURL | https://api.glexir.com/v1 |
Values are trimmed, and an empty value counts as unset. An option you pass always wins over the variable.
The SDK reads process.env only when a global process exists. In browsers and on edge runtimes without it, pass apiKey explicitly, and baseURL if you need a non-default URL.
With GLEXIR_API_KEY set, new Glexir() needs no arguments.
Base URL
The SDK sends requests to production, https://api.glexir.com/v1, by default. The default is exported as DEFAULT_BASE_URL. You don't need to set a base URL to use Glexir.
Set baseURL, or GLEXIR_BASE_URL, only to point the client somewhere else:
| Environment | baseURL |
|---|---|
| Production (default) | https://api.glexir.com/v1 |
Local gateway (apps/gateway) | http://localhost:8787/v1 |
| Another deployment | Its URL, including /v1 |
The client resolves the base URL in this order: the baseURL option, then GLEXIR_BASE_URL, then DEFAULT_BASE_URL. Method paths are appended to it, so glexir.models.list() calls <baseURL>/models.
Timeouts
timeout applies to each attempt separately. With the defaults (10 minutes, 2 retries), a call that keeps timing out can take about 30 minutes plus backoff before it throws APIConnectionTimeoutError. Lower timeout or maxRetries if you need a tighter bound, or pass an AbortSignal (see below).
For non-streaming calls the timeout covers the whole response, including the body. For streams (stream: true, responses.stream(), chat.completions.stream()) and files.content(), the timer stops once response headers arrive; after that, only your signal can stop the request.
Headers
Every request carries these headers:
| Header | Value |
|---|---|
authorization | Bearer <apiKey> |
accept | application/json, or text/event-stream for streams |
content-type | application/json for JSON bodies. Multipart uploads set their own. |
idempotency-key | A UUID per call on every non-GET request. See Idempotency. |
x-glexir-sdk | js/<VERSION>, for example js/0.2.0 |
x-glexir-retry-count | 0 on the first attempt, then 1, 2, ... |
Headers are applied in this order, later ones winning: SDK headers, then defaultHeaders, then per-request headers. In per-request headers, a null value removes a header.
Per-request options
Every method takes a RequestOptions object as its last argument.
signalAbortSignal | nulloptional- Cancels the request and any pending retries. The call rejects with
APIUserAbortError. timeoutnumberoptionaldefaultclient timeout- Per-attempt timeout in milliseconds for this call.
maxRetriesnumberoptionaldefaultclient maxRetries- Retry limit for this call.
headersRecord<string, string | null | undefined>optional- Extra headers for this call.
nullremoves a header set by the SDK or bydefaultHeaders;undefinedis ignored. idempotencyKeystringoptionaldefaultrandom UUID- Sent as
Idempotency-Keyon non-GET requests. Ignored on GET requests. The gateway accepts up to 255 characters.
responses.parse() and chat.completions.parse() take ParseOptions, which is RequestOptions plus a validate function. See Responses.
Raw responses and request IDs
Every method returns an APIPromise<T>. Await it to get the parsed body. The request starts as soon as you call the method, even if you never await the result.
The gateway's request ID is attached to parsed JSON objects as _request_id. It is non-enumerable, so it does not show up in JSON.stringify or Object.keys. It is not attached to string results (for example srt transcripts) or streams.
then / catch / finallyPromise methodsoptional- Resolve with the parsed body
T, or reject with an error from Errors & retries. withRawResponse()Promise<WithRawResponse<T>>optional- Resolves with
{ data, response, requestId }: the parsed body, the HTTPResponse, and thex-request-idheader (ornull). The body is parsed once, however many times you call it or await the promise. asResponse()Promise<Response>optional- Resolves with the HTTP
Responsewithout parsing it. For non-streaming calls the body has already been buffered, so you can still read it.
Client properties
A client exposes its resolved settings as read-only properties: apiKey, baseURL (without a trailing slash), timeout and maxRetries. The resources are properties too: responses, conversations, chat, audio, embeddings, files, batches and models. See Overview.
To change settings, create another client. Clients are cheap to create and hold no connections of their own.
Low-level requests
The client also has untyped methods for calling a path directly. They go through the same headers, retries, timeouts and error mapping as the typed methods. Prefer the typed methods; use these only for an endpoint the SDK does not wrap yet.
| Method | Description |
|---|---|
get<T>(path, options?) | Sends a GET. Returns APIPromise<T>. |
post<T>(path, options?) | Sends a POST with an idempotency key. |
delete<T>(path, options?) | Sends a DELETE with an idempotency key. |
request<T>(options) | Sends a request described by options, which must include method and path. |
buildURL(path, query?) | Returns the full URL for a path and query object. |
options accepts the per-request options above plus query (an object), body (sent as JSON), multipart: true (send body as multipart/form-data), stream: true (parse server-sent events into a Stream) and binary: true (return the raw Response). Query arrays are sent as key[]=value and nested objects as key[sub]=value. undefined and null values are left out.