# TypeScript SDK

> The Humanless SDK is one zero-dependency TypeScript file that wraps the REST API. It adds typed responses, typed errors and smart waits. `waitForCode` and `waitForLink` skip messages that have no code or link, and they re-poll until an overall deadline. It works anywhere `fetch` exists: Node 18+, Deno, Bun and browsers.

## Install

The SDK is a single source file, published at [usehumanless.com/sdk/humanless.ts](https://usehumanless.com/sdk/humanless.ts). It isn't on npm yet. Download it into your project:

```bash
curl -o humanless.ts https://usehumanless.com/sdk/humanless.ts
```

Then import it:

```ts
import { Humanless, withIdentity } from "./humanless";

const hl = new Humanless({ apiKey: process.env.HUMANLESS_API_KEY! });
```

### Constructor options

| Option | Type | Default |
|---|---|---|
| `apiKey` | string | required (`hl_live_...`) |
| `baseUrl` | string | `process.env.HUMANLESS_BASE_URL`, otherwise `https://api.usehumanless.com` |
| `fetch` | `typeof fetch` | global `fetch` (override it for tests or proxies) |

## Methods

| Method | REST call | Returns |
|---|---|---|
| `createIdentity({ channels?, domain?, ttl_seconds? })` | `POST /v1/identities` | `Identity` |
| `getIdentity(identityId)` | `GET /v1/identities/:id` | `Identity` |
| `waitForMessage({ identityId, channel?, timeout?, since?, signal? })` | `GET /v1/identities/:id/wait` | `Message` (any type) |
| `waitForCode(opts)` | same | `Message & { code: string }`. Skips messages without a code |
| `waitForLink(opts)` | same | `Message & { url: string }`. Skips messages without a link |
| `getTotp(identityId)` | `GET /v1/identities/:id/totp` | `{ code, seconds_remaining }` |
| `deleteIdentity(identityId)` | `DELETE /v1/identities/:id` | `{ identity_id, status: "deleted" }` |

### Wait options

| Option | Type | Default | Notes |
|---|---|---|---|
| `identityId` | string | required | |
| `channel` | `"any"`, `"email"` or `"sms"` | `"any"` | |
| `timeout` | seconds | `60` | Overall deadline across all long-poll rounds. Each round is at most 60 s |
| `since` | Unix ms or `Date` | none | Take `Date.now()` just **before** you trigger the send. This makes the wait race-free and lets you read the same message again |
| `signal` | `AbortSignal` | none | Cancels the wait |

## withIdentity

`withIdentity(hl, opts, fn)` creates an identity, runs `fn`, and **always** deletes the identity afterwards, even if `fn` throws. That keeps your dedicated phone numbers free and avoids `409 all_numbers_busy`.

```ts
await withIdentity(hl, { channels: ["email"] }, async ({ identity_id, email }) => {
  await page.getByLabel("Email").fill(email!);
  await page.getByRole("button", { name: "Send code" }).click();
  const { code } = await hl.waitForCode({ identityId: identity_id });
  await page.getByLabel("Code").fill(code);
});
```

## Errors

- `HumanlessError` is thrown for any non-2xx response. It has `status`, `code` (the API error code, for example `channel_not_enabled`), `message`, `retryAfterSeconds` and `details`. `err.isSmsQuotaExceeded` is `true` for `402 sms_quota_exceeded`.
- `HumanlessTimeoutError` (a subclass with `status` 408 and `code` `"timeout"`) is thrown by the wait methods when the overall `timeout` passes.

```ts
import { HumanlessError } from "./humanless";

try {
  await hl.createIdentity({ channels: ["sms"] });
} catch (err) {
  if (err instanceof HumanlessError && err.isSmsQuotaExceeded) {
    console.log("Top up or upgrade:", err.details?.billing_url);
  } else throw err;
}
```

## Types

```ts
type IdentityChannel = "email" | "sms" | "totp";

interface Identity {
  identity_id: string;
  channels: IdentityChannel[];
  email: string | null;
  phone: string | null;        // E.164
  totp_secret: string | null;  // base32
  status: "active" | "deleted" | "expired";
  expires_at: string;
  created_at: string;
  ended_at: string | null;
}

interface Message {
  type: "otp" | "magic_link" | "raw";
  code: string | null;
  url: string | null;
  channel: "email" | "sms";
  sender: string;
  subject: string | null;
  received_at: string;
  raw_snippet: string;
}
```

The SDK exports the old `Verifybox*` names as deprecated aliases, so code written against the earlier SDK keeps working.

---
Canonical: https://usehumanless.com/docs/sdk · Index of all docs: https://usehumanless.com/llms.txt
