# Use the SDK

> Read and write your database, files and realtime channels, and send email and push, from JavaScript or TypeScript.

Source: https://daster.io/docs/sdk-cli-mcp/sdk/ · Last updated: 2026-11-21

The SDK has one client per kind of credential. Pick the one that matches where your code runs.

| Client                       | Key                         | Runs in                 | Gives you                                                               |
| ---------------------------- | --------------------------- | ----------------------- | ----------------------------------------------------------------------- |
| `createProductClient`        | Browser key `daster_pk_…`   | Browser, mobile, server | `auth`, `database`, `storage`, `realtime`, with your row-level security |
| `createServiceClient`        | Server key `daster_sk_…`    | Server only             | `database`, `storage`, `realtime` as the project owner                  |
| `createCommunicationsClient` | App email key `daster_sk_…` | Server only             | `sendMessage`, `push`                                                   |

Each client takes `projectId`, the project's ID (a UUID), and its key. See [Install and sign in](https://daster.io/docs/sdk-cli-mcp/install-and-sign-in/) for where the keys come from.

## Query the database from your server

```ts
import { createServiceClient } from '@dasterhq/daster';

const db = createServiceClient({
  projectId: process.env.DASTER_PROJECT_ID!,
  serverKey: process.env.DASTER_SERVER_KEY!,
});

const { rows } = await db.database.select('todos', { eq: { done: false }, limit: 20 });
await db.database.insert('todos', { title: 'Write the docs', done: false });
await db.database.update('todos', { done: true }, { eq: { id: 42 } });
```

The server key bypasses row-level security, so filter every query yourself. Apps hosted on DASTER already receive `DASTER_SERVER_KEY` in their environment ([Connect once](https://daster.io/docs/getting-started/connect-once/)); set `DASTER_PROJECT_ID` yourself.

The database methods follow the data API's rules:

- `select` filters by equality only (`eq`). It returns at most 100 rows (50 by default); `offset` goes up to 10,000, and `paginate` walks through pages for you.
- `update` and `delete` refuse to run without an `eq` filter.
- `insert` and `update` take 1 to 30 columns.
- `rpc(name, args)` calls a database function, if the API role may run it.

## Sign a user in from the browser

```ts
import { createProductClient } from '@dasterhq/daster';

const app = createProductClient({
  projectId: '<project id>',
  publishableKey: 'daster_pk_…',
});

await app.auth.signIn({ email, password });
const { rows } = await app.database.select('todos', { limit: 20 });
```

With a signed-in user, requests carry the user's session and your row-level security policies decide what they see. `app.auth` also has `signUp`, `verifyEmail`, `requestPasswordReset`, `resetPassword`, `refresh`, `signOut`, `revokeAllSessions` and `user`. By default the session is kept in memory; pass `sessionStore` with `get` and `set` to keep it elsewhere. See [Authentication](https://daster.io/docs/auth-storage-realtime/authentication/).

## Files and realtime

```ts
const uploaded = await app.storage.upload(bucketId, {
  path: 'avatars/ada.png',
  contentType: 'image/png',
  bytes,
});
const link = await app.storage.createSignedUrl(uploaded.id, { expiresIn: 300 });

for await (const batch of app.realtime.stream('orders')) {
  for (const event of batch.items) console.log(event.type, event);
}
```

`bytes` is a `Uint8Array` (or pass `base64` instead). A signed URL lasts at most 300 seconds; `link.url` is the download address. `storage` also has `list`, `download` and `remove`. Each realtime batch carries a `cursor`; pass it back as `{ cursor }` to resume after a reconnect. `realtime.events(channel, { cursor })` reads one batch instead of a stream, and `realtime.broadcast(channel, payload, { idempotencyKey })` sends an event to a channel. See [Storage](https://daster.io/docs/auth-storage-realtime/storage/) and [Realtime](https://daster.io/docs/auth-storage-realtime/realtime/).

## Send an email

```ts
import { createCommunicationsClient } from '@dasterhq/daster';

const messages = createCommunicationsClient({
  projectId: process.env.DASTER_PROJECT_ID!,
  credential: process.env.DASTER_EMAIL_KEY!,
});

const receipt = await messages.sendMessage(
  {
    channel: 'email',
    to: 'ada@example.com',
    subject: 'Your receipt',
    text: 'Order 1042 confirmed.',
  },
  { idempotencyKey: 'receipt-1042' },
);
```

`idempotencyKey` is required. Sending again with the same key returns the first message instead of sending twice. An email takes `text`, `html`, or a `templateId` with `variables`. The answer is a receipt with the message `id`, its `status` and a masked recipient. See [App email](https://daster.io/docs/messaging/app-email/).

## Send a push notification

Push needs a credential with Push access. Apps hosted on DASTER receive one as `DASTER_PUSH_KEY` when Push is wired to them; the project's server key does not reach Push.

```ts
const push = createCommunicationsClient({
  projectId: process.env.DASTER_PROJECT_ID!,
  credential: process.env.DASTER_PUSH_KEY!,
});

await push.push.send(
  pushAppId,
  { topic: 'news', title: 'New article', body: 'Read it now.', url: 'https://example.com/news' },
  { idempotencyKey: 'news-2026-11-21' },
);
```

Choose exactly one audience: `targets` (device IDs), `broadcast: true` or `topic`. Register devices with `push.push.registerDevice(pushAppId, { target, topics })`. See [Push notifications](https://daster.io/docs/messaging/push-notifications/).

> **Note:** The communications client also has `sendOtp`, `verifyOtp` and an `sms` channel. SMS is not available in production yet, so those calls are refused for now.

## Errors and retries

| Error                  | When                                                                              |
| ---------------------- | --------------------------------------------------------------------------------- |
| `DasterApiError`       | DASTER answered with an error. It has `status`, `code` and `requestId`            |
| `DasterTransportError` | The request did not complete: `kind` is `network`, `timeout`, `aborted` and so on |
| `DasterConfigError`    | An option is wrong, for example a server key passed to `createProductClient`      |

Reads and calls with an `idempotencyKey` are retried up to 3 attempts in all. Other changes, such as `insert`, are never retried. When a `DasterTransportError` has `ambiguous: true`, the change may have happened; repeat it only with the same `idempotencyKey`. One call times out after 15 seconds by default; set `timeoutMs` (up to 120,000) on the client or on a call, or pass an `AbortSignal` as `signal`.
