> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getlurk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript and JavaScript

> @getlurk/sdk: a typed client for Node, browsers, Cloudflare Workers, Bun and Deno.

```bash theme={null}
npm install @getlurk/sdk
```

It ships ESM and CommonJS, its own types, and has no dependencies. It needs Node 20 or newer, or any runtime with `fetch` and Web Crypto.

## Send an event

```typescript theme={null}
import { Lurk } from "@getlurk/sdk";

const lurk = new Lurk(); // reads the key from LURK_KEY

const receipt = await lurk.events.send({
  event: "checkin.scanned",
  user: "maya@example.com",
  properties: { venue: "mission-st" },
  idempotency_key: "chk-mission-st-maya-0812",
});

console.log(receipt.receipts);
```

Every operation in the API is a method named after it: `lurk.rules.list()`, `lurk.units.get({ id: "beans" })`, `lurk.users.get({ id: "maya@example.com" })`, `lurk.units.use({ ... })`. Groups are camelCase (`lurk.apiKeys`, `lurk.webhookEndpoints`). Inputs and answers are typed from the API's own description, so a mistake in a field name is a compile error.

## Configure

```typescript theme={null}
const lurk = new Lurk({
  apiKey: process.env.LURK_KEY, // default: LURK_KEY
  baseUrl: "https://api.getlurk.com", // default
  maxRetries: 2, // default
  timeoutMs: 30_000, // default
});
```

| Option | What |
| - | - |
| `apiKey` | A project key, `lurk_sk_…`. Left out, it reads `LURK_KEY`. |
| `baseUrl` | Where the API is. The default is the public API. |
| `maxRetries` | How many times to retry a call that is safe to retry. |
| `timeoutMs` | Cut a call off after this long. |
| `fetch` | Your own `fetch`: a proxy-aware one, or a test double. |

Per call, the second argument takes the same kind of settings:

```typescript theme={null}
await lurk.events.send(input, {
  idempotencyKey: `order-${order.id}`, // your own, instead of a made-up one
  signal: AbortSignal.timeout(5_000), // cancel
  timeoutMs: 5_000,
  headers: { traceparent }, // join your trace to ours
});
```

## Errors

```typescript theme={null}
import { Lurk, LurkError, LurkConnectionError, isRetryable } from "@getlurk/sdk";

try {
  await lurk.units.use({ user: "maya@example.com", unit: "coupon", reference: "scan-1" });
} catch (error) {
  if (error instanceof LurkError) {
    console.error(error.code, error.param, error.requestId); // `code` is the part to branch on
    console.error(error.nextStep); // one sentence on what to do
    if (isRetryable(error)) {
      // Sending the same request again can work. The SDK already tried twice.
    }
  } else if (error instanceof LurkConnectionError) {
    // No complete answer: a network or body-transfer failure, timeout, or cancellation.
  } else {
    throw error;
  }
}
```

A `LurkError` has `status`, `code`, `param`, `requestId`, `docUrl`, `nextStep` and, for `rate_limited`, `retryAfterSeconds`. Branch on `code`, show `message`, and quote `requestId` to support. Be ready for a `code` you have not seen: Lurk adds codes, fields and event types over time and never removes one inside a version ([versioning](https://docs.getlurk.com/guides/versioning.md)).

## Pages

```typescript theme={null}
import { paginate, toArray } from "@getlurk/sdk";

for await (const rule of paginate(lurk.rules.list, { status: "live" })) {
  console.log(rule.key);
}

const units = await toArray(paginate(lurk.units.list)); // when you know the list is short
```

[More](https://docs.getlurk.com/guides/pagination.md).

## Webhooks

```typescript theme={null}
import { constructEvent } from "@getlurk/sdk";

const event = await constructEvent(rawBody, request.headers, process.env.LURK_WEBHOOK_SECRET ?? "");
if (event.type === "unit.used") console.log(event.data.unit, event.data.amount);
```

Pass the raw body, not parsed JSON. [The whole page](https://docs.getlurk.com/webhooks.md).

## Types

Every request and answer type is exported: `import type { Rule, Unit, EventsSendInput } from "@getlurk/sdk"`.

## In a browser

It runs there, but a project key must not. Call Lurk from your server, and give the browser your own endpoint.

## Testing

`@getlurk/sdk/testing` gives you a real client with no network. [Testing your code](https://docs.getlurk.com/sdks/testing.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.