Skip to main content
All constructors accept a single StructClientConfig object:
import { StructClient } from "@structbuild/sdk";

const client = new StructClient({
  apiKey: "sk_live_xxx",
  timeout: 10_000,
  retry: {
    maxRetries: 3,
    initialDelayMs: 500,
    maxDelayMs: 30_000,
  },
  headers: {
    "X-App-Version": "1.4.0",
  },
  onRequest: ({ method, url }) => console.log(`${method} ${url}`),
  onResponse: ({ status, durationMs }) => console.log(`${status} in ${durationMs}ms`),
});

Options

OptionTypeDefaultDescription
apiKeystringrequiredSecret (sk_*) or JWT public key (pk_jwt_*). Sent as X-API-Key.
jwtstring(none)User JWT, sent as Authorization: Bearer. Only used with pk_jwt_* keys.
baseUrlstringhttps://api.struct.to/v1Override the REST base URL.
headersRecord<string, string>(none)Extra headers merged into every request.
timeoutnumber30_000Per-request timeout in milliseconds. Enforced with AbortController.
retryRetryConfigoffAutomatic retry on 429 and 5xx. Disabled when omitted.
onRequest(info) => void | Promise<void>(none)Called before every request.
onResponse(info) => void | Promise<void>(none)Called after every response.

Retry

Pass a retry block to enable exponential backoff on retryable failures (429, 500, 502, 503, 504, NetworkError, TimeoutError). Retry-After headers on 429 responses are honoured when present.
const client = new StructClient({
  apiKey: "sk_live_xxx",
  retry: {
    maxRetries: 3,
    initialDelayMs: 500,
    maxDelayMs: 10_000,
  },
});
FieldDefaultDescription
maxRetries3Maximum retry attempts after the initial request.
initialDelayMs1000Base delay, doubled on each attempt.
maxDelayMs30_000Upper bound on the computed delay.

Hooks

onRequest and onResponse fire around every HTTP call and are useful for logging, tracing, and metrics. Hook errors are swallowed so they never break a request.
const client = new StructClient({
  apiKey: "sk_live_xxx",
  onRequest: ({ method, url, headers }) => {
    tracer.start({ method, url });
  },
  onResponse: ({ method, url, status, durationMs, headers }) => {
    tracer.end({ method, url, status, durationMs });
  },
});

Custom base URL

Enterprise customers with a dedicated domain can point the SDK at their own host by overriding baseUrl. Include the /v1 prefix; the SDK does not add it for you.
const client = new StructClient({
  apiKey: "sk_live_xxx",
  baseUrl: "https://api.yourcompany.com/v1",
});

WebSocket configuration

StructWebSocket and StructAlertsWebSocket share a similar config shape, plus reconnect and subscribe-timeout options. See WebSockets for details.
import { StructWebSocket } from "@structbuild/sdk";

const ws = new StructWebSocket({
  apiKey: "sk_live_xxx",
  baseUrl: "wss://api.yourcompany.com",
  subscribeTimeout: 10_000,
  reconnect: {
    maxRetries: 10,
    initialDelayMs: 500,
    maxDelayMs: 30_000,
  },
});
Last modified on April 14, 2026