# TypeScript

Use typed requests, bounded retries, and cancellation.

Use Node.js 22 or newer with native `fetch` and `AbortSignal`. Install the local `@secnd/sdk` workspace package after building it; registry publication is not assumed.

```typescript
import { Secnd, type DecideRequest } from "@secnd/sdk";

const apiKey = process.env.SECND_API_KEY;
if (!apiKey) throw new Error("Set SECND_API_KEY");
const client = new Secnd({ apiKey });
const request: DecideRequest = {
  model: "ren-1.1.0",
  type: "noul",
  situation: "The customer asks to cancel before renewal.",
  question: "Is cancellation explicitly requested?",
  threshold: 0.9,
};
const result = await client.decide(request);
console.log(result.choice, result.probability, result.action);
```

Constructor options: `apiKey` (required), `baseURL` (default `https://api.secndai.com`), `maxRetries` (default 2; 0–5), `timeoutMs` (default 30000 per attempt), and an optional `fetch` implementation. HTTPS is required except on localhost.

## Bound the whole call

```typescript
const result = await client.decide(request, {
  signal: AbortSignal.timeout(10_000),
});
```

Pass an `AbortSignal` to enforce an application deadline across attempts and retry waits. Catch errors rather than treating cancellation as a decision.

## Typed errors

`AuthenticationError`, `ValidationError`, `RateLimitError`, and `ServerError` extend `SecndError`. Instances expose `status`, `code`, `message`, optional `retryAfter`, and optional `requestId` (the `X-Request-Id` header). Network, cancellation, and invalid-response failures can use the base class.

The client retries `429` and server errors with exponential backoff, honoring the full server delay up to 60 seconds (`retry-after-ms` when present, else `Retry-After`); a longer delay throws a typed error carrying `retryAfter` instead of retrying. It surfaces transport failures directly. Do not put this client, or its bearer key, in browser code.
