SDK overview

Connecting a backend to the server with the engine SDK, its promise and Effect forms, and its configuration.

@voidhash/mimic-server/engine is the SDK a backend uses to reach a server. It reads, creates, changes and deletes documents, mints document tokens, and holds presence leases, all over the server's HTTP API. This page shows how to create an SDK instance and how its parts are organized.

Setting up what the documents live in (databases, collections, keys) is not the SDK's: it is the server's administration API, which deployment tooling calls. See Collections and documents and Access management.

Creating an SDK

import { EngineSDK } from "@voidhash/mimic-server/engine";

const sdk = new EngineSDK({
  url: "https://db.example.com",
  credential: process.env.MIMIC_KEY,
});

url is the server's base address, without a path: the SDK calls {url}/v1/databases/…. On the hosted service it is the database's clusterUrl. Create one instance and reuse it; await sdk.dispose() releases it when the process shuts down.

Credentials

The SDK takes one of two credentials:

// A database key (or a document token), sent as `Authorization: Bearer …`.
new EngineSDK({ url, credential: "mk_…" });

// A user's name and password, sent as HTTP Basic.
new EngineSDK({ url, user: { name: "admin", password: "…" } });

Use a database key scoped to one database for a service. What each credential may do is described in Authentication and access.

A document token works too, for code that should reach only one document: an agent your backend runs on a user's behalf, for example. It is refused for listings and for minting.

Configuration

NameTypeDefaultDescription
url *string—The server's base URL, for example https://db.example.com.
credentialstring—A database key or a document token, sent as Bearer.
user{ name: string; password: string }—A user's name and password, sent as HTTP Basic, instead of credential.
consumer{ name, build, versions? }—Identifies this service and the schema versions it reads and writes. See below.
maxAttemptsnumber8Attempts of a retry-safe call that is answered without a verdict.
retryBaseDuration.Input"500 millis"The first wait between attempts. It doubles, with jitter, up to 30 seconds.
attemptTimeoutDuration.Input"30 seconds"How long one attempt may take before it is given up as unanswered.
fetchtypeof fetch—The fetch implementation to use. Defaults to the global one.

The constructor throws a TypeError without credential or user.

Handles

The SDK is organized as handles that mirror the data model. A handle is a plain object that remembers ids; creating one makes no request.

const database = sdk.database(databaseId);
const boards = database.collection(collectionId, { primitive: BoardDocument });

const board = await boards.get("board-1");
console.log(board.snapshot, board.revision);
HandleFromFor
EngineSDKnew EngineSDK(config)Databases, by id
EngineDatabaseHandlesdk.database(id)A database's collections; minting tokens
EngineCollectionHandledatabase.collection(id, { primitive? })Documents: reads, writes, listings, presence

database.collection(id, { primitive }) takes the collection's schema as a primitive, which decodes what it reads and checks and encodes what you write. Without one, the handle works with encoded values only. Its schemaVersion option is the schema version documents are created at (1 by default).

Promise and Effect

EngineSDK returns promises. Its core is EngineClient, whose methods return Effect values, with the same handles:

import * as Effect from "effect/Effect";

const program = Effect.gen(function* () {
  const boards = sdk.client
    .database(databaseId)
    .collection(collectionId, { primitive: BoardDocument });
  return yield* boards.get("board-1");
});

const board = await sdk.run(program);

sdk.client is the EngineClient the SDK runs, and sdk.run runs one of its effects. Each collection handle also has an effect property with the underlying Effect handle. To build an EngineClient on your own HttpClient, pass it to new EngineClient(httpClient, config).

With either form, every failure is an EngineError with the server's code. See Errors.

Identifying a service

Pass consumer to tell the server which service is calling and which schema versions it can read and write:

const sdk = new EngineSDK({
  url,
  credential,
  consumer: { name: "billing", build: "2026.10.1", versions: [3, 4] },
});

The SDK sends it with every request as the X-Mimic-Consumer header. The server refuses, with schema_mismatch, a request from the service that reads a document at a schema version outside versions, or writes one naming such a version, instead of serving a shape the service cannot read. It keeps no record of services: deploy services that support a new schema version before the migration to it is activated.

name is 1 to 64 and build 1 to 128 printable ASCII characters without ;. The constructor throws a TypeError for a value the server would ignore.

Next