# TypeScript reference (/docs/sdks/typescript)
The TypeScript SDK runs on Node.js 18 and later, using the built-in `fetch`. Every network method returns a promise. It ships ES module and CommonJS builds with TypeScript definitions.

## Install [#install]

```bash
npm install docklee
```

```ts
import { Dock, Lynk } from 'docklee';
```

<Callout type="warn">
Call the SDK from your server. An API key in browser code can be read by anyone who opens the page.
</Callout>

## Dock [#dock]

`Dock` writes into a container or a memory space. See [Configuration](/docs/sdks/configuration#dock-options) for its options.

### `capture` [#capture]

Adds an event to the buffer and returns at once. Blank content is ignored, and identical content within 60 seconds is skipped.

```ts
dock.capture('Customer asked to move the renewal date to June.', {
  type: 'event',
  metadata: { ticket_id: 'T-4821' },
  endUser: 'user_8f2c',
});
```

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `type` | `'conversation' \| 'document' \| 'event' \| 'preference' \| 'entity' \| 'knowledge'` | `'conversation'` | What kind of event this is. |
| `metadata` | `Record<string, unknown>` | none | Your own key and value pairs. |
| `dedupKey` | `string` | none | Your own key for deciding when two captures are the same. |
| `endUser` | `string` | the `Dock` default | The person this memory belongs to. |

Returns `void`. Throws if the `Dock` is closed.

### `flush` [#flush]

Sends the buffer and waits. On failure, the events go back into the buffer for the next flush.

```ts
const result = await dock.flush();
if (result) {
  console.log(result.accepted, result.rejected);
}
```

Returns a [`DockIngestResult`](/docs/sdks/types#dockingestresult), or `null` when the buffer was empty or the request failed.

The buffer is also sent every `flushInterval` milliseconds, 5,000 by default.

### `close` [#close]

Stops the timer and sends the buffer. Call it when you're done, so your process can exit.

```ts
await dock.close();
```

### `instrument` [#instrument]

Attaches a middleware to an Express app, or any app with Express-style `use` middleware, that captures write requests. Register your body parser first. See [Instrument your app](/docs/memory/automatic-capture/instrument).

```ts
app.use(express.json());
dock.instrument(app);
```

## Lynk [#lynk]

`Lynk` reads from an engine or a memory space. See [Configuration](/docs/sdks/configuration#lynk-options) for its options.

### `query` [#query]

Asks an engine a question and returns a cited answer. See [Query an engine](/docs/knowledge/query-an-engine).

```ts
import { Lynk, type KnowledgeResult } from 'docklee';

const lynk = new Lynk();
const result = (await lynk.query('What is the refund window?', { mode: 'blended' })) as KnowledgeResult;
```

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `engineId` | `string` | the `Lynk` default | The engine to ask. |
| `topK` | `number` | `8` | Chunks to search for, from 1 to 50. |
| `mode` | `string` | `'precise'` | See [Answer modes](/docs/knowledge/answers/answer-modes). |
| `stream` | `boolean` | `false` | Returns an async generator of answer text. |

Returns `Promise<KnowledgeResult | AsyncGenerator<string>>`. Cast the result to the shape that matches `stream`. Throws when no engine ID is set.

```ts
const stream = (await lynk.query('Summarize the refund policy.', {
  stream: true,
})) as AsyncGenerator<string>;
for await (const token of stream) {
  process.stdout.write(token);
}
```

### `retrieve` [#retrieve]

Returns the ranked chunks for a question, without writing an answer. See [Retrieve context](/docs/knowledge/retrieve-context).

```ts
const chunks = await lynk.retrieve('refund window', { topK: 5 });
```

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `engineId` | `string` | the `Lynk` default | The engine to search. |
| `topK` | `number` | `8` | Chunks to return, from 1 to 100. |

Returns `Promise<KnowledgeChunk[]>`. Throws when no engine ID is set.

### `recall` [#recall]

Returns the memories that answer a question. See [Recall memory](/docs/memory/recall-memory).

```ts
const records = await lynk.recall('display preferences', { endUser: 'user_8f2c' });
```

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `memorySpaceId` | `string` | the `Lynk` default | The space to read. |
| `topK` | `number` | `5` | Memories to return, from 1 to 50. |
| `endUser` | `string` | the `Lynk` default | Returns only this person's memories. |

Returns `Promise<MemoryRecord[]>`. Throws when no memory space ID is set.

## Types [#types]

Import the result and option types alongside the classes:

```ts
import type {
  Citation,
  DockConfig,
  DockIngestResult,
  KnowledgeChunk,
  KnowledgeResult,
  LynkConfig,
  MemoryRecord,
} from 'docklee';
```

See [Types](/docs/sdks/types) for every field.

## Next steps [#next-steps]

You've seen every TypeScript method. See the shapes they return next.

<Cards>
  <Card title="Types" href="/docs/sdks/types" description="Every result and its fields." />
  <Card title="Errors and retries" href="/docs/sdks/errors-and-retries" description="What's retried, and what's thrown." />
  <Card title="Configuration" href="/docs/sdks/configuration" description="Every constructor option." />
</Cards>
